-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinter.go
80 lines (69 loc) · 1.46 KB
/
printer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"encoding/json"
"fmt"
"io"
"strconv"
"strings"
)
type printer interface {
Print(repository) error
}
type multiprinter struct {
printers []printer
}
func (p *multiprinter) Print(repo repository) error {
for _, p := range p.printers {
p.Print(repo)
}
return nil
}
type ioprinter struct {
w io.Writer
params string
}
func (p *ioprinter) Print(repo repository) error {
parts := strings.Split(p.params, ",")
buf := make([]string, 0)
for _, v := range parts {
switch strings.ToLower(v) {
case "name":
buf = append(buf, repo.Name)
case "fullname":
buf = append(buf, repo.FullName)
case "owner":
buf = append(buf, repo.Owner)
case "stars":
buf = append(buf, strconv.Itoa(repo.Stars))
case "pushed_at":
buf = append(buf, repo.PushedAt.String())
case "created_at":
buf = append(buf, repo.CreatedAt.String())
case "updated_at":
buf = append(buf, repo.UpdatedAt.String())
case "description":
buf = append(buf, repo.Description)
case "url":
buf = append(buf, repo.URL)
case "default_branch":
buf = append(buf, repo.DefaultBranch)
}
}
if len(buf) > 0 {
_, err := fmt.Fprintln(p.w, strings.Join(buf, " "))
return err
}
_, err := fmt.Fprintln(p.w, repo.FullName)
return err
}
type jsonprinter struct {
w io.Writer
}
func (p *jsonprinter) Print(repo repository) error {
buf, err := json.Marshal(repo)
if err != nil {
return err
}
_, err = fmt.Fprintln(p.w, string(buf))
return err
}