44 "encoding/json"
55 "fmt"
66 "net/http"
7+ "strings"
78 "time"
89
910 "github.com/MakeNowJust/heredoc/v2"
@@ -13,28 +14,47 @@ import (
1314 "github.com/spf13/viper"
1415)
1516
16- func NewCreateReleaseCmd () * cobra.Command {
17+ func safeConvertToReleaseStatus (status string ) (* api.UpsertReleaseJSONBodyStatus , error ) {
18+ statusLower := strings .ToLower (status )
19+ if statusLower == "ready" || statusLower == "" {
20+ s := api .Ready
21+ return & s , nil
22+ }
23+ if statusLower == "building" {
24+ s := api .Building
25+ return & s , nil
26+ }
27+ if statusLower == "failed" {
28+ s := api .Failed
29+ return & s , nil
30+ }
31+ return nil , fmt .Errorf ("invalid release status: %s" , status )
32+ }
33+
34+ func NewUpsertReleaseCmd () * cobra.Command {
1735 var versionFlag string
1836 var deploymentID []string
1937 var metadata map [string ]string
2038 var configArray map [string ]string
2139 var links map [string ]string
2240 var createdAt string
2341 var name string
42+ var status string
43+ var message string
2444
2545 cmd := & cobra.Command {
2646 Use : "release [flags]" ,
27- Short : "Create a new release" ,
28- Long : `Create a new release with the specified version and configuration.` ,
47+ Short : "Upsert a release" ,
48+ Long : `Upsert a release with the specified version and configuration.` ,
2949 Example : heredoc .Doc (`
30- # Create a new release
31- $ ctrlc create release --version v1.0.0 --deployment 1234567890
50+ # Upsert a release
51+ $ ctrlc upsert release --version v1.0.0 --deployment 1234567890
3252
33- # Create a new release using Go template syntax
34- $ ctrlc create release --version v1.0.0 --deployment 1234567890 --template='{{.status.phase}}'
53+ # Upsert a release using Go template syntax
54+ $ ctrlc upsert release --version v1.0.0 --deployment 1234567890 --template='{{.status.phase}}'
3555
36- # Create a new release for multiple deployments
37- $ ctrlc create release --version v1.0.0 --deployment 1234567890 --deployment 0987654321
56+ # Upsert a new release for multiple deployments
57+ $ ctrlc upsert release --version v1.0.0 --deployment 1234567890 --deployment 0987654321
3858 ` ),
3959 RunE : func (cmd * cobra.Command , args []string ) error {
4060 apiURL := viper .GetString ("url" )
@@ -61,16 +81,23 @@ func NewCreateReleaseCmd() *cobra.Command {
6181 metadata ["ctrlplane/links" ] = string (linksJSON )
6282 }
6383
84+ stat , err := safeConvertToReleaseStatus (status )
85+ if err != nil {
86+ return fmt .Errorf ("failed to convert release status: %w" , err )
87+ }
88+
6489 config := cliutil .ConvertConfigArrayToNestedMap (configArray )
6590 var response * http.Response
6691 for _ , id := range deploymentID {
67- resp , err := client .CreateRelease (cmd .Context (), api.CreateReleaseJSONRequestBody {
92+ resp , err := client .UpsertRelease (cmd .Context (), api.UpsertReleaseJSONRequestBody {
6893 Version : versionFlag ,
6994 DeploymentId : id ,
7095 Metadata : & metadata ,
7196 CreatedAt : parsedTime ,
7297 Config : & config ,
7398 Name : & name ,
99+ Status : stat ,
100+ Message : & message ,
74101 })
75102 if err != nil {
76103 return fmt .Errorf ("failed to create release: %w" , err )
@@ -90,6 +117,8 @@ func NewCreateReleaseCmd() *cobra.Command {
90117 cmd .Flags ().StringToStringVarP (& links , "link" , "l" , make (map [string ]string ), "Links key-value pairs (can be specified multiple times)" )
91118 cmd .Flags ().StringVarP (& createdAt , "created-at" , "t" , "" , "Created at timestamp (e.g. --created-at 2024-01-01T00:00:00Z) for the release channel" )
92119 cmd .Flags ().StringVarP (& name , "name" , "n" , "" , "Name of the release channel" )
120+ cmd .Flags ().StringVarP (& status , "status" , "s" , string (api .Ready ), "Status of the release channel (one of: ready, building, failed)" )
121+ cmd .Flags ().StringVar (& message , "message" , "" , "Message of the release channel" )
93122
94123 cmd .MarkFlagRequired ("version" )
95124 cmd .MarkFlagRequired ("deployment" )
0 commit comments