|
| 1 | +package release |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/MakeNowJust/heredoc/v2" |
| 9 | + "github.com/ctrlplanedev/cli/internal/api" |
| 10 | + "github.com/ctrlplanedev/cli/internal/cliutil" |
| 11 | + "github.com/spf13/cobra" |
| 12 | + "github.com/spf13/viper" |
| 13 | +) |
| 14 | + |
| 15 | +func safeConvertToReleaseStatus(stat string) (*api.UpdateReleaseJSONBodyStatus, error) { |
| 16 | + if stat == "" { |
| 17 | + return nil, nil |
| 18 | + } |
| 19 | + status := stat |
| 20 | + statusLower := strings.ToLower(status) |
| 21 | + if statusLower == "ready" || statusLower == "" { |
| 22 | + s := api.UpdateReleaseJSONBodyStatusReady |
| 23 | + return &s, nil |
| 24 | + } |
| 25 | + if statusLower == "building" { |
| 26 | + s := api.UpdateReleaseJSONBodyStatusBuilding |
| 27 | + return &s, nil |
| 28 | + } |
| 29 | + if statusLower == "failed" { |
| 30 | + s := api.UpdateReleaseJSONBodyStatusFailed |
| 31 | + return &s, nil |
| 32 | + } |
| 33 | + return nil, fmt.Errorf("invalid release status: %s", status) |
| 34 | +} |
| 35 | + |
| 36 | +func NewUpdateReleaseCmd() *cobra.Command { |
| 37 | + var releaseID string |
| 38 | + var versionFlag string |
| 39 | + var metadata map[string]string |
| 40 | + var configArray map[string]string |
| 41 | + var links map[string]string |
| 42 | + var name string |
| 43 | + var status string |
| 44 | + var message string |
| 45 | + |
| 46 | + cmd := &cobra.Command{ |
| 47 | + Use: "release [flags]", |
| 48 | + Short: "Update a release", |
| 49 | + Long: `Update a release with the specified version and configuration.`, |
| 50 | + Example: heredoc.Doc(` |
| 51 | + # Update a release |
| 52 | + $ ctrlc update release --release-id 1234567890 --version v1.0.0 --status ready |
| 53 | + `), |
| 54 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 55 | + if releaseID == "" { |
| 56 | + return fmt.Errorf("release ID is required") |
| 57 | + } |
| 58 | + |
| 59 | + apiURL := viper.GetString("url") |
| 60 | + apiKey := viper.GetString("api-key") |
| 61 | + client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey) |
| 62 | + if err != nil { |
| 63 | + return fmt.Errorf("failed to create API client: %w", err) |
| 64 | + } |
| 65 | + |
| 66 | + stat, err := safeConvertToReleaseStatus(status) |
| 67 | + if err != nil { |
| 68 | + return fmt.Errorf("failed to convert release status: %w", err) |
| 69 | + } |
| 70 | + |
| 71 | + if len(links) > 0 { |
| 72 | + linksJSON, err := json.Marshal(links) |
| 73 | + if err != nil { |
| 74 | + return fmt.Errorf("failed to marshal links: %w", err) |
| 75 | + } |
| 76 | + metadata["ctrlplane/links"] = string(linksJSON) |
| 77 | + } |
| 78 | + |
| 79 | + config := cliutil.ConvertConfigArrayToNestedMap(configArray) |
| 80 | + |
| 81 | + resp, err := client.UpdateRelease(cmd.Context(), releaseID, api.UpdateReleaseJSONRequestBody{ |
| 82 | + Version: cliutil.StringPtr(versionFlag), |
| 83 | + Metadata: cliutil.MetadataPtr(metadata), |
| 84 | + Config: cliutil.ConfigPtr(config), |
| 85 | + Name: cliutil.StringPtr(name), |
| 86 | + Status: stat, |
| 87 | + Message: cliutil.StringPtr(message), |
| 88 | + }) |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("failed to update release: %w", err) |
| 91 | + } |
| 92 | + |
| 93 | + return cliutil.HandleOutput(cmd, resp) |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + cmd.Flags().StringVarP(&releaseID, "release-id", "r", "", "ID of the release to update (required)") |
| 98 | + cmd.Flags().StringVarP(&versionFlag, "version", "v", "", "Version of the release") |
| 99 | + cmd.Flags().StringToStringVarP(&metadata, "metadata", "m", make(map[string]string), "Metadata key-value pairs (e.g. --metadata key=value)") |
| 100 | + cmd.Flags().StringToStringVarP(&configArray, "config", "c", make(map[string]string), "Config key-value pairs with nested values (can be specified multiple times)") |
| 101 | + cmd.Flags().StringToStringVarP(&links, "link", "l", make(map[string]string), "Links key-value pairs (can be specified multiple times)") |
| 102 | + cmd.Flags().StringVarP(&name, "name", "n", "", "Name of the release") |
| 103 | + cmd.Flags().StringVarP(&status, "status", "s", "", "Status of the release") |
| 104 | + cmd.Flags().StringVar(&message, "message", "", "Message of the release") |
| 105 | + |
| 106 | + cmd.MarkFlagRequired("release-id") |
| 107 | + |
| 108 | + return cmd |
| 109 | +} |
0 commit comments