-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathversion.go
More file actions
59 lines (51 loc) · 1.32 KB
/
version.go
File metadata and controls
59 lines (51 loc) · 1.32 KB
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
package commands
import (
"encoding/json"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/golangci/golangci-lint/pkg/config"
)
type jsonVersion struct {
Version string `json:"version"`
Commit string `json:"commit"`
Date string `json:"date"`
}
func (e *Executor) initVersionConfiguration(cmd *cobra.Command) {
fs := cmd.Flags()
fs.SortFlags = false // sort them as they are defined here
initVersionFlagSet(fs, e.cfg)
}
func initVersionFlagSet(fs *pflag.FlagSet, cfg *config.Config) {
// Version config
vc := &cfg.Version
fs.StringVar(&vc.Format, "format", "", wh("The version's format can be: 'short', 'json'"))
}
func (e *Executor) initVersion() {
versionCmd := &cobra.Command{
Use: "version",
Short: "Version",
RunE: func(cmd *cobra.Command, _ []string) error {
switch strings.ToLower(e.cfg.Version.Format) {
case "short":
cmd.Println(e.version)
case "json":
ver := jsonVersion{
Version: e.version,
Commit: e.commit,
Date: e.date,
}
data, err := json.Marshal(&ver)
if err != nil {
return err
}
cmd.Println(string(data))
default:
cmd.Printf("golangci-lint has version %s built from %s on %s\n", e.version, e.commit, e.date)
}
return nil
},
}
e.rootCmd.AddCommand(versionCmd)
e.initVersionConfiguration(versionCmd)
}