@@ -6,32 +6,28 @@ import (
6
6
"io"
7
7
"os"
8
8
"runtime/debug"
9
- "strings"
10
9
11
10
"github.com/fatih/color"
12
11
"github.com/spf13/cobra"
13
12
)
14
13
15
14
type BuildInfo struct {
16
- GoVersion string `json:"goVersion"`
17
- Version string `json:"version"`
18
- Commit string `json:"commit"`
19
- Date string `json:"date"`
15
+ GoVersion string `json:"goVersion"`
16
+ Version string `json:"version"`
17
+ Commit string `json:"commit"`
18
+ Date string `json:"date"`
19
+ BuildInfo * debug.BuildInfo `json:"buildInfo,omitempty"`
20
20
}
21
21
22
22
func (b BuildInfo ) String () string {
23
23
return fmt .Sprintf ("golangci-lint has version %s built with %s from %s on %s" ,
24
24
b .Version , b .GoVersion , b .Commit , b .Date )
25
25
}
26
26
27
- type versionInfo struct {
28
- Info BuildInfo
29
- BuildInfo * debug.BuildInfo
30
- }
31
-
32
27
type versionOptions struct {
33
- Format string
34
- Debug bool
28
+ Debug bool
29
+ JSON bool
30
+ Short bool
35
31
}
36
32
37
33
type versionCommand struct {
@@ -55,43 +51,36 @@ func newVersionCommand(info BuildInfo) *versionCommand {
55
51
fs := versionCmd .Flags ()
56
52
fs .SortFlags = false // sort them as they are defined here
57
53
58
- fs .StringVar (& c .opts .Format , "format" , "" , color .GreenString ("The version's format can be: 'short', 'json'" ))
59
54
fs .BoolVar (& c .opts .Debug , "debug" , false , color .GreenString ("Add build information" ))
55
+ fs .BoolVar (& c .opts .JSON , "json" , false , color .GreenString ("Display as JSON" ))
56
+ fs .BoolVar (& c .opts .Short , "short" , false , color .GreenString ("Display only the version number" ))
60
57
61
58
c .cmd = versionCmd
62
59
63
60
return c
64
61
}
65
62
66
63
func (c * versionCommand ) execute (_ * cobra.Command , _ []string ) error {
64
+ var info * debug.BuildInfo
67
65
if c .opts .Debug {
68
- info , ok := debug .ReadBuildInfo ()
69
- if ! ok {
70
- return nil
71
- }
72
-
73
- switch strings .ToLower (c .opts .Format ) {
74
- case "json" :
75
- return json .NewEncoder (os .Stdout ).Encode (versionInfo {
76
- Info : c .info ,
77
- BuildInfo : info ,
78
- })
79
-
80
- default :
81
- fmt .Println (info .String ())
82
- return printVersion (os .Stdout , c .info )
83
- }
66
+ info , _ = debug .ReadBuildInfo ()
84
67
}
85
68
86
- switch strings .ToLower (c .opts .Format ) {
87
- case "short" :
88
- fmt .Println (c .info .Version )
89
- return nil
69
+ switch {
70
+ case c .opts .JSON :
71
+ c .info .BuildInfo = info
90
72
91
- case "json" :
92
73
return json .NewEncoder (os .Stdout ).Encode (c .info )
74
+ case c .opts .Short :
75
+ fmt .Println (c .info .Version )
76
+
77
+ return nil
93
78
94
79
default :
80
+ if info != nil {
81
+ fmt .Println (info .String ())
82
+ }
83
+
95
84
return printVersion (os .Stdout , c .info )
96
85
}
97
86
}
0 commit comments