Skip to content

Commit baa9052

Browse files
authored
version --assert-matching (keybase#23978)
1 parent 360c5ad commit baa9052

1 file changed

Lines changed: 50 additions & 2 deletions

File tree

‎go/client/cmd_version.go‎

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
package client
55

66
import (
7+
"errors"
8+
"fmt"
9+
710
"github.com/keybase/cli"
811
"github.com/keybase/client/go/libcmdline"
912
"github.com/keybase/client/go/libkb"
@@ -19,8 +22,9 @@ const (
1922
)
2023

2124
type CmdVersion struct {
22-
mode mode
23-
svc bool
25+
mode mode
26+
svc bool
27+
assertMatching bool
2428
libkb.Contextified
2529
}
2630

@@ -45,6 +49,10 @@ func NewCmdVersion(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Comma
4549
Name: "S, no-service",
4650
Usage: "Don't report on the service's build information",
4751
},
52+
cli.BoolFlag{
53+
Name: "assert-matching",
54+
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.",
55+
},
4856
},
4957
Action: func(c *cli.Context) {
5058
cl.ChooseCommand(NewCmdVersionRunner(g), "version", c)
@@ -63,18 +71,58 @@ func (v *CmdVersion) ParseArgv(c *cli.Context) error {
6371
v.mode = modeVerbose
6472
}
6573
v.svc = !c.Bool("S")
74+
v.assertMatching = c.Bool("assert-matching")
75+
if v.assertMatching && (v.mode != modeNormal || !v.svc) {
76+
return errors.New("cannot use --assert-matching with other options")
77+
}
6678
return nil
6779
}
6880

6981
func (v *CmdVersion) Run() error {
7082
var err error
83+
if v.assertMatching {
84+
return v.runAssertMatching()
85+
}
7186
v.runLocal()
7287
if v.svc {
7388
err = v.runService()
7489
}
7590
return err
7691
}
7792

93+
func (v *CmdVersion) runAssertMatching() error {
94+
cli, err := GetConfigClient(v.G())
95+
if err != nil {
96+
v.G().Log.Debug("no service running: %v", err)
97+
return nil
98+
}
99+
conf, err := cli.GetConfig(context.TODO(), 0)
100+
if err != nil {
101+
return err
102+
}
103+
clients, err := cli.GetClientStatus(context.TODO(), 0)
104+
if err != nil {
105+
return err
106+
}
107+
108+
dui := v.G().UI.GetDumbOutputUI()
109+
110+
m := make(map[string]struct{})
111+
m[conf.Version] = struct{}{}
112+
dui.Printf("service: %s\n", conf.Version)
113+
for _, client := range clients {
114+
version := client.Details.Version
115+
dui.Printf("%v: %s\n", client.Details.ClientType, version)
116+
m[version] = struct{}{}
117+
}
118+
119+
if len(m) > 1 {
120+
return fmt.Errorf("failed to assert matching versions; got %d different versions: %v", len(m), m)
121+
}
122+
123+
return nil
124+
}
125+
78126
func (v *CmdVersion) runService() error {
79127
cli, err := GetConfigClient(v.G())
80128
if err != nil {

0 commit comments

Comments
 (0)