Skip to content

Commit

Permalink
worker: use local (root) command arguments instead of global (Persist…
Browse files Browse the repository at this point in the history
…ent) arguments
  • Loading branch information
displague committed Sep 23, 2020
1 parent b9d655a commit 385caf4
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions cmd/tink-worker/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,24 @@ func NewRootCommand(version string, logger log.Logger) *cobra.Command {
Use: "tink-worker",
Short: "Tink Worker",
Version: version,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, args []string) error {
viper, err := createViper()
if err != nil {
return err
}
return applyViper(viper, cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
retryInterval, _ := cmd.PersistentFlags().GetDuration("retry-interval")
retries, _ := cmd.PersistentFlags().GetInt("retries")
retryInterval, _ := cmd.Flags().GetDuration("retry-interval")
retries, _ := cmd.Flags().GetInt("retries")
// TODO(displague) is log-level no longer useful?
// logLevel, _ := cmd.PersistentFlags().GetString("log-level")
workerID, _ := cmd.PersistentFlags().GetString("id")
maxFileSize, _ := cmd.PersistentFlags().GetInt64("max-file-size")
timeOut, _ := cmd.PersistentFlags().GetDuration("timeout")
user, _ := cmd.PersistentFlags().GetString("registry-username")
pwd, _ := cmd.PersistentFlags().GetString("registry-password")
registry, _ := cmd.PersistentFlags().GetString("docker-registry")
// logLevel, _ := cmd.Flags().GetString("log-level")
workerID, _ := cmd.Flags().GetString("id")
maxFileSize, _ := cmd.Flags().GetInt64("max-file-size")
timeOut, _ := cmd.Flags().GetDuration("timeout")
user, _ := cmd.Flags().GetString("registry-username")
pwd, _ := cmd.Flags().GetString("registry-password")
registry, _ := cmd.Flags().GetString("docker-registry")

logger.With("version", version).Info("starting")
if setupErr := client.Setup(); setupErr != nil {
Expand Down Expand Up @@ -85,27 +85,27 @@ func NewRootCommand(version string, logger log.Logger) *cobra.Command {
},
}

rootCmd.PersistentFlags().Duration("retry-interval", retryIntervalDefault, "Retry interval in seconds")
rootCmd.Flags().Duration("retry-interval", retryIntervalDefault, "Retry interval in seconds")

rootCmd.PersistentFlags().Duration("timeout", time.Duration(defaultTimeoutMinutes*time.Minute), "Max duration to wait for worker to complete")
rootCmd.Flags().Duration("timeout", time.Duration(defaultTimeoutMinutes*time.Minute), "Max duration to wait for worker to complete")

rootCmd.PersistentFlags().Int("max-retry", retryCountDefault, "Maximum number of retries to attempt")
rootCmd.Flags().Int("max-retry", retryCountDefault, "Maximum number of retries to attempt")

rootCmd.PersistentFlags().Int64("max-file-size", defaultMaxFileSize, "Maximum file size in bytes")
rootCmd.Flags().Int64("max-file-size", defaultMaxFileSize, "Maximum file size in bytes")

// rootCmd.PersistentFlags().String("log-level", "info", "Sets the worker log level (panic, fatal, error, warn, info, debug, trace)")
// rootCmd.Flags().String("log-level", "info", "Sets the worker log level (panic, fatal, error, warn, info, debug, trace)")

rootCmd.PersistentFlags().StringP("id", "i", "", "Sets the worker id")
must(rootCmd.MarkPersistentFlagRequired("id"))
rootCmd.Flags().StringP("id", "i", "", "Sets the worker id")
must(rootCmd.MarkFlagRequired("id"))

rootCmd.PersistentFlags().StringP("docker-registry", "r", "", "Sets the Docker registry")
must(rootCmd.MarkPersistentFlagRequired("docker-registry"))
rootCmd.Flags().StringP("docker-registry", "r", "", "Sets the Docker registry")
must(rootCmd.MarkFlagRequired("docker-registry"))

rootCmd.PersistentFlags().StringP("registry-username", "u", "", "Sets the registry username")
must(rootCmd.MarkPersistentFlagRequired("registry-username"))
rootCmd.Flags().StringP("registry-username", "u", "", "Sets the registry username")
must(rootCmd.MarkFlagRequired("registry-username"))

rootCmd.PersistentFlags().StringP("registry-password", "p", "", "Sets the registry-password")
must(rootCmd.MarkPersistentFlagRequired("registry-password"))
rootCmd.Flags().StringP("registry-password", "p", "", "Sets the registry-password")
must(rootCmd.MarkFlagRequired("registry-password"))

return rootCmd
}
Expand Down Expand Up @@ -137,7 +137,7 @@ func createViper() (*viper.Viper, error) {
func applyViper(v *viper.Viper, cmd *cobra.Command) error {
errors := []error{}

cmd.PersistentFlags().VisitAll(func(f *pflag.Flag) {
cmd.Flags().VisitAll(func(f *pflag.Flag) {
if !f.Changed && v.IsSet(f.Name) {
val := v.Get(f.Name)
if err := cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val)); err != nil {
Expand Down

0 comments on commit 385caf4

Please sign in to comment.