Skip to content
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

Add --version/-v flag to schedule-builder #3548

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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