Skip to content

Commit 84cff6e

Browse files
committed
feat(api): make verb optional
Add verb switching and documentation showing how to use the API without a verb.
1 parent e701ee7 commit 84cff6e

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

cmd/gh-slack/cmd/api.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
var apiCmd = &cobra.Command{
15-
Use: "api verb path",
15+
Use: "api [verb] path",
1616
Short: "Send an API call to slack",
1717
Long: "Send an API call to slack",
1818
RunE: func(cmd *cobra.Command, args []string) error {
@@ -26,13 +26,6 @@ var apiCmd = &cobra.Command{
2626
return err
2727
}
2828

29-
if len(args) != 2 {
30-
return fmt.Errorf("Expected 2 arguments: verb and path, see help")
31-
}
32-
33-
verb := strings.ToUpper(args[0])
34-
path := args[1]
35-
3629
fields, err := cmd.Flags().GetStringArray("field")
3730
if err != nil {
3831
return err
@@ -53,6 +46,21 @@ var apiCmd = &cobra.Command{
5346
return err
5447
}
5548

49+
var verb, path string
50+
if len(args) == 2 {
51+
verb = strings.ToUpper(args[0])
52+
path = args[1]
53+
} else if len(args) == 1 {
54+
path = args[0]
55+
if body == "" {
56+
verb = "GET"
57+
} else {
58+
verb = "POST"
59+
}
60+
} else {
61+
return fmt.Errorf("Expected 1 or 2 arguments: verb and/or path, see help")
62+
}
63+
5664
response, err := client.API(verb, path, mappedFields, body)
5765
if err != nil {
5866
return err
@@ -70,7 +78,7 @@ var body string
7078

7179
func init() {
7280
apiCmd.Flags().StringArrayVarP(&fields, "field", "f", nil, "Fields to pass to the api call")
73-
apiCmd.Flags().StringVarP(&body, "body", "b", "{}", "Body to send as JSON")
81+
apiCmd.Flags().StringVarP(&body, "body", "b", "", "Body to send as JSON")
7482
apiCmd.Flags().StringP("team", "t", "", "Slack team name (required here or in config)")
7583
apiCmd.SetHelpTemplate(apiCmdUsage)
7684
apiCmd.SetUsageTemplate(apiCmdUsage)
@@ -99,7 +107,12 @@ Aliases:
99107
{{.NameAndAliases}}{{end}}{{if .HasExample}}
100108
101109
Examples:
102-
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}{{$cmds := .Commands}}{{if eq (len .Groups) 0}}
110+
{{.Example}}
111+
112+
The verb is optional:
113+
- If no body is sent, GET will be used.
114+
- If a body is sent, POST will be used.
115+
{{end}}{{if .HasAvailableSubCommands}}{{$cmds := .Commands}}{{if eq (len .Groups) 0}}
103116
104117
Available Commands:{{range $cmds}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
105118
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{else}}{{range $group := .Groups}}

0 commit comments

Comments
 (0)