/
cmd.go
68 lines (58 loc) · 1.79 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
package version
import (
"fmt"
"github.com/Aptomi/aptomi/cmd/common"
"github.com/Aptomi/aptomi/pkg/client/rest"
"github.com/Aptomi/aptomi/pkg/client/rest/http"
"github.com/Aptomi/aptomi/pkg/config"
"github.com/Aptomi/aptomi/pkg/version"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
// NewCommand returns instance of cobra command that shows version from version package (injected at build tome)
func NewCommand(cfg *config.Client) *cobra.Command {
var client, server, short bool
cmd := &cobra.Command{
Use: "version",
Short: "Print Aptomi Client (and Server) versions",
Run: func(cmd *cobra.Command, args []string) {
if !client && !server {
client = true
server = true
}
if client {
info := version.GetBuildInfo()
if short {
fmt.Println("Client Version:", info.GitVersion)
} else {
data, err := common.Format(cfg.Output, false, info)
if err != nil {
panic(fmt.Sprintf("error while formating policy: %s", err))
}
log.Infof("Client: ")
fmt.Println(string(data))
}
}
if server {
info, infoErr := rest.New(cfg, http.NewClient(cfg)).Version().Show()
if infoErr != nil {
log.Fatalf("error while getting server version: %s", infoErr)
}
if short {
fmt.Println("Server Version:", info.GitVersion)
} else {
data, err := common.Format(cfg.Output, false, info)
if err != nil {
panic(fmt.Sprintf("error while formating server version: %s", err))
}
log.Infof("Server: ")
fmt.Println(string(data))
}
}
},
}
cmd.Flags().BoolVarP(&client, "client", "c", false, "Show client version")
cmd.Flags().BoolVarP(&server, "server", "s", false, "Show server version")
cmd.Flags().BoolVarP(&short, "short", "", false, "Print just the version number")
return cmd
}