@@ -6,32 +6,28 @@ import (
66 "io"
77 "os"
88 "runtime/debug"
9- "strings"
109
1110 "github.com/fatih/color"
1211 "github.com/spf13/cobra"
1312)
1413
1514type 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"`
2020}
2121
2222func (b BuildInfo ) String () string {
2323 return fmt .Sprintf ("golangci-lint has version %s built with %s from %s on %s" ,
2424 b .Version , b .GoVersion , b .Commit , b .Date )
2525}
2626
27- type versionInfo struct {
28- Info BuildInfo
29- BuildInfo * debug.BuildInfo
30- }
31-
3227type versionOptions struct {
33- Format string
34- Debug bool
28+ Debug bool
29+ JSON bool
30+ Short bool
3531}
3632
3733type versionCommand struct {
@@ -55,43 +51,36 @@ func newVersionCommand(info BuildInfo) *versionCommand {
5551 fs := versionCmd .Flags ()
5652 fs .SortFlags = false // sort them as they are defined here
5753
58- fs .StringVar (& c .opts .Format , "format" , "" , color .GreenString ("The version's format can be: 'short', 'json'" ))
5954 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" ))
6057
6158 c .cmd = versionCmd
6259
6360 return c
6461}
6562
6663func (c * versionCommand ) execute (_ * cobra.Command , _ []string ) error {
64+ var info * debug.BuildInfo
6765 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 ()
8467 }
8568
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
9072
91- case "json" :
9273 return json .NewEncoder (os .Stdout ).Encode (c .info )
74+ case c .opts .Short :
75+ fmt .Println (c .info .Version )
76+
77+ return nil
9378
9479 default :
80+ if info != nil {
81+ fmt .Println (info .String ())
82+ }
83+
9584 return printVersion (os .Stdout , c .info )
9685 }
9786}
0 commit comments