Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions cmd/archivistactl/cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package cmd

import (
"encoding/json"
"errors"
"fmt"
"strings"

"github.com/in-toto/archivista/pkg/api"
Expand All @@ -42,6 +44,10 @@ Digests are expected to be in the form algorithm:digest, for instance: sha256:45
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if format != "table" && format != "json" {
return fmt.Errorf("unsupported format %q: supported formats are table, json", format)
}
Comment thread
manzil-infinity180 marked this conversation as resolved.

algo, digest, err := validateDigestString(args[0])
if err != nil {
return err
Expand All @@ -52,13 +58,25 @@ Digests are expected to be in the form algorithm:digest, for instance: sha256:45
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
}

printResults(results)
return nil
},
}

var format string

func init() {
rootCmd.AddCommand(searchCmd)
searchCmd.Flags().StringVarP(&format, "format", "f", "table", "Output format (table|json)")
}

func validateDigestString(ds string) (algo, digest string, err error) {
Expand Down
13 changes: 13 additions & 0 deletions cmd/archivistactl/cmd/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ func (ut *UTSearchSuite) Test_SearchInvalidDigestString() {
}
}

func (ut *UTSearchSuite) Test_SearchInvalidFormat() {
output := bytes.NewBufferString("")
rootCmd.SetOut(output)
rootCmd.SetErr(output)
rootCmd.SetArgs([]string{"search", "sha256:test", "--format", "yaml"})
err := rootCmd.Execute()
if err != nil {
ut.ErrorContains(err, "unsupported format")
} else {
ut.FailNow("Expected: error")
}
}

func (ut *UTSearchSuite) Test_NoDB() {
output := bytes.NewBufferString("")
rootCmd.SetOut(output)
Expand Down