feat: add json output for list and view commands

This commit is contained in:
Romain Bertrand 2026-01-18 11:48:08 +01:00
parent fe23f2fce3
commit 3ccef4e1c6
5 changed files with 192 additions and 52 deletions

View file

@ -76,8 +76,10 @@ func init() {
issueListCmd.Flags().StringP("repo", "R", "", "Repository in owner/name format")
issueListCmd.Flags().StringP("state", "s", "open", "Filter by state: open, closed, all")
issueListCmd.Flags().Bool("json", false, "Output issues as JSON")
issueViewCmd.Flags().StringP("repo", "R", "", "Repository in owner/name format")
issueViewCmd.Flags().Bool("json", false, "Output issue as JSON")
issueCreateCmd.Flags().StringP("repo", "R", "", "Repository in owner/name format")
issueCreateCmd.Flags().StringP("title", "t", "", "Title for the issue")
@ -133,17 +135,26 @@ func runIssueList(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to list issues: %w", err)
}
if len(issues) == 0 {
nonPRIssues := make([]*gitea.Issue, 0, len(issues))
for _, issue := range issues {
if issue.PullRequest == nil {
nonPRIssues = append(nonPRIssues, issue)
}
}
if jsonOutput, _ := cmd.Flags().GetBool("json"); jsonOutput {
return writeJSON(nonPRIssues)
}
if len(nonPRIssues) == 0 {
fmt.Printf("No %s issues in %s/%s\n", state, owner, name)
return nil
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
_, _ = fmt.Fprintf(w, "NUMBER\tTITLE\tSTATE\n")
for _, issue := range issues {
if issue.PullRequest == nil {
_, _ = fmt.Fprintf(w, "#%d\t%s\t%s\n", issue.Index, issue.Title, issue.State)
}
for _, issue := range nonPRIssues {
_, _ = fmt.Fprintf(w, "#%d\t%s\t%s\n", issue.Index, issue.Title, issue.State)
}
_ = w.Flush()
@ -177,6 +188,23 @@ func runIssueView(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to get issue: %w", err)
}
var comments []*gitea.Comment
comments, _, err = client.ListIssueComments(owner, name, issueNumber, gitea.ListIssueCommentOptions{})
if err != nil {
comments = nil
}
if jsonOutput, _ := cmd.Flags().GetBool("json"); jsonOutput {
payload := struct {
Issue *gitea.Issue `json:"issue"`
Comments []*gitea.Comment `json:"comments,omitempty"`
}{
Issue: issue,
Comments: comments,
}
return writeJSON(payload)
}
fmt.Printf("Issue #%d\n", issue.Index)
fmt.Printf("Title: %s\n", issue.Title)
fmt.Printf("State: %s\n", issue.State)
@ -187,8 +215,7 @@ func runIssueView(cmd *cobra.Command, args []string) error {
fmt.Printf("\n%s\n", issue.Body)
}
comments, _, err := client.ListIssueComments(owner, name, issueNumber, gitea.ListIssueCommentOptions{})
if err == nil && len(comments) > 0 {
if len(comments) > 0 {
fmt.Printf("\nComments (%d):\n", len(comments))
for _, comment := range comments {
fmt.Printf("\n---\n%s (@%s) - %s\n%s\n",