-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.go
161 lines (148 loc) · 3.45 KB
/
get.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"text/tabwriter"
"github.com/docker/docker/client"
"github.com/mitchellh/cli"
"github.com/pkg/errors"
"github.com/schattian/bencher/internal/bencher"
"go.etcd.io/bbolt"
)
type getCmd struct {
docker *client.Client
}
func prepareGet() (cli.Command, error) {
docker, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
return nil, err
}
pruneContainers(context.Background(), docker)
return &getCmd{docker: docker}, nil
}
func (cmd *getCmd) Run(args []string) int {
db, err := initDB()
if err != nil {
fmt.Printf("err initDB: %v", err)
return 1
}
defer db.Close()
switch len(args) {
case 0:
err = errors.Wrap(cmd.printListJobs(db), "printListJobs")
case 1:
err = errors.Wrap(cmd.printJobDetail(db, args[0]), "printJobDetail")
default:
return cli.RunResultHelp
}
if err != nil {
fmt.Printf("err %v", err)
return 1
}
return 0
}
func (cmd *getCmd) printJobDetail(db *bbolt.DB, version string) error {
j := &bencher.Job{}
err := db.View(func(tx *bbolt.Tx) error {
bJobs := tx.Bucket(bencher.KeyJob)
if bJobs == nil {
return nil
}
b := bJobs.Get([]byte(version))
if b == nil {
return nil
}
return json.Unmarshal(b, j)
})
if err != nil {
return errors.Wrap(err, "db.View")
}
if j.Version == "" {
v, err := getRunningVersion()
if os.IsNotExist(err) {
err = nil
}
if err != nil {
return errors.Wrap(err, "getRunningVersion")
}
if v != version {
fmt.Printf("job %s not found", version)
return nil
}
j.Version = v
}
detail := fmt.Sprintf("name: %s\nstatus: %s", j.Version, j.Status())
if j.Stdout != "" {
detail = fmt.Sprintf("%s\noutput:\n\t%s", detail, strings.ReplaceAll(j.Stdout, "\n", "\n\t"))
}
if j.Stderr != "" {
detail = fmt.Sprintf("%s\nerror:\n\t%s", detail, strings.ReplaceAll(j.Stderr, "\n", "\n\t"))
}
fmt.Println(detail)
return nil
}
func (cmd *getCmd) printListJobs(db *bbolt.DB) error {
jobs, err := listJobs(db)
if err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 3, 3, 3, ' ', 0)
fmt.Fprintln(w, "name\tstatus\t")
for _, job := range jobs {
fmt.Fprintf(w, "%s\t%s\t\n", job.Version, job.Status())
}
runningVer, err := getRunningVersion()
if os.IsNotExist(err) {
err = nil
}
if err != nil {
return errors.Wrap(err, "getRunningVersion")
}
if runningVer != "" {
fmt.Fprintf(w, "%s\t%s\t\n", runningVer, "running")
}
err = db.View(func(tx *bbolt.Tx) error {
bSched := tx.Bucket(bencher.KeySched)
if bSched == nil {
return nil
}
sched := string(bSched.Get(bencher.KeySched))
for i, pendingVer := range strings.Split(sched, ",") {
if pendingVer == "" {
continue
}
fmt.Fprintf(w, "%s\t%s\t\n", pendingVer, fmt.Sprintf("scheduled at order #%d", i))
}
return nil
})
w.Flush()
return nil
}
func listJobs(db *bbolt.DB) (jobs []*bencher.Job, err error) {
err = db.View(func(tx *bbolt.Tx) error {
bJobs := tx.Bucket(bencher.KeyJob)
if bJobs == nil {
return nil
}
return bJobs.ForEach(func(version, jobBytes []byte) error {
j := &bencher.Job{}
err := json.Unmarshal(jobBytes, j)
if err != nil {
return err
}
jobs = append(jobs, j)
return nil
})
})
return
}
func (cmd *getCmd) Synopsis() string {
return `get job(s) detail or print the jobs list`
}
func (cmd *getCmd) Help() string {
return `Usage: bencher get [version]
Print details for the given version. In case no version is given, list all jobs. It's aliased with "ls"`
}