-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cmd_version.go
169 lines (149 loc) · 3.82 KB
/
cmd_version.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
162
163
164
165
166
167
168
169
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"errors"
"fmt"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
"golang.org/x/net/context"
)
type mode int
const (
modeNormal mode = iota
modeShort
modeVerbose
)
type CmdVersion struct {
mode mode
svc bool
assertMatching bool
libkb.Contextified
}
func NewCmdVersionRunner(g *libkb.GlobalContext) *CmdVersion {
return &CmdVersion{
mode: modeNormal,
svc: true,
Contextified: libkb.NewContextified(g),
}
}
func NewCmdVersion(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "version",
Usage: "Print out version and build information",
Flags: []cli.Flag{
cli.StringFlag{
Name: "f, format",
Usage: "Alternate format for version output. Specify 's' for simple (1.2.3) or 'v' for verbose. Default (blank) includes build number (1.2.3-400).",
},
cli.BoolFlag{
Name: "S, no-service",
Usage: "Don't report on the service's build information",
},
cli.BoolFlag{
Name: "assert-matching",
Usage: "Return with a successful exit code iff the client binary, the service, KBFS, and the GUI (if they are running) all have matching versions.",
},
},
Action: func(c *cli.Context) {
cl.ChooseCommand(NewCmdVersionRunner(g), "version", c)
cl.SetForkCmd(libcmdline.NoFork)
cl.SetLogForward(libcmdline.LogForwardNone)
cl.SetSkipOutOfDateCheck()
},
}
}
func (v *CmdVersion) ParseArgv(c *cli.Context) error {
switch c.String("format") {
case "s":
v.mode = modeShort
case "v":
v.mode = modeVerbose
}
v.svc = !c.Bool("S")
v.assertMatching = c.Bool("assert-matching")
if v.assertMatching && (v.mode != modeNormal || !v.svc) {
return errors.New("cannot use --assert-matching with other options")
}
return nil
}
func (v *CmdVersion) Run() error {
var err error
if v.assertMatching {
return v.runAssertMatching()
}
v.runLocal()
if v.svc {
err = v.runService()
}
return err
}
func (v *CmdVersion) runAssertMatching() error {
cli, err := GetConfigClient(v.G())
if err != nil {
v.G().Log.Debug("no service running: %v", err)
return nil
}
conf, err := cli.GetConfig(context.TODO(), 0)
if err != nil {
return err
}
clients, err := cli.GetClientStatus(context.TODO(), 0)
if err != nil {
return err
}
dui := v.G().UI.GetDumbOutputUI()
m := make(map[string]struct{})
m[conf.Version] = struct{}{}
dui.Printf("service: %s\n", conf.Version)
for _, client := range clients {
version := client.Details.Version
dui.Printf("%v: %s\n", client.Details.ClientType, version)
m[version] = struct{}{}
}
if len(m) > 1 {
return fmt.Errorf("failed to assert matching versions; got %d different versions: %v", len(m), m)
}
return nil
}
func (v *CmdVersion) runService() error {
cli, err := GetConfigClient(v.G())
if err != nil {
v.G().Log.Debug("no service running: %v", err)
return nil
}
res, err := cli.GetConfig(context.TODO(), 0)
if err != nil {
return err
}
dui := v.G().UI.GetDumbOutputUI()
switch v.mode {
case modeShort:
dui.Printf("Service: %s\n", res.VersionShort)
case modeNormal:
dui.Printf("Service: %s\n", res.Version)
case modeVerbose:
dui.Printf("\n--------- Service Version Information -------\n")
dui.Printf("%s\n", res.VersionFull)
}
return nil
}
func (v *CmdVersion) runLocal() {
dui := v.G().UI.GetDumbOutputUI()
prfx := ""
if v.svc {
prfx = "Client: "
}
switch v.mode {
case modeShort:
dui.Printf("%s%s\n", prfx, libkb.Version)
case modeNormal:
dui.Printf("%s%s\n", prfx, libkb.VersionString())
case modeVerbose:
libkb.VersionMessage(func(s string) { dui.Printf("%s\n", s) })
}
}
func (v *CmdVersion) GetUsage() libkb.Usage {
return libkb.Usage{}
}