Skip to content

Add option to exclude a given version fields and omit empty #12

@mszostok

Description

@mszostok

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/customizationEnables default behavior customization

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions