From e407315fc4ff27cd337875443bd38d8486ff632f Mon Sep 17 00:00:00 2001 From: Aditya030207 <230317630+Aditya030207@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:04:56 +0530 Subject: [PATCH 1/2] Add opt-in pretty formatting for jq output --- README.md | 2 +- internal/cmd/api.go | 8 ++++---- internal/cmd/cmd_test.go | 12 ++++++++++++ internal/cmd/environment.go | 4 +++- internal/cmd/root.go | 5 ++++- internal/output/output.go | 23 ++++++++++++++--------- internal/output/output_test.go | 14 ++++++++++++++ 7 files changed, 52 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 6df6dc9..712d524 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,6 @@ Reading works against any Flagsmith instance. Changing flags — `flag update`, ## Conventions -- `--json` (or `FLAGSMITH_JSON_OUTPUT`) for machine-readable output; `--jq ` to filter it. +- `--json` (or `FLAGSMITH_JSON_OUTPUT`) for machine-readable output; `--jq ` to filter it. Add `--jq-pretty` to indent filtered JSON when reading or saving it. - Static credentials: `FLAGSMITH_API_KEY` (Admin API), `FLAGSMITH_ENVIRONMENT_KEY` (SDK). When self-hosting Flagsmith, append the host and port, doubling `-` and writing `.` and `:` as `_`: `https://flagsmith-staging.com:8000` reads `FLAGSMITH_API_KEY_flagsmith__staging_com_8000`. - Self-hosted: `--api-url` or `FLAGSMITH_API_URL`. diff --git a/internal/cmd/api.go b/internal/cmd/api.go index d108563..c333937 100644 --- a/internal/cmd/api.go +++ b/internal/cmd/api.go @@ -90,7 +90,7 @@ func runAPI(cmd *cobra.Command, args []string) error { if apiIncludeFlag { writeStatusLine(out, resp) } - return writeBody(out, respBody, jqFlag) + return writeBody(out, respBody, outputOpts()) } // apiTarget resolves the base URL and the auth applier for the chosen surface. @@ -266,8 +266,8 @@ func writeStatusLine(w io.Writer, resp *http.Response) { // writeBody writes the response verbatim, or filters it through jq. Raw // passthrough preserves the API's own formatting; --jq requires JSON. -func writeBody(w io.Writer, body []byte, jqExpr string) error { - if jqExpr == "" { +func writeBody(w io.Writer, body []byte, opts output.Options) error { + if opts.JQ == "" { _, err := w.Write(body) return err } @@ -275,7 +275,7 @@ func writeBody(w io.Writer, body []byte, jqExpr string) error { if err := json.Unmarshal(body, &v); err != nil { return fmt.Errorf("response is not JSON, cannot apply --jq: %w", err) } - return output.Render(w, v, output.Options{JQ: jqExpr}, nil) + return output.Render(w, v, opts, nil) } func init() { diff --git a/internal/cmd/cmd_test.go b/internal/cmd/cmd_test.go index 1777c36..d0b8155 100644 --- a/internal/cmd/cmd_test.go +++ b/internal/cmd/cmd_test.go @@ -8173,6 +8173,18 @@ func TestAPI(t *testing.T) { } }) + t.Run("--jq-pretty indents filtered API output", func(t *testing.T) { + flagUpdateEnv(t) + out, err := run("", "api", "api/v1/organisations/", "--jq", "{names: [.results[].name]}", "--jq-pretty") + if err != nil { + t.Fatalf("api: %v", err) + } + want := "{\n \"names\": [\n \"Acme\"\n ]\n}\n" + if out != want { + t.Errorf("out = %q, want %q", out, want) + } + }) + t.Run("a field implies POST with a typed JSON body", func(t *testing.T) { flagUpdateEnv(t) e := echoJSON(t, "", "-F", "n=3", "-f", "s=3") diff --git a/internal/cmd/environment.go b/internal/cmd/environment.go index 42dcb0e..86115a2 100644 --- a/internal/cmd/environment.go +++ b/internal/cmd/environment.go @@ -258,7 +258,9 @@ var environmentDocumentCmd = &cobra.Command{ return err } // A document is always JSON; --jq composes. - return output.Render(cmd.OutOrStdout(), parsed, output.Options{JSON: true, JQ: jqFlag}, nil) + opts := outputOpts() + opts.JSON = true + return output.Render(cmd.OutOrStdout(), parsed, opts, nil) }, } diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 583f9b1..3ae5e11 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -94,6 +94,7 @@ var ( noInputFlag bool jsonFlag bool jqFlag string + jqPrettyFlag bool ) // jsonOutput reports whether machine-readable output was requested. @@ -103,7 +104,7 @@ func jsonOutput() bool { // outputOpts is the render format for the current invocation. func outputOpts() output.Options { - return output.Options{JSON: jsonOutput(), JQ: jqFlag} + return output.Options{JSON: jsonOutput(), JQ: jqFlag, JQPretty: jqPrettyFlag} } // usageError is a missing/invalid input a prompt would have collected @@ -264,6 +265,8 @@ func init() { "output JSON instead of human-readable text (env: FLAGSMITH_JSON_OUTPUT)") flags.StringVar(&jqFlag, "jq", "", "filter JSON output through a jq expression (implies --json)") + flags.BoolVar(&jqPrettyFlag, "jq-pretty", false, + "indent JSON results from --jq") rootCmd.SetUsageTemplate(singleLineUsage(rootCmd.UsageTemplate())) diff --git a/internal/output/output.go b/internal/output/output.go index 40e7270..6f5c2d7 100644 --- a/internal/output/output.go +++ b/internal/output/output.go @@ -15,8 +15,9 @@ import ( // Options selects the output format for a single command invocation. type Options struct { - JSON bool // emit JSON instead of a human view - JQ string // filter JSON through this jq expression (implies JSON) + JSON bool // emit JSON instead of a human view + JQ string // filter JSON through this jq expression (implies JSON) + JQPretty bool // indent structured jq results } func (o Options) jsonMode() bool { return o.JSON || o.JQ != "" } @@ -66,7 +67,7 @@ func Render(w io.Writer, data any, opts Options, human func(io.Writer) error) er return human(w) } if opts.JQ != "" { - return renderJQ(w, data, opts.JQ) + return renderJQ(w, data, opts.JQ, opts.JQPretty) } encoded, err := json.MarshalIndent(data, "", " ") if err != nil { @@ -77,8 +78,8 @@ func Render(w io.Writer, data any, opts Options, human func(io.Writer) error) er } // renderJQ runs a jq expression over data and prints each result: raw for -// strings, compact JSON otherwise (matching `gh --jq`). -func renderJQ(w io.Writer, data any, expr string) error { +// strings, JSON otherwise. JSON is compact by default (matching `gh --jq`). +func renderJQ(w io.Writer, data any, expr string, pretty bool) error { query, err := gojq.Parse(expr) if err != nil { return fmt.Errorf("invalid jq expression: %w", err) @@ -93,6 +94,10 @@ func renderJQ(w io.Writer, data any, expr string) error { return err } iter := query.Run(input) + encoder := json.NewEncoder(w) + if pretty { + encoder.SetIndent("", " ") + } for { v, ok := iter.Next() if !ok { @@ -102,14 +107,14 @@ func renderJQ(w io.Writer, data any, expr string) error { return err } if s, ok := v.(string); ok { - fmt.Fprintln(w, s) + if _, err := fmt.Fprintln(w, s); err != nil { + return err + } continue } - encoded, err := json.Marshal(v) - if err != nil { + if err := encoder.Encode(v); err != nil { return err } - fmt.Fprintln(w, string(encoded)) } return nil } diff --git a/internal/output/output_test.go b/internal/output/output_test.go index a28a632..7fee9e7 100644 --- a/internal/output/output_test.go +++ b/internal/output/output_test.go @@ -131,6 +131,20 @@ func TestRenderJQImpliesJSONAndFilters(t *testing.T) { } }) + t.Run("pretty output indents objects and keeps strings raw", func(t *testing.T) { + var b bytes.Buffer + err := Render(&b, sample{7, "beta"}, Options{ + JQ: "{n: [.id]}, .name", JQPretty: true, + }, nil) + if err != nil { + t.Fatal(err) + } + want := "{\n \"n\": [\n 7\n ]\n}\nbeta\n" + if got := b.String(); got != want { + t.Errorf("output = %q, want %q", got, want) + } + }) + t.Run("invalid expression errors", func(t *testing.T) { // Given var b bytes.Buffer From 33381fff2b2fcc19ac6eb1e1595e33489e2e3965 Mon Sep 17 00:00:00 2001 From: Aditya030207 <230317630+Aditya030207@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:14:55 +0530 Subject: [PATCH 2/2] test: label jq pretty-print test sections --- internal/cmd/cmd_test.go | 5 +++++ internal/output/output_test.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/internal/cmd/cmd_test.go b/internal/cmd/cmd_test.go index d0b8155..7f38c9c 100644 --- a/internal/cmd/cmd_test.go +++ b/internal/cmd/cmd_test.go @@ -8174,8 +8174,13 @@ func TestAPI(t *testing.T) { }) t.Run("--jq-pretty indents filtered API output", func(t *testing.T) { + // Given flagUpdateEnv(t) + + // When out, err := run("", "api", "api/v1/organisations/", "--jq", "{names: [.results[].name]}", "--jq-pretty") + + // Then if err != nil { t.Fatalf("api: %v", err) } diff --git a/internal/output/output_test.go b/internal/output/output_test.go index 7fee9e7..f609a33 100644 --- a/internal/output/output_test.go +++ b/internal/output/output_test.go @@ -132,10 +132,15 @@ func TestRenderJQImpliesJSONAndFilters(t *testing.T) { }) t.Run("pretty output indents objects and keeps strings raw", func(t *testing.T) { + // Given var b bytes.Buffer + + // When err := Render(&b, sample{7, "beta"}, Options{ JQ: "{n: [.id]}, .name", JQPretty: true, }, nil) + + // Then if err != nil { t.Fatal(err) }