version icon indicating copy to clipboard operation
version copied to clipboard

Add option to exclude a given version fields and omit empty

Open mszostok opened this issue 3 years ago • 4 comments

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
    • AllFields
    • GoRuntimeFields
    • VCSFields - VCS (Version control systems) related fields (git commit, date, dirty)

mszostok avatar Jul 30 '22 12:07 mszostok

Is anyone working on this? is this available to pickup?

Kamsiy avatar May 03 '23 20:05 Kamsiy

Hi @Kamsiy

It's still available 👍 Ping me if you would like to discuss that in more details, I am happy to assist 👍

mszostok avatar May 05 '23 11:05 mszostok

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

Kamsiy avatar May 10 '23 15:05 Kamsiy

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

Kamsiy avatar May 12 '23 01:05 Kamsiy