|
| 1 | +package create |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/stackitcloud/stackit-cli/internal/cmd/params" |
| 9 | + |
| 10 | + "github.com/goccy/go-yaml" |
| 11 | + "github.com/spf13/cobra" |
| 12 | + "github.com/stackitcloud/stackit-cli/internal/pkg/args" |
| 13 | + "github.com/stackitcloud/stackit-cli/internal/pkg/errors" |
| 14 | + "github.com/stackitcloud/stackit-cli/internal/pkg/examples" |
| 15 | + "github.com/stackitcloud/stackit-cli/internal/pkg/flags" |
| 16 | + "github.com/stackitcloud/stackit-cli/internal/pkg/globalflags" |
| 17 | + "github.com/stackitcloud/stackit-cli/internal/pkg/print" |
| 18 | + "github.com/stackitcloud/stackit-cli/internal/pkg/services/git/client" |
| 19 | + "github.com/stackitcloud/stackit-cli/internal/pkg/spinner" |
| 20 | + "github.com/stackitcloud/stackit-cli/internal/pkg/utils" |
| 21 | + "github.com/stackitcloud/stackit-sdk-go/services/git" |
| 22 | + "github.com/stackitcloud/stackit-sdk-go/services/git/wait" |
| 23 | +) |
| 24 | + |
| 25 | +const ( |
| 26 | + nameFlag = "name" |
| 27 | +) |
| 28 | + |
| 29 | +type inputModel struct { |
| 30 | + *globalflags.GlobalFlagModel |
| 31 | + Id *string |
| 32 | + Name string |
| 33 | +} |
| 34 | + |
| 35 | +func NewCmd(params *params.CmdParams) *cobra.Command { |
| 36 | + cmd := &cobra.Command{ |
| 37 | + Use: "create", |
| 38 | + Short: "Creates STACKIT Git instance", |
| 39 | + Long: "Create a STACKIT Git instance by name.", |
| 40 | + Args: args.NoArgs, |
| 41 | + Example: examples.Build( |
| 42 | + examples.NewExample( |
| 43 | + `Create a instance with name 'my-new-instance'`, |
| 44 | + `$ stackit git create --name my-new-instance`, |
| 45 | + ), |
| 46 | + ), |
| 47 | + RunE: func(cmd *cobra.Command, _ []string) (err error) { |
| 48 | + ctx := context.Background() |
| 49 | + model, err := parseInput(params.Printer, cmd) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + // Configure API client |
| 55 | + apiClient, err := client.ConfigureClient(params.Printer) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + |
| 60 | + if !model.AssumeYes { |
| 61 | + prompt := fmt.Sprintf("Are you sure you want to create the instance %q?", model.Name) |
| 62 | + err = params.Printer.PromptForConfirmation(prompt) |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Call API |
| 69 | + request := buildRequest(ctx, model, apiClient) |
| 70 | + |
| 71 | + result, err := request.Execute() |
| 72 | + if err != nil { |
| 73 | + return fmt.Errorf("create stackit git instance: %w", err) |
| 74 | + } |
| 75 | + model.Id = result.Id |
| 76 | + |
| 77 | + // Wait for async operation, if async mode not enabled |
| 78 | + if !model.Async { |
| 79 | + s := spinner.New(params.Printer) |
| 80 | + s.Start("Creating stackit git instance") |
| 81 | + _, err = wait.CreateGitInstanceWaitHandler(ctx, apiClient, model.ProjectId, *model.Id).WaitWithContext(ctx) |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("wait for stackit git Instance creation: %w", err) |
| 84 | + } |
| 85 | + s.Stop() |
| 86 | + } |
| 87 | + |
| 88 | + return outputResult(params.Printer, model, result) |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + configureFlags(cmd) |
| 93 | + return cmd |
| 94 | +} |
| 95 | + |
| 96 | +func configureFlags(cmd *cobra.Command) { |
| 97 | + cmd.Flags().String(nameFlag, "", "The name of the instance.") |
| 98 | + if err := flags.MarkFlagsRequired(cmd, nameFlag); err != nil { |
| 99 | + cobra.CheckErr(err) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) { |
| 104 | + globalFlags := globalflags.Parse(p, cmd) |
| 105 | + |
| 106 | + if globalFlags.ProjectId == "" { |
| 107 | + return nil, &errors.ProjectIdError{} |
| 108 | + } |
| 109 | + name := flags.FlagToStringValue(p, cmd, nameFlag) |
| 110 | + |
| 111 | + model := inputModel{ |
| 112 | + GlobalFlagModel: globalFlags, |
| 113 | + Name: name, |
| 114 | + } |
| 115 | + |
| 116 | + if p.IsVerbosityDebug() { |
| 117 | + modelStr, err := print.BuildDebugStrFromInputModel(model) |
| 118 | + if err != nil { |
| 119 | + p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err) |
| 120 | + } else { |
| 121 | + p.Debug(print.DebugLevel, "parsed input values: %s", modelStr) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return &model, nil |
| 126 | +} |
| 127 | + |
| 128 | +func buildRequest(ctx context.Context, model *inputModel, apiClient *git.APIClient) git.ApiCreateInstanceRequest { |
| 129 | + return apiClient.CreateInstance(ctx, model.ProjectId).CreateInstancePayload(createPayload(model)) |
| 130 | +} |
| 131 | + |
| 132 | +func createPayload(model *inputModel) git.CreateInstancePayload { |
| 133 | + return git.CreateInstancePayload{ |
| 134 | + Name: &model.Name, |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func outputResult(p *print.Printer, model *inputModel, resp *git.Instance) error { |
| 139 | + if model == nil { |
| 140 | + return fmt.Errorf("input model is nil") |
| 141 | + } |
| 142 | + var outputFormat string |
| 143 | + if model.GlobalFlagModel != nil { |
| 144 | + outputFormat = model.OutputFormat |
| 145 | + } |
| 146 | + switch outputFormat { |
| 147 | + case print.JSONOutputFormat: |
| 148 | + details, err := json.MarshalIndent(resp, "", " ") |
| 149 | + if err != nil { |
| 150 | + return fmt.Errorf("marshal instance: %w", err) |
| 151 | + } |
| 152 | + p.Outputln(string(details)) |
| 153 | + |
| 154 | + return nil |
| 155 | + case print.YAMLOutputFormat: |
| 156 | + details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler()) |
| 157 | + if err != nil { |
| 158 | + return fmt.Errorf("marshal iminstanceage: %w", err) |
| 159 | + } |
| 160 | + p.Outputln(string(details)) |
| 161 | + |
| 162 | + return nil |
| 163 | + default: |
| 164 | + p.Outputf("Created instance %q with id %s\n", model.Name, utils.PtrString(model.Id)) |
| 165 | + return nil |
| 166 | + } |
| 167 | +} |
0 commit comments