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

@ -8,9 +8,9 @@ import (
"text/tabwriter"
"code.gitea.io/sdk/gitea"
"github.com/spf13/cobra"
"codeberg.org/romaintb/fgj/internal/api"
"codeberg.org/romaintb/fgj/internal/config"
"github.com/spf13/cobra"
)
var prCmd = &cobra.Command{
@ -59,8 +59,10 @@ func init() {
prListCmd.Flags().StringP("repo", "R", "", "Repository in owner/name format")
prListCmd.Flags().StringP("state", "s", "open", "Filter by state: open, closed, all")
prListCmd.Flags().Bool("json", false, "Output pull requests as JSON")
prViewCmd.Flags().StringP("repo", "R", "", "Repository in owner/name format")
prViewCmd.Flags().Bool("json", false, "Output pull request as JSON")
prCreateCmd.Flags().StringP("repo", "R", "", "Repository in owner/name format")
prCreateCmd.Flags().StringP("title", "t", "", "Title for the pull request")
@ -111,6 +113,10 @@ func runPRList(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to list pull requests: %w", err)
}
if jsonOutput, _ := cmd.Flags().GetBool("json"); jsonOutput {
return writeJSON(prs)
}
if len(prs) == 0 {
fmt.Printf("No %s pull requests in %s/%s\n", state, owner, name)
return nil
@ -153,6 +159,10 @@ func runPRView(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to get pull request: %w", err)
}
if jsonOutput, _ := cmd.Flags().GetBool("json"); jsonOutput {
return writeJSON(pr)
}
fmt.Printf("Pull Request #%d\n", pr.Index)
fmt.Printf("Title: %s\n", pr.Title)
fmt.Printf("State: %s\n", pr.State)
@ -279,4 +289,3 @@ func runPRMerge(cmd *cobra.Command, args []string) error {
return nil
}