Add option to exclude a given version fields and omit empty
Description
Add option to exclude a given version fields. Currently, all fields are displayed:
Version (devel)
Git Commit d03329a
Build Date 30 Jul 22 14:08 CEST (3 seconds ago)
Commit Date 30 Jul 22 13:42 CEST (26 minutes ago)
Dirty Build yes
Go Version 1.18.2
Compiler gc
Platform darwin/amd64
Reasons
Use wants to print shorted, more concise version output and fields such as Compiler or GoVersion can be not important for them.
Use cases
- Exclude Go related fields:
// excludedFields defines preset for fields that should be excluded in output. const excludedFields = version.FieldGoVersion | version.FieldCompiler | version.FieldPlatform printer := version.NewPrinter(version.WithExlcudedFields(excludedFields)) if err := printer.Print(os.Stdout); err != nil { log.Fatal(err) } - Don't display empty(
"") and unset(N/A) fields:printer := version.NewPrinter(version.WithOmitUnset(excludedFields)) if err := printer.Print(os.Stdout); err != nil { log.Fatal(err) }
Implementation
- Use bit masks to set the enabled/disabled fields
const ( FieldVersion = 1 << iota FieldGitCommit FieldBuildDate FieldCommitDate FieldDirtyBuild FieldGoVersion FieldCompiler FieldPlatform typeRevMaxKey ) - Add popular presets
AllFieldsGoRuntimeFieldsVCSFields- VCS (Version control systems) related fields (git commit, date, dirty)
Is anyone working on this? is this available to pickup?
Hi @Kamsiy
It's still available 👍 Ping me if you would like to discuss that in more details, I am happy to assist 👍
Hey @mszostok 👋 ,
In this feature, should the Get function in the version package also exclude fields?
https://github.com/mszostok/version/blob/a69cf43b345ccd616da915912e2dc9deb8bfc992/version.go#L115-L133
Also for the common presets, Please can I get some feedback on my current mapping
GoRuntimeFields - FieldGoVersion | FieldCompiler | FieldPlatform
VCSFields - FieldGitCommit | FieldBuildDate | FieldCommitDate | FieldDirtyBuild
AllFields - VCSFields | GoRuntimeFields | FieldVersion | FieldMeta | FieldExtraFields