@@ -31,8 +31,8 @@ func NewCmdVerifyAsset(f *cmdutil.Factory, runF func(*attestation.AttestOptions)
3131 return cmdutil .FlagErrorf ("You must specify a tag and a file path" )
3232 }
3333
34- opts . TagName = args [0 ]
35- opts . FilePath = args [1 ]
34+ tagName : = args [0 ]
35+ assetFilePath : = args [1 ]
3636
3737 httpClient , err := f .HttpClient ()
3838 if err != nil {
@@ -46,8 +46,8 @@ func NewCmdVerifyAsset(f *cmdutil.Factory, runF func(*attestation.AttestOptions)
4646 hostname , _ := ghauth .DefaultHost ()
4747
4848 * opts = attestation.AttestOptions {
49- TagName : opts . TagName ,
50- FilePath : opts . FilePath ,
49+ TagName : tagName ,
50+ AssetFilePath : assetFilePath ,
5151 Repo : baseRepo .RepoOwner () + "/" + baseRepo .RepoName (),
5252 APIClient : api .NewLiveClient (httpClient , hostname , logger ),
5353 Limit : 10 ,
@@ -93,15 +93,17 @@ func NewCmdVerifyAsset(f *cmdutil.Factory, runF func(*attestation.AttestOptions)
9393 return verifyAssetRun (opts )
9494 },
9595 }
96+ cmdutil .AddFormatFlags (cmd , & opts .Exporter )
97+
9698 return cmd
9799}
98100
99101func verifyAssetRun (opts * attestation.AttestOptions ) error {
100102 ctx := context .Background ()
101- fileName := getFileName (opts .FilePath )
103+ fileName := getFileName (opts .AssetFilePath )
102104
103105 // calculate the digest of the file
104- fileDigest , err := artifact .NewDigestedArtifact (nil , opts .FilePath , "sha256" )
106+ fileDigest , err := artifact .NewDigestedArtifact (nil , opts .AssetFilePath , "sha256" )
105107 if err != nil {
106108 opts .Logger .Println (opts .Logger .ColorScheme .Red ("✗ Failed to calculate file digest" ))
107109 return err
@@ -141,7 +143,7 @@ func verifyAssetRun(opts *attestation.AttestOptions) error {
141143 }
142144
143145 if len (filteredAttestations ) == 0 {
144- opts .Logger .Printf (opts .Logger .ColorScheme .Red ("Release %s does not contain %s (%s)\n " ), opts .TagName , opts .FilePath , fileDigest .DigestWithAlg ())
146+ opts .Logger .Printf (opts .Logger .ColorScheme .Red ("Release %s does not contain %s (%s)\n " ), opts .TagName , opts .AssetFilePath , fileDigest .DigestWithAlg ())
145147 return nil
146148 }
147149
@@ -152,10 +154,20 @@ func verifyAssetRun(opts *attestation.AttestOptions) error {
152154
153155 if err != nil {
154156 opts .Logger .Println (opts .Logger .ColorScheme .Red (errMsg ))
155- opts .Logger .Printf (opts .Logger .ColorScheme .Red ("Release %s does not contain %s (%s)\n " ), opts .TagName , opts .FilePath , fileDigest .DigestWithAlg ())
157+ opts .Logger .Printf (opts .Logger .ColorScheme .Red ("Release %s does not contain %s (%s)\n " ), opts .TagName , opts .AssetFilePath , fileDigest .DigestWithAlg ())
156158 return err
157159 }
158160
161+ // If an exporter is provided with the --json flag, write the results to the terminal in JSON format
162+ if opts .Exporter != nil {
163+ // print the results to the terminal as an array of JSON objects
164+ if err = opts .Exporter .Write (opts .Logger .IO , verified ); err != nil {
165+ opts .Logger .Println (opts .Logger .ColorScheme .Red ("✗ Failed to write JSON output" ))
166+ return err
167+ }
168+ return nil
169+ }
170+
159171 opts .Logger .Printf ("The following %s matched the policy criteria\n \n " , text .Pluralize (len (verified ), "attestation" ))
160172 opts .Logger .Println (opts .Logger .ColorScheme .Green ("✓ Verification succeeded!\n " ))
161173 opts .Logger .Printf ("Attestation found matching release %s (%s)\n " , opts .TagName , releaseRefDigest .DigestWithAlg ())
0 commit comments