Skip to content

Commit 944d5a6

Browse files
committed
fix: switch to singular
by switching to singular, we can use inferred openapi schema documents.
1 parent 9918907 commit 944d5a6

1 file changed

Lines changed: 78 additions & 61 deletions

File tree

internal/service/resource_definition.go

Lines changed: 78 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type DeleteMethod struct {
4141
}
4242

4343
func (r *Resource) ExecuteCommand(args []string) (*http.Request, error) {
44-
c := cobra.Command{Use: r.Plural}
44+
c := cobra.Command{Use: r.Singular}
4545
var err error
4646
var req *http.Request
4747
var parents []*string
@@ -73,74 +73,91 @@ func (r *Resource) ExecuteCommand(args []string) (*http.Request, error) {
7373
return fmt.Sprintf("%s%s", prefix, path)
7474
}
7575

76-
createArgs := map[string]interface{}{}
77-
createCmd := &cobra.Command{
78-
Use: "create",
79-
Short: fmt.Sprintf("Create a %v", strings.ToLower(r.Singular)),
80-
Run: func(cmd *cobra.Command, args []string) {
81-
id := args[0]
82-
p := withPrefix(fmt.Sprintf("?id=%s", id))
83-
jsonBody, err := generateJsonPayload(cmd, createArgs)
84-
if err != nil {
85-
slog.Error(fmt.Sprintf("unable to create json body for update: %v", err))
86-
}
87-
req, err = http.NewRequest("POST", p, strings.NewReader(string(jsonBody)))
88-
if err != nil {
89-
slog.Error(fmt.Sprintf("error creating post request: %v", err))
90-
}
91-
},
76+
if r.CreateMethod != nil {
77+
createArgs := map[string]interface{}{}
78+
createCmd := &cobra.Command{
79+
Use: "create",
80+
Short: fmt.Sprintf("Create a %v", strings.ToLower(r.Singular)),
81+
Run: func(cmd *cobra.Command, args []string) {
82+
id := args[0]
83+
p := withPrefix(fmt.Sprintf("?id=%s", id))
84+
jsonBody, err := generateJsonPayload(cmd, createArgs)
85+
if err != nil {
86+
slog.Error(fmt.Sprintf("unable to create json body for update: %v", err))
87+
}
88+
req, err = http.NewRequest("POST", p, strings.NewReader(string(jsonBody)))
89+
if err != nil {
90+
slog.Error(fmt.Sprintf("error creating post request: %v", err))
91+
}
92+
},
93+
}
94+
addSchemaFlags(createCmd, *r.Schema, createArgs)
95+
c.AddCommand(createCmd)
9296
}
93-
addSchemaFlags(createCmd, *r.Schema, createArgs)
94-
95-
getCmd := &cobra.Command{
96-
Use: "get",
97-
Short: fmt.Sprintf("Get a %v", strings.ToLower(r.Singular)),
98-
Run: func(cmd *cobra.Command, args []string) {
99-
id := args[0]
100-
p := withPrefix(fmt.Sprintf("/%s", id))
101-
req, err = http.NewRequest("GET", p, nil)
102-
},
97+
98+
if r.GetMethod != nil {
99+
getCmd := &cobra.Command{
100+
Use: "get",
101+
Short: fmt.Sprintf("Get a %v", strings.ToLower(r.Singular)),
102+
Run: func(cmd *cobra.Command, args []string) {
103+
id := args[0]
104+
p := withPrefix(fmt.Sprintf("/%s", id))
105+
req, err = http.NewRequest("GET", p, nil)
106+
},
107+
}
108+
c.AddCommand(getCmd)
103109
}
104110

105-
updateArgs := map[string]interface{}{}
106-
updateCmd := &cobra.Command{
107-
Use: "update",
108-
Short: fmt.Sprintf("Update a %v", strings.ToLower(r.Singular)),
109-
Run: func(cmd *cobra.Command, args []string) {
110-
id := args[0]
111-
p := withPrefix(fmt.Sprintf("/%s", id))
112-
jsonBody, err := generateJsonPayload(cmd, updateArgs)
113-
if err != nil {
114-
slog.Error(fmt.Sprintf("unable to create json body for update: %v", err))
115-
}
116-
req, err = http.NewRequest("PATCH", p, strings.NewReader(string(jsonBody)))
117-
if err != nil {
118-
slog.Error(fmt.Sprintf("error creating patch request: %v", err))
119-
}
120-
},
111+
if r.UpdateMethod != nil {
112+
113+
updateArgs := map[string]interface{}{}
114+
updateCmd := &cobra.Command{
115+
Use: "update",
116+
Short: fmt.Sprintf("Update a %v", strings.ToLower(r.Singular)),
117+
Run: func(cmd *cobra.Command, args []string) {
118+
id := args[0]
119+
p := withPrefix(fmt.Sprintf("/%s", id))
120+
jsonBody, err := generateJsonPayload(cmd, updateArgs)
121+
if err != nil {
122+
slog.Error(fmt.Sprintf("unable to create json body for update: %v", err))
123+
}
124+
req, err = http.NewRequest("PATCH", p, strings.NewReader(string(jsonBody)))
125+
if err != nil {
126+
slog.Error(fmt.Sprintf("error creating patch request: %v", err))
127+
}
128+
},
129+
}
130+
addSchemaFlags(updateCmd, *r.Schema, updateArgs)
131+
c.AddCommand(updateCmd)
121132
}
122-
addSchemaFlags(updateCmd, *r.Schema, updateArgs)
123-
124-
deleteCmd := &cobra.Command{
125-
Use: "delete",
126-
Short: fmt.Sprintf("Delete a %v", strings.ToLower(r.Singular)),
127-
Run: func(cmd *cobra.Command, args []string) {
128-
id := args[0]
129-
p := withPrefix(fmt.Sprintf("/%s", id))
130-
req, err = http.NewRequest("DELETE", p, nil)
131-
},
133+
134+
if r.DeleteMethod != nil {
135+
136+
deleteCmd := &cobra.Command{
137+
Use: "delete",
138+
Short: fmt.Sprintf("Delete a %v", strings.ToLower(r.Singular)),
139+
Run: func(cmd *cobra.Command, args []string) {
140+
id := args[0]
141+
p := withPrefix(fmt.Sprintf("/%s", id))
142+
req, err = http.NewRequest("DELETE", p, nil)
143+
},
144+
}
145+
c.AddCommand(deleteCmd)
132146
}
133147

134-
listCmd := &cobra.Command{
135-
Use: "list",
136-
Short: fmt.Sprintf("List %v", strings.ToLower(r.Plural)),
137-
Run: func(cmd *cobra.Command, args []string) {
138-
p := withPrefix("")
139-
req, err = http.NewRequest("GET", p, nil)
140-
},
148+
if r.ListMethod != nil {
149+
150+
listCmd := &cobra.Command{
151+
Use: "list",
152+
Short: fmt.Sprintf("List %v", strings.ToLower(r.Singular)),
153+
Run: func(cmd *cobra.Command, args []string) {
154+
p := withPrefix("")
155+
req, err = http.NewRequest("GET", p, nil)
156+
},
157+
}
158+
c.AddCommand(listCmd)
141159
}
142160

143-
c.AddCommand(createCmd, getCmd, updateCmd, deleteCmd, listCmd)
144161
c.SetArgs(args)
145162
if err := c.Execute(); err != nil {
146163
return nil, err

0 commit comments

Comments
 (0)