Skip to content

Commit a43f412

Browse files
author
Sergey Vilgelm
committed
Support short and json formats for version cmd
1 parent 6ac41d9 commit a43f412

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

pkg/commands/run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ func (e *Executor) getConfigForCommandLine() (*config.Config, error) {
234234
// Use another config variable here, not e.cfg, to not
235235
// affect main parsing by this parsing of only config option.
236236
initFlagSet(fs, &cfg, e.DBManager, false)
237+
initVersionFlagSet(fs, &cfg)
237238

238239
// Parse max options, even force version option: don't want
239240
// to get access to Executor here: it's error-prone to use

pkg/commands/version.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,58 @@
11
package commands
22

33
import (
4+
"encoding/json"
5+
"strings"
6+
7+
"github.com/golangci/golangci-lint/pkg/config"
48
"github.com/spf13/cobra"
9+
"github.com/spf13/pflag"
510
)
611

12+
type jsonVersion struct {
13+
Version string `json:"version"`
14+
Commit string `json:"commit"`
15+
Date string `json:"date"`
16+
}
17+
18+
func (e *Executor) initVersionConfiguration(cmd *cobra.Command) {
19+
fs := cmd.Flags()
20+
fs.SortFlags = false // sort them as they are defined here
21+
initVersionFlagSet(fs, e.cfg)
22+
}
23+
24+
func initVersionFlagSet(fs *pflag.FlagSet, cfg *config.Config) {
25+
// Version config
26+
vc := &cfg.Version
27+
fs.StringVar(&vc.Format, "format", "", wh("The version's format can be: 'short', 'json'"))
28+
}
29+
730
func (e *Executor) initVersion() {
831
versionCmd := &cobra.Command{
932
Use: "version",
1033
Short: "Version",
11-
Run: func(cmd *cobra.Command, _ []string) {
12-
cmd.Printf("golangci-lint has version %s built from %s on %s\n", e.version, e.commit, e.date)
34+
RunE: func(cmd *cobra.Command, _ []string) error {
35+
switch strings.ToLower(e.cfg.Version.Format) {
36+
case "short":
37+
cmd.Println(e.version)
38+
case "json":
39+
ver := jsonVersion{
40+
Version: e.version,
41+
Commit: e.commit,
42+
Date: e.date,
43+
}
44+
data, err := json.Marshal(&ver)
45+
if err != nil {
46+
return err
47+
}
48+
cmd.Println(string(data))
49+
default:
50+
cmd.Printf("golangci-lint has version %s built from %s on %s\n", e.version, e.commit, e.date)
51+
}
52+
return nil
1353
},
1454
}
1555

1656
e.rootCmd.AddCommand(versionCmd)
57+
e.initVersionConfiguration(versionCmd)
1758
}

pkg/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,10 @@ type Severity struct {
521521
Rules []SeverityRule `mapstructure:"rules"`
522522
}
523523

524+
type Version struct {
525+
Format string `mapstructure:"json"`
526+
}
527+
524528
type Config struct {
525529
Run Run
526530

@@ -539,6 +543,7 @@ type Config struct {
539543
Linters Linters
540544
Issues Issues
541545
Severity Severity
546+
Version Version
542547

543548
InternalTest bool // Option is used only for testing golangci-lint code, don't use it
544549
}

0 commit comments

Comments
 (0)