use format flag to handle json output in verify cmd

Signed-off-by: Meredith Lancaster <malancas@github.com>
This commit is contained in:
Meredith Lancaster 2024-03-18 10:27:32 -06:00
parent 53df7cd8f2
commit 172e4f6d6d
3 changed files with 7 additions and 6 deletions

View file

@ -8,6 +8,7 @@ import (
"github.com/cli/cli/v2/pkg/cmd/attestation/api"
"github.com/cli/cli/v2/pkg/cmd/attestation/artifact/oci"
"github.com/cli/cli/v2/pkg/cmd/attestation/io"
"github.com/cli/cli/v2/pkg/cmdutil"
)
// Options captures the options for the verify command
@ -17,7 +18,6 @@ type Options struct {
CustomTrustedRoot string
DenySelfHostedRunner bool
DigestAlgorithm string
JsonResult bool
NoPublicGood bool
OIDCIssuer string
Owner string
@ -28,6 +28,7 @@ type Options struct {
Logger *io.Handler
Limit int
OCIClient oci.Client
exporter cmdutil.Exporter
}
// Clean cleans the file path option values

View file

@ -120,9 +120,9 @@ func NewVerifyCmd(f *cmdutil.Factory, runF func(*Options) error) *cobra.Command
verifyCmd.MarkFlagsMutuallyExclusive("owner", "repo")
verifyCmd.MarkFlagsOneRequired("owner", "repo")
verifyCmd.Flags().BoolVarP(&opts.NoPublicGood, "no-public-good", "", false, "Only verify attestations signed with GitHub's Sigstore instance")
verifyCmd.Flags().BoolVarP(&opts.JsonResult, "json-result", "j", false, "Output verification result as JSON lines")
verifyCmd.Flags().StringVarP(&opts.CustomTrustedRoot, "custom-trusted-root", "", "", "Path to a custom trustedroot.json file to use for verification")
verifyCmd.Flags().IntVarP(&opts.Limit, "limit", "L", api.DefaultLimit, "Maximum number of attestations to fetch")
cmdutil.AddFormatFlags(verifyCmd, &opts.exporter)
// policy enforcement flags
verifyCmd.Flags().BoolVarP(&opts.DenySelfHostedRunner, "deny-self-hosted-runners", "", false, "Fail verification for attestations generated on self-hosted runners.")
verifyCmd.Flags().StringVarP(&opts.SAN, "cert-identity", "", "", "Enforce that the certificate's subject alternative name matches the provided value exactly")
@ -191,7 +191,7 @@ func runVerify(opts *Options) error {
opts.Logger.Println(opts.Logger.ColorScheme.Green("All attestations have been successfully verified!"))
if opts.JsonResult {
if opts.exporter != nil {
verificationResults := sigstoreRes.VerifyResults
// print each result as JSON line
@ -204,8 +204,9 @@ func runVerify(opts *Options) error {
jsonResults[i] = string(jsonBytes)
}
fmt.Fprintf(opts.Logger.IO.Out, "%v", jsonResults)
if err = opts.exporter.Write(opts.Logger.IO, jsonResults); err != nil {
return fmt.Errorf("failed to write JSON output")
}
}
// All attestations passed verification and policy evaluation

View file

@ -194,7 +194,6 @@ func TestNewVerifyCmd(t *testing.T) {
assert.Equal(t, tc.wants.CustomTrustedRoot, opts.CustomTrustedRoot)
assert.Equal(t, tc.wants.DenySelfHostedRunner, opts.DenySelfHostedRunner)
assert.Equal(t, tc.wants.DigestAlgorithm, opts.DigestAlgorithm)
assert.Equal(t, tc.wants.JsonResult, opts.JsonResult)
assert.Equal(t, tc.wants.Limit, opts.Limit)
assert.Equal(t, tc.wants.NoPublicGood, opts.NoPublicGood)
assert.Equal(t, tc.wants.OIDCIssuer, opts.OIDCIssuer)