Skip to content

feat: add --output=json to the profiles command#643

Open
fgrosse wants to merge 4 commits into
creativeprojects:masterfrom
fgrosse:fgrosse/profiles-json-output
Open

feat: add --output=json to the profiles command#643
fgrosse wants to merge 4 commits into
creativeprojects:masterfrom
fgrosse:fgrosse/profiles-json-output

Conversation

@fgrosse
Copy link
Copy Markdown
Contributor

@fgrosse fgrosse commented May 23, 2026

Description

I want to be able to retrieve all defined profiles and then run some scripts on them. Currently, I parse profile names using resticprofile profiles | awk ..., but I would like a more flexible, robust approach that supports structured output such as JSON and pipes the result into jq.

Summary

Disclaimer: The code was originally written by Claude and then I polished it myself until I was happy with the result. 🤖

  • Add --output=<format> to the profiles command (plain default, json available) so callers can pipe profile/group metadata into jq and friends.
  • JSON shape mirrors today's text output: each profile has name, description, sections; each group has name, description, profiles. Top-level object so it can grow non-breakingly later.
  • When a structured format is requested, diagnostic logs (e.g. "using configuration file: …") are routed to stderr so stdout stays valid JSON.

Test plan

  • go test -count=1 -short . passes
  • golangci-lint run ./... clean
  • resticprofile profiles output is unchanged (plain text path is byte-identical)
  • resticprofile profiles --output=json | jq '.profiles[].name' works end-to-end
  • resticprofile profiles --output json (space form) works
  • resticprofile profiles --output=yaml errors with unknown output format "yaml"
  • resticprofile help profiles lists the new flag

Render the profiles and groups listing as machine-readable JSON for
scripting, alongside the existing plain text output:

  resticprofile profiles --output=json | jq '.profiles[].name'

When a structured output format is requested, diagnostic log lines are
routed to stderr so stdout stays valid for parsers.
@codecov
Copy link
Copy Markdown

codecov Bot commented May 23, 2026

Codecov Report

❌ Patch coverage is 96.82540% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.33%. Comparing base (8b7bca2) to head (0da7a1d).

Files with missing lines Patch % Lines
commands_display.go 94.87% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #643      +/-   ##
==========================================
+ Coverage   80.99%   81.33%   +0.34%     
==========================================
  Files         162      163       +1     
  Lines       12113    12173      +60     
==========================================
+ Hits         9810     9900      +90     
+ Misses       1806     1772      -34     
- Partials      497      501       +4     
Flag Coverage Δ
unittests 79.61% <96.83%> (+0.35%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

fgrosse added 2 commits May 23, 2026 23:43
- Refresh doc comments on parseOutputFormat and wantsStructuredOutput
  after the validation moved into displayProfilesCommand
- Surface valid formats in the "unknown output format" error
- Document the --output=json mode in the usage guide
Verify that an empty configuration still emits "profiles": [] and
"groups": [] (not null), so scripts can always rely on the keys
holding arrays.
Comment thread commands.go
action: displayProfilesCommand,
needConfiguration: true,
flags: map[string]string{
"--output=<format>": "output format: plain (default) or json",
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that I wanted to mirror the well-known pattern from tools such as kubectl. The existing behavior is retained with the plain output format, which remains the default.

Inline the YAML fixture into the helper and tighten the invalid-format
assertion to match the full error message.
@fgrosse fgrosse marked this pull request as ready for review May 24, 2026 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant