Skip to content

feat: add --format json to search cmd - #657

Open
manzil-infinity180 wants to merge 4 commits into
in-toto:mainfrom
manzil-infinity180:feat/format-search-output
Open

feat: add --format json to search cmd#657
manzil-infinity180 wants to merge 4 commits into
in-toto:mainfrom
manzil-infinity180:feat/format-search-output

Conversation

@manzil-infinity180

Copy link
Copy Markdown
Contributor

What this PR does / why we need it

Description
Added the format flag to the search command to get the output in JSON format

./archivistactl search sha256:423da4cff198bbffbe3220ed9510d32ba96698e4b1f654552521d1f541abb6dc
Gitoid: 4462a729251af54c7699dbca2f7d5bf5759a5fc6273b3cd606da29d531387c86
Collection name: build
Attestations: https://witness.dev/attestations/command-run/v0.1, https://witness.dev/attestations/environment/v0.1, https://witness.dev/attestations/git/v0.1, https://witness.dev/attestations/material/v0.1, https://witness.dev/attestations/product/v0.1
archivistactl (feat/format-search-output) $ ./archivistactl search sha256:423da4cff198bbffbe3220ed9510d32ba96698e4b1f654552521d1f541abb6dc --format json
{
  "dsses": {
    "edges": [
      {
        "node": {
          "gitoidSha256": "4462a729251af54c7699dbca2f7d5bf5759a5fc6273b3cd606da29d531387c86",
          "statement": {
            "attestationCollections": {
              "name": "build",
              "attestations": [
                {
                  "type": "https://witness.dev/attestations/command-run/v0.1"
                },
                {
                  "type": "https://witness.dev/attestations/environment/v0.1"
                },
                {
                  "type": "https://witness.dev/attestations/git/v0.1"
                },
                {
                  "type": "https://witness.dev/attestations/material/v0.1"
                },
                {
                  "type": "https://witness.dev/attestations/product/v0.1"
                }
              ]
            }
          }
        }
      }
    ]
  }
}

Which issue(s) this PR fixes (optional)

Fixes #399

Acceptance Criteria Met

  • Docs changes if needed
  • Testing changes if needed
  • All workflow checks passing (automatically enforced)
  • All review conversations resolved (automatically enforced)
  • DCO Sign-off

Special notes for your reviewer:

@codecov

codecov Bot commented Nov 1, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 14.28571% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 2.12%. Comparing base (a035c62) to head (cc604c3).
⚠️ Report is 491 commits behind head on main.

Files with missing lines Patch % Lines
cmd/archivistactl/cmd/search.go 14.28% 5 Missing and 1 partial ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jkjell
jkjell force-pushed the feat/format-search-output branch from f7d6f44 to 5fae30d Compare May 12, 2026 15:11
@jkjell
jkjell force-pushed the feat/format-search-output branch from 5fae30d to cc604c3 Compare May 20, 2026 21:38
@netlify

netlify Bot commented Jul 5, 2026

Copy link
Copy Markdown

Deploy Preview for archivista-dev ready!

Name Link
🔨 Latest commit 387f597
🔍 Latest deploy log https://app.netlify.com/projects/archivista-dev/deploys/6a73963be2a3410008a1ee4a
😎 Deploy Preview https://deploy-preview-657--archivista-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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) on search to switch between human-readable output and JSON.
  • Implements JSON serialization of GraphQL search results when --format json is 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 json and 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.

Comment thread cmd/archivistactl/cmd/search.go

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread cmd/archivistactl/cmd/search.go

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 --format behavior (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 json outputs 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
		}

Comment on lines +61 to +68
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>
@jkjell
jkjell force-pushed the feat/format-search-output branch from 095c65f to 387f597 Compare August 5, 2026 19:59
@jkjell
jkjell requested a lite review from Copilot August 7, 2026 01:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 format flag 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"})

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.

[Feat]: Different output format for cli search

2 participants