-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
55 lines (47 loc) · 1.4 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
// Package veribi implements command line commands that are user inside Veribi CLI.
package veribi
import (
"encoding/json"
"fmt"
"log"
"github.com/coreos/go-semver/semver"
"github.com/spf13/cobra"
"github.com/vaclav-dvorak/veribi-cli/pkg/veribi"
)
type versionOutput struct {
VersionString string `json:"version"`
Outdated bool `json:"outdated"`
}
var (
outputJSON bool
version = "v0.0.1+sha"
versionCmd = &cobra.Command{
Use: "version",
Aliases: []string{"v"},
Short: "Gets CLI version",
Run: func(cmd *cobra.Command, args []string) {
if !silent && !outputJSON {
printLogo()
}
latest := veribi.GetLatestVersion()
outdated := semver.New(version[1:]).LessThan(*semver.New(latest[1:])) //? we are striping "v" from our tags as its not technically part of semver
if outputJSON {
o, err := json.Marshal(versionOutput{VersionString: version, Outdated: outdated})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", o)
return
}
fmt.Printf("Veribi CLI Version: %s\n", version)
if outdated {
fmt.Printf("\nYour version of Veribi CLI is out of date! The latest version\n"+
"is %s. You can update by downloading from https://github.com/%s/releases\n", latest[1:], veribi.VeribiRepo)
}
},
}
)
func init() {
versionCmd.Flags().BoolVarP(&outputJSON, "json", "j", false, "Get output as JSON object")
rootCmd.AddCommand(versionCmd)
}