|
| 1 | +package tag |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + |
| 7 | + "github.com/MakeNowJust/heredoc/v2" |
| 8 | + "github.com/OctopusDeploy/cli/pkg/cmd" |
| 9 | + "github.com/OctopusDeploy/cli/pkg/constants" |
| 10 | + "github.com/OctopusDeploy/cli/pkg/factory" |
| 11 | + "github.com/OctopusDeploy/cli/pkg/output" |
| 12 | + "github.com/OctopusDeploy/cli/pkg/question" |
| 13 | + "github.com/OctopusDeploy/cli/pkg/question/selectors" |
| 14 | + "github.com/OctopusDeploy/cli/pkg/util/flag" |
| 15 | + "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/projects" |
| 16 | + "github.com/spf13/cobra" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + FlagTag = "tag" |
| 21 | + FlagProject = "project" |
| 22 | +) |
| 23 | + |
| 24 | +type TagFlags struct { |
| 25 | + Tag *flag.Flag[[]string] |
| 26 | + Project *flag.Flag[string] |
| 27 | +} |
| 28 | + |
| 29 | +func NewTagFlags() *TagFlags { |
| 30 | + return &TagFlags{ |
| 31 | + Tag: flag.New[[]string](FlagTag, false), |
| 32 | + Project: flag.New[string](FlagProject, false), |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func NewCmdTag(f factory.Factory) *cobra.Command { |
| 37 | + createFlags := NewTagFlags() |
| 38 | + |
| 39 | + cmd := &cobra.Command{ |
| 40 | + Use: "tag", |
| 41 | + Short: "Override tags for a project", |
| 42 | + Long: "Override tags for a project in Octopus Deploy", |
| 43 | + Example: heredoc.Docf("$ %s project tag Project-1", constants.ExecutableName), |
| 44 | + RunE: func(c *cobra.Command, _ []string) error { |
| 45 | + opts := NewTagOptions(createFlags, cmd.NewDependencies(f, c)) |
| 46 | + |
| 47 | + return createRun(opts) |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + flags := cmd.Flags() |
| 52 | + flags.StringArrayVarP(&createFlags.Tag.Value, createFlags.Tag.Name, "t", []string{}, "Tag to apply to project, must use canonical name: <tag_set>/<tag_name>") |
| 53 | + flags.StringVar(&createFlags.Project.Value, createFlags.Project.Name, "", "Name or ID of the project you wish to update") |
| 54 | + |
| 55 | + return cmd |
| 56 | +} |
| 57 | + |
| 58 | +func createRun(opts *TagOptions) error { |
| 59 | + var optsArray []cmd.Dependable |
| 60 | + var err error |
| 61 | + if !opts.NoPrompt { |
| 62 | + optsArray, err = PromptMissing(opts) |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + } else { |
| 67 | + // Validate tags when running with --no-prompt |
| 68 | + if len(opts.Tag.Value) > 0 { |
| 69 | + tagSets, err := opts.GetAllTagsCallback() |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + if err := selectors.ValidateTags(opts.Tag.Value, tagSets); err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + } |
| 77 | + optsArray = append(optsArray, opts) |
| 78 | + } |
| 79 | + |
| 80 | + for _, o := range optsArray { |
| 81 | + if err := o.Commit(); err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if !opts.NoPrompt { |
| 87 | + fmt.Fprintln(opts.Out, "\nAutomation Commands:") |
| 88 | + for _, o := range optsArray { |
| 89 | + o.GenerateAutomationCmd() |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +func PromptMissing(opts *TagOptions) ([]cmd.Dependable, error) { |
| 97 | + nestedOpts := []cmd.Dependable{} |
| 98 | + |
| 99 | + project, err := AskProjects(opts.Ask, opts.Out, opts.Project.Value, opts.GetProjectsCallback, opts.GetProjectCallback) |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + opts.project = project |
| 104 | + opts.Project.Value = project.Name |
| 105 | + |
| 106 | + tagSets, err := opts.GetAllTagsCallback() |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + |
| 111 | + tags, err := selectors.Tags(opts.Ask, opts.project.ProjectTags, opts.Tag.Value, tagSets) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + opts.Tag.Value = tags |
| 116 | + |
| 117 | + nestedOpts = append(nestedOpts, opts) |
| 118 | + return nestedOpts, nil |
| 119 | +} |
| 120 | + |
| 121 | +func AskProjects(ask question.Asker, out io.Writer, value string, getProjectsCallback GetProjectsCallback, getProjectCallback GetProjectCallback) (*projects.Project, error) { |
| 122 | + if value != "" { |
| 123 | + project, err := getProjectCallback(value) |
| 124 | + if err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + return project, nil |
| 128 | + } |
| 129 | + |
| 130 | + // Check if there's only one project |
| 131 | + projs, err := getProjectsCallback() |
| 132 | + if err != nil { |
| 133 | + return nil, err |
| 134 | + } |
| 135 | + if len(projs) == 1 { |
| 136 | + fmt.Fprintf(out, "Selecting only available project '%s'.\n", output.Cyan(projs[0].Name)) |
| 137 | + return projs[0], nil |
| 138 | + } |
| 139 | + |
| 140 | + project, err := selectors.Select(ask, "Select the Project you would like to update", getProjectsCallback, func(item *projects.Project) string { |
| 141 | + return item.Name |
| 142 | + }) |
| 143 | + if err != nil { |
| 144 | + return nil, err |
| 145 | + } |
| 146 | + |
| 147 | + return project, nil |
| 148 | +} |
0 commit comments