Skip to content

Commit 3c0bd52

Browse files
committed
fix: prettify JSON output in --json mode
- Apply pretty.Pretty() to --json output for indentation - Only skip colorization, not formatting - Update flag description to clarify behavior - Remove unused prettyPrint helper function Refs: beans-9w6d
1 parent 0aa2fb7 commit 3c0bd52

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
# beans-9w6d
3+
title: Prettify JSON output in --json mode
4+
status: completed
5+
type: bug
6+
created_at: 2025-12-27T21:36:52Z
7+
updated_at: 2025-12-27T21:36:52Z
8+
---
9+
10+
The `beans query --json` output was compact/raw JSON, but it should still be prettified (indented) - just without colors. This makes the output more readable when piping to tools like `jq` or saving to files, while still being valid for machine parsing.

cmd/graphql.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ Examples:
100100
return err
101101
}
102102

103-
// Output
103+
// Output (both modes are prettified, but --json skips color)
104104
if queryJSON {
105-
fmt.Println(string(result))
105+
fmt.Println(string(pretty.Pretty(result)))
106106
} else {
107-
prettyPrint(result)
107+
fmt.Println(string(pretty.Color(pretty.Pretty(result), nil)))
108108
}
109109

110110
return nil
@@ -181,11 +181,6 @@ func formatGraphQLErrors(errs gqlerror.List) error {
181181
return fmt.Errorf("graphql errors:\n %s", strings.Join(msgs, "\n "))
182182
}
183183

184-
// prettyPrint outputs the JSON with colors and indentation.
185-
func prettyPrint(data []byte) {
186-
fmt.Println(string(pretty.Color(pretty.Pretty(data), nil)))
187-
}
188-
189184
// printSchema outputs the GraphQL schema.
190185
func printSchema() error {
191186
fmt.Print(GetGraphQLSchema())
@@ -207,7 +202,7 @@ func GetGraphQLSchema() string {
207202
}
208203

209204
func init() {
210-
graphqlCmd.Flags().BoolVar(&queryJSON, "json", false, "Output raw JSON (no formatting)")
205+
graphqlCmd.Flags().BoolVar(&queryJSON, "json", false, "Output JSON without colors (for piping)")
211206
graphqlCmd.Flags().StringVarP(&queryVariables, "variables", "v", "", "Query variables as JSON string")
212207
graphqlCmd.Flags().StringVarP(&queryOperation, "operation", "o", "", "Operation name (for multi-operation documents)")
213208
graphqlCmd.Flags().BoolVar(&querySchemaOnly, "schema", false, "Print the GraphQL schema and exit")

0 commit comments

Comments
 (0)