This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 276
/
version.go
70 lines (56 loc) · 1.89 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
// +build !homebrew
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package cmd
import (
"time"
ttnlog "github.com/TheThingsNetwork/go-utils/log"
"github.com/TheThingsNetwork/ttn/utils/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const unknown = "unknown"
var versionCmd = &cobra.Command{
Use: "version",
Short: "Get build and version information",
Long: `ttnctl version gets the build and version information of ttnctl`,
Run: func(cmd *cobra.Command, args []string) {
assertArgsLength(cmd, args, 0, 0)
gitBranch := viper.GetString("gitBranch")
gitCommit := viper.GetString("gitCommit")
buildDate := viper.GetString("buildDate")
ctx.WithFields(ttnlog.Fields{
"Version": viper.GetString("version"),
"Branch": gitBranch,
"Commit": gitCommit,
"BuildDate": buildDate,
}).Info("Got build information")
if gitBranch == unknown || gitCommit == unknown || buildDate == unknown {
ctx.Warn("This is not an official ttnctl build")
ctx.Warn("If you're building ttnctl from source, you should use the Makefile")
return
}
latest, err := version.GetLatestInfo()
if err != nil {
ctx.WithError(err).Warn("Could not get latest version information")
return
}
if latest.Commit == gitCommit {
ctx.Info("This is an up-to-date ttnctl build")
return
}
if buildDate, err := time.Parse(time.RFC3339, buildDate); err == nil {
ctx.Warn("This is not the latest official ttnctl build")
if buildDate.Before(latest.Date) {
ctx.Warnf("The newest ttnctl build is %s newer.", latest.Date.Sub(buildDate))
} else {
ctx.Warn("Your ttnctl build is newer than the latest official one, which is fine if you're a developer")
}
} else {
ctx.Warn("This ttnctl contains invalid build information")
}
},
}
func init() {
RootCmd.AddCommand(versionCmd)
}