diff --git a/cmd/changes_submit_plan.go b/cmd/changes_submit_plan.go index 5d1245e6..6f936a43 100644 --- a/cmd/changes_submit_plan.go +++ b/cmd/changes_submit_plan.go @@ -143,6 +143,14 @@ func SubmitPlan(cmd *cobra.Command, args []string) error { log.WithContext(ctx).WithError(err).WithFields(lf).Debug("Failed to detect repository URL. Use the --repo flag to specify it manually if you require it") } } + tags, err := parseTagsArgument() + if err != nil { + return loggedError{ + err: err, + fields: lf, + message: "Failed to parse tags", + } + } properties := &sdp.ChangeProperties{ Title: title, Description: viper.GetString("description"), @@ -151,6 +159,7 @@ func SubmitPlan(cmd *cobra.Command, args []string) error { RawPlan: tfPlanOutput, CodeChanges: codeChangesOutput, Repo: repoUrl, + Tags: tags, } if changeUuid == uuid.Nil { @@ -289,17 +298,11 @@ func init() { changesCmd.AddCommand(submitPlanCmd) addAPIFlags(submitPlanCmd) + addChangeCreationFlags(submitPlanCmd) + submitPlanCmd.PersistentFlags().String("frontend", "", "The frontend base URL") _ = submitPlanCmd.PersistentFlags().MarkDeprecated("frontend", "This flag is no longer used and will be removed in a future release. Use the '--app' flag instead.") // MarkDeprecated only errors if the flag doesn't exist, we fall back to using app - submitPlanCmd.PersistentFlags().String("title", "", "Short title for this change. If this is not specified, overmind will try to come up with one for you.") - submitPlanCmd.PersistentFlags().String("description", "", "Quick description of the change.") - submitPlanCmd.PersistentFlags().String("ticket-link", "*", "Link to the ticket for this change. Usually this would be the link to something like the pull request, since the CLI uses this as a unique identifier for the change, meaning that multiple runs with the same ticket link will update the same change.") - submitPlanCmd.PersistentFlags().String("owner", "", "The owner of this change.") - submitPlanCmd.PersistentFlags().String("repo", "", "The repository URL that this change should be linked to. This will be automatically detected is possible from the Git config or CI environment.") - // submitPlanCmd.PersistentFlags().String("cc-emails", "", "A comma-separated list of emails to keep updated with the status of this change.") - - submitPlanCmd.PersistentFlags().String("terraform-plan-output", "", "Filename of cached terraform plan output for this change.") - submitPlanCmd.PersistentFlags().String("code-changes-diff", "", "Filename of the code diff of this change.") + submitPlanCmd.PersistentFlags().Int32("blast-radius-link-depth", 0, "Used in combination with '--blast-radius-max-items' to customise how many levels are traversed when calculating the blast radius. Larger numbers will result in a more comprehensive blast radius, but may take longer to calculate. Defaults to the account level settings.") submitPlanCmd.PersistentFlags().Int32("blast-radius-max-items", 0, "Used in combination with '--blast-radius-link-depth' to customise how many items are included in the blast radius. Larger numbers will result in a more comprehensive blast radius, but may take longer to calculate. Defaults to the account level settings.") } diff --git a/cmd/flags.go b/cmd/flags.go new file mode 100644 index 00000000..e4fab4fc --- /dev/null +++ b/cmd/flags.go @@ -0,0 +1,61 @@ +package cmd + +import ( + "fmt" + "strings" + + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +// This file contains re-usable sets of flags that should be used when creating +// commands + +// Adds flags for selecting a change by UUID, frontend URL or ticket link +func addChangeUuidFlags(cmd *cobra.Command) { + cmd.PersistentFlags().String("change", "", "The frontend URL of the change to get") + cmd.PersistentFlags().String("ticket-link", "", "Link to the ticket for this change.") + cmd.PersistentFlags().String("uuid", "", "The UUID of the change that should be displayed.") + cmd.MarkFlagsMutuallyExclusive("change", "ticket-link", "uuid") +} + +// Adds flags that should be present when creating a change +func addChangeCreationFlags(cmd *cobra.Command) { + cmd.PersistentFlags().String("title", "", "Short title for this change. If this is not specified, overmind will try to come up with one for you.") + cmd.PersistentFlags().String("description", "", "Quick description of the change.") + cmd.PersistentFlags().String("ticket-link", "*", "Link to the ticket for this change. Usually this would be the link to something like the pull request, since the CLI uses this as a unique identifier for the change, meaning that multiple runs with the same ticket link will update the same change.") + cmd.PersistentFlags().String("owner", "", "The owner of this change.") + cmd.PersistentFlags().String("repo", "", "The repository URL that this change should be linked to. This will be automatically detected is possible from the Git config or CI environment.") + cmd.PersistentFlags().String("terraform-plan-output", "", "Filename of cached terraform plan output for this change.") + cmd.PersistentFlags().String("code-changes-diff", "", "Filename of the code diff of this change.") + cmd.PersistentFlags().StringSlice("tags", []string{}, "Tags to apply to this change, these should be specified in key=value format. Multiple tags can be specified by repeating the flag or using a comma separated list.") +} + +func parseTagsArgument() (map[string]string, error) { + tags := map[string]string{} + for _, tag := range viper.GetStringSlice("tags") { + parts := strings.SplitN(tag, "=", 2) + if len(parts) != 2 { + return tags, fmt.Errorf("invalid tag format: %s", tag) + } + tags[parts[0]] = parts[1] + } + return tags, nil +} + +// Adds common flags to API commands e.g. timeout +func addAPIFlags(cmd *cobra.Command) { + cmd.PersistentFlags().String("timeout", "10m", "How long to wait for responses") + cmd.PersistentFlags().String("app", "https://app.overmind.tech", "The overmind instance to connect to.") +} + +// Adds terraform-related flags to a command +func addTerraformBaseFlags(cmd *cobra.Command) { + cmd.PersistentFlags().Bool("reset-stored-config", false, "[deprecated: this is now autoconfigured from local terraform files] Set this to reset the sources config stored in Overmind and input fresh values.") + cmd.PersistentFlags().String("aws-config", "", "[deprecated: this is now autoconfigured from local terraform files] The chosen AWS config method, best set through the initial wizard when running the CLI. Options: 'profile_input', 'aws_profile', 'defaults', 'managed'.") + cmd.PersistentFlags().String("aws-profile", "", "[deprecated: this is now autoconfigured from local terraform files] Set this to the name of the AWS profile to use.") + cobra.CheckErr(cmd.PersistentFlags().MarkHidden("reset-stored-config")) + cobra.CheckErr(cmd.PersistentFlags().MarkHidden("aws-config")) + cobra.CheckErr(cmd.PersistentFlags().MarkHidden("aws-profile")) + cmd.PersistentFlags().Bool("only-use-managed-sources", false, "Set this to skip local autoconfiguration and only use the managed sources as configured in Overmind.") +} diff --git a/cmd/root.go b/cmd/root.go index dff77bbf..c5f4c47c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -255,19 +255,6 @@ func parseChangeUrl(changeUrlString string) (uuid.UUID, error) { return changeUuid, nil } -func addChangeUuidFlags(cmd *cobra.Command) { - cmd.PersistentFlags().String("change", "", "The frontend URL of the change to get") - cmd.PersistentFlags().String("ticket-link", "", "Link to the ticket for this change.") - cmd.PersistentFlags().String("uuid", "", "The UUID of the change that should be displayed.") - cmd.MarkFlagsMutuallyExclusive("change", "ticket-link", "uuid") -} - -// Adds common flags to API commands e.g. timeout -func addAPIFlags(cmd *cobra.Command) { - cmd.PersistentFlags().String("timeout", "10m", "How long to wait for responses") - cmd.PersistentFlags().String("app", "https://app.overmind.tech", "The overmind instance to connect to.") -} - type flagError struct { usage string } diff --git a/cmd/terraform_plan.go b/cmd/terraform_plan.go index 9b4951d2..2c09790a 100644 --- a/cmd/terraform_plan.go +++ b/cmd/terraform_plan.go @@ -506,16 +506,6 @@ func getTicketLinkFromPlan(planFile string) (string, error) { return fmt.Sprintf("tfplan://{SHA256}%x", h.Sum(nil)), nil } -func addTerraformBaseFlags(cmd *cobra.Command) { - cmd.PersistentFlags().Bool("reset-stored-config", false, "[deprecated: this is now autoconfigured from local terraform files] Set this to reset the sources config stored in Overmind and input fresh values.") - cmd.PersistentFlags().String("aws-config", "", "[deprecated: this is now autoconfigured from local terraform files] The chosen AWS config method, best set through the initial wizard when running the CLI. Options: 'profile_input', 'aws_profile', 'defaults', 'managed'.") - cmd.PersistentFlags().String("aws-profile", "", "[deprecated: this is now autoconfigured from local terraform files] Set this to the name of the AWS profile to use.") - cobra.CheckErr(cmd.PersistentFlags().MarkHidden("reset-stored-config")) - cobra.CheckErr(cmd.PersistentFlags().MarkHidden("aws-config")) - cobra.CheckErr(cmd.PersistentFlags().MarkHidden("aws-profile")) - cmd.PersistentFlags().Bool("only-use-managed-sources", false, "Set this to skip local autoconfiguration and only use the managed sources as configured in Overmind.") -} - func init() { terraformCmd.AddCommand(terraformPlanCmd)