From 84cff6eabf4794cbfdafd312f3cd324bf8ecd473 Mon Sep 17 00:00:00 2001 From: nobe4 Date: Wed, 6 Dec 2023 19:51:37 +0100 Subject: [PATCH] feat(api): make verb optional Add verb switching and documentation showing how to use the API without a verb. --- cmd/gh-slack/cmd/api.go | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/cmd/gh-slack/cmd/api.go b/cmd/gh-slack/cmd/api.go index caa7830..37453b3 100644 --- a/cmd/gh-slack/cmd/api.go +++ b/cmd/gh-slack/cmd/api.go @@ -12,7 +12,7 @@ import ( ) var apiCmd = &cobra.Command{ - Use: "api verb path", + Use: "api [verb] path", Short: "Send an API call to slack", Long: "Send an API call to slack", RunE: func(cmd *cobra.Command, args []string) error { @@ -26,13 +26,6 @@ var apiCmd = &cobra.Command{ return err } - if len(args) != 2 { - return fmt.Errorf("Expected 2 arguments: verb and path, see help") - } - - verb := strings.ToUpper(args[0]) - path := args[1] - fields, err := cmd.Flags().GetStringArray("field") if err != nil { return err @@ -53,6 +46,21 @@ var apiCmd = &cobra.Command{ return err } + var verb, path string + if len(args) == 2 { + verb = strings.ToUpper(args[0]) + path = args[1] + } else if len(args) == 1 { + path = args[0] + if body == "" { + verb = "GET" + } else { + verb = "POST" + } + } else { + return fmt.Errorf("Expected 1 or 2 arguments: verb and/or path, see help") + } + response, err := client.API(verb, path, mappedFields, body) if err != nil { return err @@ -70,7 +78,7 @@ var body string func init() { apiCmd.Flags().StringArrayVarP(&fields, "field", "f", nil, "Fields to pass to the api call") - apiCmd.Flags().StringVarP(&body, "body", "b", "{}", "Body to send as JSON") + apiCmd.Flags().StringVarP(&body, "body", "b", "", "Body to send as JSON") apiCmd.Flags().StringP("team", "t", "", "Slack team name (required here or in config)") apiCmd.SetHelpTemplate(apiCmdUsage) apiCmd.SetUsageTemplate(apiCmdUsage) @@ -99,7 +107,12 @@ Aliases: {{.NameAndAliases}}{{end}}{{if .HasExample}} Examples: -{{.Example}}{{end}}{{if .HasAvailableSubCommands}}{{$cmds := .Commands}}{{if eq (len .Groups) 0}} +{{.Example}} + +The verb is optional: +- If no body is sent, GET will be used. +- If a body is sent, POST will be used. +{{end}}{{if .HasAvailableSubCommands}}{{$cmds := .Commands}}{{if eq (len .Groups) 0}} Available Commands:{{range $cmds}}{{if (or .IsAvailableCommand (eq .Name "help"))}} {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{else}}{{range $group := .Groups}}