Skip to content

Commit b700147

Browse files
committed
add resrouce create
1 parent 21b624d commit b700147

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

cmd/ctrlc/root/api/create/create.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package create
22

33
import (
4+
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create/environment"
45
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create/release"
56
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create/releasechannel"
7+
"github.com/ctrlplanedev/cli/cmd/ctrlc/root/api/create/resources"
68
"github.com/spf13/cobra"
79
)
810

@@ -18,5 +20,7 @@ func NewCreateCmd() *cobra.Command {
1820

1921
cmd.AddCommand(release.NewReleaseCmd())
2022
cmd.AddCommand(releasechannel.NewReleaseChannelCmd())
23+
cmd.AddCommand(environment.NewEnvironmentCmd())
24+
cmd.AddCommand(resources.NewResourcesCmd())
2125
return cmd
2226
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package resources
2+
3+
import (
4+
"fmt"
5+
"strconv"
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/google/uuid"
12+
"github.com/spf13/cobra"
13+
"github.com/spf13/viper"
14+
)
15+
16+
func NewResourcesCmd() *cobra.Command {
17+
var workspaceID string
18+
var name string
19+
var identifier string
20+
var kind string
21+
var version string
22+
var metadata map[string]string
23+
var configArray map[string]string
24+
25+
cmd := &cobra.Command{
26+
Use: "resources [flags]",
27+
Short: "Create a new resources",
28+
Long: `Create a new resources with the specified version and configuration.`,
29+
Example: heredoc.Doc(`
30+
# Create a new resource
31+
$ ctrlc create resources --version v1.0.0
32+
33+
# Create a new resource using Go template syntax
34+
$ ctrlc create resources --version v1.0.0 --template='{{.status.phase}}'
35+
`),
36+
RunE: func(cmd *cobra.Command, args []string) error {
37+
apiURL := viper.GetString("url")
38+
apiKey := viper.GetString("api-key")
39+
client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
40+
if err != nil {
41+
return fmt.Errorf("failed to create API client: %w", err)
42+
}
43+
44+
// Convert configArray into a nested map[string]interface{}
45+
config := make(map[string]interface{})
46+
for path, value := range configArray {
47+
// Split path into segments
48+
segments := strings.Split(path, ".")
49+
50+
// Start at the root map
51+
current := config
52+
53+
// Navigate through segments
54+
for i, segment := range segments {
55+
if i == len(segments)-1 {
56+
// Last segment - try to convert string to number or boolean if possible
57+
if num, err := strconv.ParseFloat(value, 64); err == nil {
58+
current[segment] = num
59+
} else if value == "true" {
60+
current[segment] = true
61+
} else if value == "false" {
62+
current[segment] = false
63+
} else {
64+
current[segment] = value
65+
}
66+
} else {
67+
// Create nested map if it doesn't exist
68+
if _, exists := current[segment]; !exists {
69+
current[segment] = make(map[string]interface{})
70+
}
71+
// Move to next level
72+
current = current[segment].(map[string]interface{})
73+
}
74+
}
75+
}
76+
77+
// Extrat into vars
78+
resp, err := client.UpsertTargets(cmd.Context(), api.UpsertTargetsJSONRequestBody{
79+
Targets: []struct {
80+
Config map[string]interface{} `json:"config"`
81+
Identifier string `json:"identifier"`
82+
Kind string `json:"kind"`
83+
Metadata *map[string]string `json:"metadata,omitempty"`
84+
Name string `json:"name"`
85+
Variables *[]struct {
86+
Key string `json:"key"`
87+
Sensitive *bool `json:"sensitive,omitempty"`
88+
Value api.UpsertTargetsJSONBody_Targets_Variables_Value `json:"value"`
89+
} `json:"variables,omitempty"`
90+
Version string `json:"version"`
91+
}{
92+
{
93+
Version: version,
94+
Identifier: identifier,
95+
Metadata: &metadata,
96+
Name: name,
97+
Kind: kind,
98+
Config: config,
99+
},
100+
},
101+
102+
WorkspaceId: uuid.Must(uuid.Parse(workspaceID)),
103+
})
104+
105+
if err != nil {
106+
return fmt.Errorf("failed to create resource: %w", err)
107+
}
108+
109+
return cliutil.HandleOutput(cmd, resp)
110+
},
111+
}
112+
113+
// Add flags
114+
cmd.Flags().StringVar(&workspaceID, "workspace", "", "ID of the workspace (required)")
115+
cmd.Flags().StringVar(&name, "name", "", "Name of the resource (required)")
116+
cmd.Flags().StringVar(&identifier, "identifier", "", "Identifier of the resource (required)")
117+
cmd.Flags().StringVar(&kind, "kind", "", "Kind of the resource (required)")
118+
cmd.Flags().StringVar(&version, "version", "", "Version of the resource (required)")
119+
cmd.Flags().StringToStringVar(&metadata, "metadata", make(map[string]string), "Metadata key-value pairs (e.g. --metadata key=value)")
120+
cmd.Flags().StringToStringVar(&configArray, "config", make(map[string]string), "Config key-value pairs with nested values (can be specified multiple times)")
121+
122+
cmd.MarkFlagRequired("version")
123+
cmd.MarkFlagRequired("workspace")
124+
cmd.MarkFlagRequired("name")
125+
cmd.MarkFlagRequired("identifier")
126+
cmd.MarkFlagRequired("kind")
127+
128+
return cmd
129+
}

0 commit comments

Comments
 (0)