/
version.go
80 lines (65 loc) · 1.68 KB
/
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
package cmd
import (
"context"
"fmt"
"os"
"sync"
"text/tabwriter"
"github.com/urfave/cli"
"github.com/manifoldco/torus-cli/api"
"github.com/manifoldco/torus-cli/apitypes"
"github.com/manifoldco/torus-cli/config"
)
func init() {
version := cli.Command{
Name: "version",
Usage: "Display versions of utility components",
Category: "SYSTEM",
Action: VersionLookup,
}
Cmds = append(Cmds, version)
}
// VersionLookup ensures the environment is ready and then executes version cmd
func VersionLookup(ctx *cli.Context) error {
return chain(
ensureDaemon, listVersionsCmd, checkUpdates,
)(ctx)
}
func listVersionsCmd(ctx *cli.Context) error {
cfg, err := config.LoadConfig()
if err != nil {
return err
}
client := api.NewClient(cfg)
c := context.Background()
daemonVersion, registryVersion := retrieveVersions(c, client)
w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0)
fmt.Fprintf(w, "%s\t%s\n", "CLI", cfg.Version)
fmt.Fprintf(w, "%s\t%s\n", "Daemon", daemonVersion.Version)
fmt.Fprintf(w, "%s\t%s\n", "Registry", registryVersion.Version)
w.Flush()
return nil
}
func retrieveVersions(c context.Context, client *api.Client) (*apitypes.Version, *apitypes.Version) {
var wg sync.WaitGroup
wg.Add(1)
var daemonVersion *apitypes.Version
go func() {
var dErr error
daemonVersion, dErr = client.Version.GetDaemon(c)
if dErr != nil {
daemonVersion = &apitypes.Version{
Version: "unknown (" + dErr.Error() + ")",
}
}
wg.Done()
}()
registryVersion, rErr := client.Version.Get(c)
if rErr != nil {
registryVersion = &apitypes.Version{
Version: "unknown (" + rErr.Error() + ")",
}
}
wg.Wait()
return daemonVersion, registryVersion
}