From acaf888537b7da5296e75c5038584f770ff28b00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20B=2E?= Date: Mon, 16 Sep 2024 10:23:36 +0200 Subject: [PATCH] feat: Add option to output issue creation result as Json --- internal/cmd/issue/create/create.go | 36 +++++++++++++++++++---------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/internal/cmd/issue/create/create.go b/internal/cmd/issue/create/create.go index 5e418826..31d6e63f 100644 --- a/internal/cmd/issue/create/create.go +++ b/internal/cmd/issue/create/create.go @@ -1,12 +1,10 @@ package create import ( + "encoding/json" "fmt" "github.com/AlecAivazis/survey/v2" - "github.com/spf13/cobra" - "github.com/spf13/viper" - "github.com/ankitpokhrel/jira-cli/api" "github.com/ankitpokhrel/jira-cli/internal/cmdcommon" "github.com/ankitpokhrel/jira-cli/internal/cmdutil" @@ -14,6 +12,8 @@ import ( "github.com/ankitpokhrel/jira-cli/pkg/jira" "github.com/ankitpokhrel/jira-cli/pkg/surveyext" "github.com/ankitpokhrel/jira-cli/pkg/tui" + "github.com/spf13/cobra" + "github.com/spf13/viper" ) const ( @@ -42,17 +42,23 @@ $ echo "Description from stdin" | jira issue create -s"Summary" -tTask # For issue description, the flag --body/-b takes precedence over the --template flag # The example below will add "Body from flag" as an issue description $ jira issue create -tTask -sSummary -b"Body from flag" --template /path/to/template.tpl` + + flagJson = "json" ) // NewCmdCreate is a create command. func NewCmdCreate() *cobra.Command { - return &cobra.Command{ + cmd := cobra.Command{ Use: "create", Short: "Create an issue in a project", Long: helpText, Example: examples, Run: create, } + + cmd.Flags().Bool(flagJson, false, "Print output in JSON format") + + return &cmd } // SetFlags sets flags supported by create command. @@ -94,7 +100,7 @@ func create(cmd *cobra.Command, _ []string) { params.Reporter = cmdcommon.GetRelevantUser(client, project, params.Reporter) params.Assignee = cmdcommon.GetRelevantUser(client, project, params.Assignee) - key, err := func() (string, error) { + issue, err := func() (*jira.CreateResponse, error) { s := cmdutil.Info("Creating an issue...") defer s.Stop() @@ -126,18 +132,24 @@ func create(cmd *cobra.Command, _ []string) { cr.SubtaskField = handle } - resp, err := client.CreateV2(&cr) - if err != nil { - return "", err - } - return resp.Key, nil + return client.CreateV2(&cr) }() cmdutil.ExitIfError(err) - cmdutil.Success("Issue created\n%s", cmdutil.GenerateServerBrowseURL(server, key)) + + jsonFlag, err := cmd.Flags().GetBool(flagJson) + cmdutil.ExitIfError(err) + if jsonFlag { + jsonData, err := json.Marshal(issue) + cmdutil.ExitIfError(err) + fmt.Println(string(jsonData)) + return + } + + cmdutil.Success("Issue created\n%s", cmdutil.GenerateServerBrowseURL(server, issue.Key)) if web, _ := cmd.Flags().GetBool("web"); web { - err := cmdutil.Navigate(server, key) + err := cmdutil.Navigate(server, issue.Key) cmdutil.ExitIfError(err) } }