Skip to content

Commit f333385

Browse files
authored
feat(cli): add json output to attestation push (#730)
Signed-off-by: Miguel Martinez Trivino <[email protected]>
1 parent da83386 commit f333385

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

app/cli/cmd/attestation_push.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,16 @@ func newAttestationPushCmd() *cobra.Command {
8282
return newGracefulError(err)
8383
}
8484

85-
if err := encodeJSON(res.Envelope); err != nil {
86-
return err
85+
// NOTE: we are not moving yet to full table by default
86+
// to maintain backward compatibility
87+
if flagOutputFormat == formatJSON {
88+
return encodeJSON(res)
8789
}
8890

91+
// default to mix of json and text to stdout and stderr by default
92+
// kept to maintain backward compatibility
93+
// TODO: in the future we'll render either a table or a proper report
94+
fmt.Println(res.Envelope)
8995
if res.Digest != "" {
9096
logger.Info().Msgf("Attestation Digest: %s", res.Digest)
9197
}

app/cli/internal/action/attestation_push.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package action
1717

1818
import (
19+
"bytes"
1920
"context"
2021
"encoding/json"
2122
"fmt"
@@ -35,8 +36,8 @@ type AttestationPushOpts struct {
3536
}
3637

3738
type AttestationResult struct {
38-
Digest string `json:"digest"`
39-
Envelope *dsse.Envelope `json:"envelope"`
39+
Digest string `json:"digest"`
40+
Envelope string `json:"envelope"`
4041
}
4142

4243
type AttestationPush struct {
@@ -129,7 +130,14 @@ func (action *AttestationPush) Run(ctx context.Context, attestationID string, ru
129130
return nil, err
130131
}
131132

132-
attestationResult := &AttestationResult{Envelope: envelope}
133+
var rawEnvelope bytes.Buffer
134+
encoder := json.NewEncoder(&rawEnvelope)
135+
encoder.SetIndent("", " ")
136+
if err := encoder.Encode(envelope); err != nil {
137+
return nil, fmt.Errorf("failed to encode output: %w", err)
138+
}
139+
140+
attestationResult := &AttestationResult{Envelope: rawEnvelope.String()}
133141

134142
action.Logger.Debug().Msg("render completed")
135143
if action.c.CraftingState.DryRun {

0 commit comments

Comments
 (0)