Skip to content

Commit

Permalink
Add --version/-v flag to schedule-builder
Browse files Browse the repository at this point in the history
This allows to print the version and exit.

Signed-off-by: Sascha Grunert <[email protected]>
  • Loading branch information
saschagrunert committed Apr 4, 2024
1 parent 8d27211 commit 9a6c26c
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions cmd/schedule-builder/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/spf13/cobra"

"sigs.k8s.io/release-utils/log"
"sigs.k8s.io/release-utils/version"
"sigs.k8s.io/yaml"
)

Expand All @@ -47,6 +48,7 @@ type options struct {
logLevel string
typeFile string
update bool
version bool
}

var opts = &options{}
Expand All @@ -56,14 +58,11 @@ const (
outputFileFlag = "output-file"
typeFlag = "type"
updateFlag = "update"
versionFlag = "version"
typePatch = "patch"
typeRelease = "release"
)

var requiredFlags = []string{
configPathFlag,
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
Expand Down Expand Up @@ -113,18 +112,26 @@ func init() {
fmt.Sprintf("update the '--%s' based on the latest available data (or date). Right now only supported if '--%s' is set to '%s'", configPathFlag, typeFlag, typePatch),
)

for _, flag := range requiredFlags {
if err := rootCmd.MarkPersistentFlagRequired(flag); err != nil {
logrus.Fatal(err)
}
}
rootCmd.PersistentFlags().BoolVarP(
&opts.version,
versionFlag,
"v",
false,
"print the version and exit",
)
}

func initLogging(*cobra.Command, []string) error {
return log.SetupGlobalLogger(opts.logLevel)
}

func run(opts *options) error {
if opts.version {
info := version.GetVersionInfo()
fmt.Print(info.String())
return nil
}

if err := opts.SetAndValidate(); err != nil {
return fmt.Errorf("validating options: %w", err)
}
Expand Down

0 comments on commit 9a6c26c

Please sign in to comment.