feat: add --format json to search cmd - #657
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #657 +/- ##
==========================================
- Coverage 82.40% 2.12% -80.28%
==========================================
Files 10 135 +125
Lines 358 30170 +29812
==========================================
+ Hits 295 642 +347
- Misses 43 29450 +29407
- Partials 20 78 +58 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
f7d6f44 to
5fae30d
Compare
5fae30d to
cc604c3
Compare
✅ Deploy Preview for archivista-dev ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
Pull request overview
Adds a --format flag to archivistactl search so callers can request machine-readable JSON output for scripting, addressing Issue #399.
Changes:
- Introduces
--format(with-f) onsearchto switch between human-readable output and JSON. - Implements JSON serialization of GraphQL search results when
--format jsonis selected.
Comments suppressed due to low confidence (2)
cmd/archivistactl/cmd/search.go:75
- The default output isn't actually a table; it's a human-readable text format. Using "table" in the default value/help string is misleading for users and makes the validation message less clear.
searchCmd.Flags().StringVarP(&format, "format", "f", "table", "Output format (table|json)")
cmd/archivistactl/cmd/search.go:64
- The new JSON output path isn't covered by tests. Since this repo already has unit/e2e tests for archivistactl commands, add a test that runs
search --format jsonand asserts the output is valid JSON (and matches expected fields) to prevent regressions.
if format == "json" {
jsonData, err := json.MarshalIndent(results, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal results to JSON: %w", err)
}
fmt.Println(string(jsonData))
return nil
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
aab812b to
fdf72b8
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
cmd/archivistactl/cmd/search.go:68
- New
--formatbehavior (validation + JSON output branch) isn’t covered by tests. Since this file already has unit tests, please add coverage for at least the unsupported format case (which should fail before any network call) and an integration/e2e assertion that--format jsonoutputs valid JSON when the backend is available.
if format != "table" && format != "json" {
return fmt.Errorf("unsupported format %q: supported formats are table, json", format)
}
algo, digest, err := validateDigestString(args[0])
if err != nil {
return err
}
results, err := api.GraphQlQuery[searchResults](cmd.Context(), archivistaUrl, searchQuery, searchVars{Algorithm: algo, Digest: digest}, requestOptions()...)
if err != nil {
return err
}
if format == "json" {
jsonData, err := json.MarshalIndent(results, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal results to JSON: %w", err)
}
fmt.Fprintln(cmd.OutOrStdout(), string(jsonData))
return nil
}
| if format == "json" { | ||
| jsonData, err := json.MarshalIndent(results, "", " ") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal results to JSON: %w", err) | ||
| } | ||
| fmt.Fprintln(cmd.OutOrStdout(), string(jsonData)) | ||
| return nil | ||
| } |
Signed-off-by: Rahul Vishwakarma <rahulvs2809@gmail.com>
Signed-off-by: Rahul Vishwakarma <rahulvs2809@gmail.com>
Signed-off-by: Rahul Vishwakarma <rahulvs2809@gmail.com>
Signed-off-by: Rahul Vishwakarma <rahulvs2809@gmail.com>
095c65f to
387f597
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
Suppressed comments (2)
cmd/archivistactl/cmd/search.go:65
- The JSON output path allocates an intermediate string and doesn’t stream directly to the configured command output writer. Using json.Encoder keeps the output handling consistent and avoids an extra allocation while still producing indented JSON.
if format == "json" {
jsonData, err := json.MarshalIndent(results, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal results to JSON: %w", err)
}
cmd/archivistactl/cmd/search_test.go:64
- This test sets the global
formatflag value to "yaml" via cobra flag parsing, and that value can leak into later tests that execute the CLI in the same process. Restoring the previous value (or resetting to the default) makes the test suite order-independent.
func (ut *UTSearchSuite) Test_SearchInvalidFormat() {
output := bytes.NewBufferString("")
rootCmd.SetOut(output)
rootCmd.SetErr(output)
rootCmd.SetArgs([]string{"search", "sha256:test", "--format", "yaml"})
What this PR does / why we need it
Description
Added the format flag to the search command to get the output in JSON format
Which issue(s) this PR fixes (optional)
Fixes #399
Acceptance Criteria Met
Special notes for your reviewer: