-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -521,6 +521,10 @@ type Severity struct { | |
Rules []SeverityRule `mapstructure:"rules"` | ||
} | ||
|
||
type Version struct { | ||
Format string `mapstructure:"format"` | ||
} | ||
|
||
type Config struct { | ||
Run Run | ||
|
||
|
@@ -539,6 +543,7 @@ type Config struct { | |
Linters Linters | ||
Issues Issues | ||
Severity Severity | ||
Version Version | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so this one will come in .golangci.yaml file as well ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)"} There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
|
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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:golangci-lint/pkg/commands/run.go
Line 244 in f7417e6
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 flagsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see 👍