Skip to content

Commit a35fd6e

Browse files
author
Sergey Vilgelm
authored
Support short and json formats for version cmd (#1315)
1 parent 8084559 commit a35fd6e

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-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: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,59 @@
11
package commands
22

33
import (
4+
"encoding/json"
5+
"strings"
6+
47
"github.com/spf13/cobra"
8+
"github.com/spf13/pflag"
9+
10+
"github.com/golangci/golangci-lint/pkg/config"
511
)
612

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

1657
e.rootCmd.AddCommand(versionCmd)
58+
e.initVersionConfiguration(versionCmd)
1759
}

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:"format"`
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)