-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add --output json flag for machine-readable output #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
59bc757
feat: add --output json flag for machine-readable output
rgarcia 7070bc5
fix: update test files to pass required input structs to List methods
rgarcia 52b5c9b
fix: handle JSON mode properly for error cases
rgarcia c5a11b5
fix: address remaining cursor[bot] review feedback
rgarcia 362c55a
fix: address Sayan's review feedback
rgarcia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
|
|
@@ -44,9 +45,11 @@ func init() { | |
| appListCmd.Flags().Int("limit", 20, "Max apps to return (default 20)") | ||
| appListCmd.Flags().Int("per-page", 20, "Items per page (alias of --limit)") | ||
| appListCmd.Flags().Int("page", 1, "Page number (1-based)") | ||
| appListCmd.Flags().StringP("output", "o", "", "Output format: json for raw API response") | ||
|
|
||
| // Limit rows returned for app history (0 = all) | ||
| appHistoryCmd.Flags().Int("limit", 20, "Max deployments to return (default 20)") | ||
| appHistoryCmd.Flags().StringP("output", "o", "", "Output format: json for raw API response") | ||
| } | ||
|
|
||
| func runAppList(cmd *cobra.Command, args []string) error { | ||
|
|
@@ -56,6 +59,12 @@ func runAppList(cmd *cobra.Command, args []string) error { | |
| lim, _ := cmd.Flags().GetInt("limit") | ||
| perPage, _ := cmd.Flags().GetInt("per-page") | ||
| page, _ := cmd.Flags().GetInt("page") | ||
| output, _ := cmd.Flags().GetString("output") | ||
|
|
||
| if output != "" && output != "json" { | ||
| pterm.Error.Println("unsupported --output value: use 'json'") | ||
| return nil | ||
|
||
| } | ||
|
|
||
| // Determine pagination inputs: prefer page/per-page if provided; else map legacy --limit | ||
| usePager := cmd.Flags().Changed("per-page") || cmd.Flags().Changed("page") | ||
|
|
@@ -73,7 +82,9 @@ func runAppList(cmd *cobra.Command, args []string) error { | |
| page = 1 | ||
| } | ||
|
|
||
| pterm.Debug.Println("Fetching deployed applications...") | ||
| if output != "json" { | ||
| pterm.Debug.Println("Fetching deployed applications...") | ||
| } | ||
|
|
||
| params := kernel.AppListParams{} | ||
| if appName != "" { | ||
|
|
@@ -92,6 +103,19 @@ func runAppList(cmd *cobra.Command, args []string) error { | |
| return nil | ||
| } | ||
|
|
||
| if output == "json" { | ||
| if apps == nil || len(apps.Items) == 0 { | ||
| fmt.Println("[]") | ||
| return nil | ||
| } | ||
| bs, err := json.MarshalIndent(apps.Items, "", " ") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| fmt.Println(string(bs)) | ||
| return nil | ||
| } | ||
|
|
||
| if apps == nil || len(apps.Items) == 0 { | ||
| pterm.Info.Println("No applications found") | ||
| return nil | ||
|
|
@@ -193,8 +217,16 @@ func runAppHistory(cmd *cobra.Command, args []string) error { | |
| client := getKernelClient(cmd) | ||
| appName := args[0] | ||
| lim, _ := cmd.Flags().GetInt("limit") | ||
| output, _ := cmd.Flags().GetString("output") | ||
|
|
||
| pterm.Debug.Printf("Fetching deployment history for app '%s'...\n", appName) | ||
| if output != "" && output != "json" { | ||
| pterm.Error.Println("unsupported --output value: use 'json'") | ||
| return nil | ||
| } | ||
|
|
||
| if output != "json" { | ||
| pterm.Debug.Printf("Fetching deployment history for app '%s'...\n", appName) | ||
| } | ||
|
|
||
| params := kernel.DeploymentListParams{} | ||
| if appName != "" { | ||
|
|
@@ -207,6 +239,19 @@ func runAppHistory(cmd *cobra.Command, args []string) error { | |
| return nil | ||
| } | ||
|
|
||
| if output == "json" { | ||
| if deployments == nil || len(deployments.Items) == 0 { | ||
| fmt.Println("[]") | ||
| return nil | ||
| } | ||
| bs, err := json.MarshalIndent(deployments.Items, "", " ") | ||
| if err != nil { | ||
| return err | ||
| } | ||
| fmt.Println(string(bs)) | ||
| return nil | ||
| } | ||
|
|
||
| if deployments == nil || len(deployments.Items) == 0 { | ||
| pterm.Info.Println("No deployments found for this application") | ||
| return nil | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning
nilhere exits 0, which is a bit surprising for an invalid flag value (especially if this is used in scripts). Consider returning an error so the CLI exits non-zero.