Skip to content

Commit

Permalink
Added support for tags
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanratcliffe committed Nov 27, 2024
1 parent 85a7e25 commit 79960f3
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 32 deletions.
21 changes: 12 additions & 9 deletions cmd/changes_submit_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -151,6 +159,7 @@ func SubmitPlan(cmd *cobra.Command, args []string) error {
RawPlan: tfPlanOutput,
CodeChanges: codeChangesOutput,
Repo: repoUrl,
Tags: tags,
}

if changeUuid == uuid.Nil {
Expand Down Expand Up @@ -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.")
}
61 changes: 61 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
@@ -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.")
}
13 changes: 0 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 0 additions & 10 deletions cmd/terraform_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 79960f3

Please sign in to comment.