|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "maps" |
| 7 | + "slices" |
| 8 | + |
| 9 | + "github.com/chainguard-dev/platform-examples/digestabotctl/digestabot" |
| 10 | + "github.com/chainguard-dev/platform-examples/digestabotctl/platforms" |
| 11 | + "github.com/spf13/cobra" |
| 12 | + "github.com/spf13/viper" |
| 13 | +) |
| 14 | + |
| 15 | +func validateEnvs(vals ...string) error { |
| 16 | + var errs []error |
| 17 | + for _, v := range vals { |
| 18 | + if !viper.IsSet(v) { |
| 19 | + errs = append(errs, fmt.Errorf("%v must be set", v)) |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + return errors.Join(errs...) |
| 24 | +} |
| 25 | + |
| 26 | +// Flags are defined here. Because of the way Viper binds values, if the same flag name is called |
| 27 | +// with viper.BindPFlag multiple times during init() the value will be overwritten. For example if |
| 28 | +// two subcommands each have a flag called name but they each have their own default values, |
| 29 | +// viper can overwrite any value passed in for one subcommand with the default value of the other subcommand. |
| 30 | +// The answer here is to not use init() and instead use something like PersistentPreRun to bind the |
| 31 | +// viper values. Using init for the cobra flags is ok, they are only in here to limit duplication of names. |
| 32 | + |
| 33 | +// bindFileFlags binds the file flag values to viper |
| 34 | +func bindFileFlags(cmd *cobra.Command) { |
| 35 | + viper.BindPFlag("file_types", cmd.Flags().Lookup("file-types")) |
| 36 | + viper.BindPFlag("directory", cmd.Flags().Lookup("directory")) |
| 37 | +} |
| 38 | + |
| 39 | +// fileFlags adds the file flags to the passed in command |
| 40 | +func fileFlags(cmd *cobra.Command) { |
| 41 | + cmd.Flags().StringSliceP("file-types", "f", digestabot.DefaultFileTypes, "Files to update") |
| 42 | + cmd.Flags().StringP("directory", "d", ".", "Directory to update files") |
| 43 | +} |
| 44 | + |
| 45 | +// bindFileFlags binds the pr flag values to viper |
| 46 | +func bindPRFlags(cmd *cobra.Command) { |
| 47 | + viper.BindPFlag("create_pr", cmd.Flags().Lookup("create-pr")) |
| 48 | + viper.BindPFlag("owner", cmd.Flags().Lookup("owner")) |
| 49 | + viper.BindPFlag("repo", cmd.Flags().Lookup("repo")) |
| 50 | + viper.BindPFlag("branch", cmd.Flags().Lookup("branch")) |
| 51 | + viper.BindPFlag("base", cmd.Flags().Lookup("base")) |
| 52 | + viper.BindPFlag("title", cmd.Flags().Lookup("title")) |
| 53 | + viper.BindPFlag("token", cmd.Flags().Lookup("token")) |
| 54 | + viper.BindPFlag("description", cmd.Flags().Lookup("description")) |
| 55 | + viper.BindPFlag("platform", cmd.Flags().Lookup("platform")) |
| 56 | +} |
| 57 | + |
| 58 | +// prFlags adds the pr flags to the passed in command |
| 59 | +func prFlags(cmd *cobra.Command) { |
| 60 | + cmd.PersistentFlags().Bool("create-pr", false, "Create a PR") |
| 61 | + cmd.PersistentFlags().String("owner", "", "Repo owner/organization") |
| 62 | + cmd.PersistentFlags().String("repo", "", "Repo name") |
| 63 | + cmd.PersistentFlags().String("branch", "", "branch for commit") |
| 64 | + cmd.PersistentFlags().String("base", "main", "branch for PR to merge into") |
| 65 | + cmd.PersistentFlags().String("title", "Updating image digests", "PR title") |
| 66 | + cmd.PersistentFlags().String("token", "", "API token") |
| 67 | + cmd.PersistentFlags().String("description", "Updating image digests", "PR description") |
| 68 | + cmd.PersistentFlags().String("platform", "", fmt.Sprintf("Platform to create the PR. Options are %s", slices.Collect(maps.Keys(platforms.ValidPlatforms)))) |
| 69 | +} |
0 commit comments