Skip to content

Support short and json formats for version cmd #1315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ func (e *Executor) getConfigForCommandLine() (*config.Config, error) {
// Use another config variable here, not e.cfg, to not
// affect main parsing by this parsing of only config option.
initFlagSet(fs, &cfg, e.DBManager, false)
initVersionFlagSet(fs, &cfg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you explain rationale behind this line ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. We need to refactor this code. Now the executer uses same set of the flags for all commands, and when I defined the flag for version command only it's failed with an error with unknown --format arg, here:

fs.Usage = func() {} // otherwise help text will be printed twice

This is kind of workaround, I think we need to have a hard refactoring of all this code and do not mix the flags for run command with other command, ideally every command must have independent sets of flags

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see 👍


// Parse max options, even force version option: don't want
// to get access to Executor here: it's error-prone to use
Expand Down
46 changes: 44 additions & 2 deletions pkg/commands/version.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,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",
Run: func(cmd *cobra.Command, _ []string) {
cmd.Printf("golangci-lint has version %s built from %s on %s\n", e.version, e.commit, e.date)
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)
}
5 changes: 5 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,10 @@ type Severity struct {
Rules []SeverityRule `mapstructure:"rules"`
}

type Version struct {
Format string `mapstructure:"format"`
}

type Config struct {
Run Run

Expand All @@ -539,6 +543,7 @@ type Config struct {
Linters Linters
Issues Issues
Severity Severity
Version Version
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this one will come in .golangci.yaml file as well ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it does.

20-08-18 10:28 % ./golangci-lint version
golangci-lint has version (devel) built from (unknown, mod sum: "") on (unknown)


20-08-18 10:28 % git diff
diff --git a/.golangci.yml b/.golangci.yml
index 5e9e1e7..fa899f6 100644
--- a/.golangci.yml
+++ b/.golangci.yml
@@ -146,3 +146,6 @@ service:
   golangci-lint-version: 1.23.x # use the fixed version to not introduce new linters unexpectedly
   prepare:
     - echo "here I can run custom commands, but no preparation needed for this repo"
+
+version:
+  format: json
20-08-18 10:28 % ./golangci-lint version
{"version":"(devel)","commit":"(unknown, mod sum: \"\")","date":"(unknown)"}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My original concern was that if this should be part of configuration file. Then, I think it's not much a big deal as long as we don't break declarative understanding of config file (e.g. it will be very weird to specify actual version in config file).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh no, we don't specify a version in the config file, just the output format


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