-
Notifications
You must be signed in to change notification settings - Fork 149
/
cmd.go
85 lines (71 loc) · 2.15 KB
/
cmd.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
package version
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/weaveworks/go-checkpoint"
"github.com/weaveworks/weave-gitops/pkg/flux"
"github.com/weaveworks/weave-gitops/pkg/osys"
"github.com/weaveworks/weave-gitops/pkg/runner"
"github.com/spf13/cobra"
)
// The current wego version
var Version = "v0.0.0"
var GitCommit = ""
var Branch = ""
var BuildTime = ""
var Cmd = &cobra.Command{
Use: "version",
Short: "Display gitops version",
Run: runCmd,
PostRun: func(cmd *cobra.Command, args []string) {
CheckVersion(CheckpointParams())
},
}
func runCmd(cmd *cobra.Command, args []string) {
fmt.Println("Current Version:", Version)
fmt.Println("GitCommit:", GitCommit)
fmt.Println("BuildTime:", BuildTime)
fmt.Println("Branch:", Branch)
version, err := CheckFluxVersion()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Flux Version:", version)
}
// CheckVersion looks to see if there is a newer version of the software available
func CheckVersion(p *checkpoint.CheckParams) {
checkResponse, err := checkpoint.Check(p)
if err == nil && checkResponse.Outdated {
log.Infof("gitops version %s is available; please update at %s",
checkResponse.CurrentVersion, checkResponse.CurrentDownloadURL)
}
}
// CheckpointParams creates the structure to pass to CheckVersion
func CheckpointParams() *checkpoint.CheckParams {
return &checkpoint.CheckParams{
Product: "weave-gitops",
Version: Version,
}
}
// CheckpointParamsWithFlags adds the object and command from the arguments list to the checkpoint parameters
func CheckpointParamsWithFlags(params *checkpoint.CheckParams, c *cobra.Command) *checkpoint.CheckParams {
// wego uses noun verb command syntax and the parent command will have the noun and the command passed in will be the verb
p := params
if params == nil {
p = CheckpointParams()
}
if c.HasParent() && c.Parent().Name() != "wego" {
p.Flags = map[string]string{
"object": c.Parent().Name(),
"command": c.Name(),
}
}
return p
}
func CheckFluxVersion() (string, error) {
cliRunner := &runner.CLIRunner{}
osysClient := osys.New()
fluxClient := flux.New(osysClient, cliRunner)
return fluxClient.GetVersion()
}