Skip to content

Commit 8e82f7f

Browse files
authored
chore(cli): make attestation cmds context aware (#497)
Signed-off-by: Miguel Martinez Trivino <[email protected]>
1 parent bec4b2a commit 8e82f7f

File tree

14 files changed

+76
-72
lines changed

14 files changed

+76
-72
lines changed

app/cli/cmd/attestation_add.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func newAttestationAddCmd() *cobra.Command {
5555
return err
5656
}
5757

58-
if err := a.Run("", name, value, annotations); err != nil {
58+
if err := a.Run(cmd.Context(), "", name, value, annotations); err != nil {
5959
if errors.Is(err, action.ErrAttestationNotInitialized) {
6060
return err
6161
}

app/cli/cmd/attestation_init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func newAttestationInitCmd() *cobra.Command {
4848
}
4949

5050
// Initialize it
51-
err = a.Run(contractRevision)
51+
err = a.Run(cmd.Context(), contractRevision)
5252
if err != nil {
5353
if errors.Is(err, action.ErrAttestationAlreadyExist) {
5454
return err
@@ -67,7 +67,7 @@ func newAttestationInitCmd() *cobra.Command {
6767
return newGracefulError(err)
6868
}
6969

70-
res, err := statusAction.Run("")
70+
res, err := statusAction.Run(cmd.Context(), "")
7171
if err != nil {
7272
return newGracefulError(err)
7373
}

app/cli/cmd/attestation_push.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func newAttestationPushCmd() *cobra.Command {
7373
return err
7474
}
7575

76-
res, err := a.Run("", annotations)
76+
res, err := a.Run(cmd.Context(), "", annotations)
7777
if err != nil {
7878
if errors.Is(err, action.ErrAttestationNotInitialized) {
7979
return err

app/cli/cmd/attestation_reset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func newAttestationResetCmd() *cobra.Command {
4646
return fmt.Errorf("failed to load action: %w", err)
4747
}
4848

49-
if err := a.Run("", trigger, reason); err != nil {
49+
if err := a.Run(cmd.Context(), "", trigger, reason); err != nil {
5050
return newGracefulError(err)
5151
}
5252

app/cli/cmd/attestation_status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func newAttestationStatusCmd() *cobra.Command {
4343
return fmt.Errorf("failed to load action: %w", err)
4444
}
4545

46-
res, err := a.Run("")
46+
res, err := a.Run(cmd.Context(), "")
4747
if err != nil {
4848
return err
4949
}

app/cli/internal/action/attestation_add.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,19 @@ func NewAttestationAdd(cfg *AttestationAddOpts) (*AttestationAdd, error) {
5757

5858
var ErrAttestationNotInitialized = errors.New("attestation not yet initialized")
5959

60-
func (action *AttestationAdd) Run(attestationID, materialName, materialValue string, annotations map[string]string) error {
61-
if initialized := action.c.AlreadyInitialized(attestationID); !initialized {
60+
func (action *AttestationAdd) Run(ctx context.Context, attestationID, materialName, materialValue string, annotations map[string]string) error {
61+
if initialized := action.c.AlreadyInitialized(ctx, attestationID); !initialized {
6262
return ErrAttestationNotInitialized
6363
}
6464

65-
if err := action.c.LoadCraftingState(attestationID); err != nil {
65+
if err := action.c.LoadCraftingState(ctx, attestationID); err != nil {
6666
action.Logger.Err(err).Msg("loading existing attestation")
6767
return err
6868
}
6969

7070
// Get upload creds and CASbackend for the current attestation and set up CAS client
7171
client := pb.NewAttestationServiceClient(action.CPConnection)
72-
creds, err := client.GetUploadCreds(context.Background(),
72+
creds, err := client.GetUploadCreds(ctx,
7373
&pb.AttestationServiceGetUploadCredsRequest{
7474
WorkflowRunId: action.c.CraftingState.GetAttestation().GetWorkflow().GetWorkflowRunId(),
7575
},
@@ -101,7 +101,7 @@ func (action *AttestationAdd) Run(attestationID, materialName, materialValue str
101101
casBackend.Uploader = casclient.New(artifactCASConn, casclient.WithLogger(action.Logger))
102102
}
103103

104-
if err := action.c.AddMaterial(attestationID, materialName, materialValue, casBackend, annotations); err != nil {
104+
if err := action.c.AddMaterial(ctx, attestationID, materialName, materialValue, casBackend, annotations); err != nil {
105105
return fmt.Errorf("adding material: %w", err)
106106
}
107107

app/cli/internal/action/attestation_init.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,10 @@ func NewAttestationInit(cfg *AttestationInitOpts) (*AttestationInit, error) {
6161
}, nil
6262
}
6363

64-
func (action *AttestationInit) Run(contractRevision int) error {
64+
func (action *AttestationInit) Run(ctx context.Context, contractRevision int) error {
6565
action.Logger.Debug().Msg("Retrieving attestation definition")
6666
client := pb.NewAttestationServiceClient(action.ActionsOpts.CPConnection)
6767
// get information of the workflow
68-
ctx := context.Background()
6968
resp, err := client.GetContract(ctx, &pb.AttestationServiceGetContractRequest{ContractRevision: int32(contractRevision)})
7069
if err != nil {
7170
return err
@@ -123,17 +122,17 @@ func (action *AttestationInit) Run(contractRevision int) error {
123122
AttestationID: attestationID,
124123
}
125124

126-
if err := action.c.Init(initOpts); err != nil {
125+
if err := action.c.Init(ctx, initOpts); err != nil {
127126
return err
128127
}
129128

130129
// Load the env variables both the system populated and the user predefined ones
131-
if err := action.c.ResolveEnvVars(attestationID); err != nil {
130+
if err := action.c.ResolveEnvVars(ctx, attestationID); err != nil {
132131
if action.dryRun {
133132
return nil
134133
}
135134

136-
_ = action.c.Reset(attestationID)
135+
_ = action.c.Reset(ctx, attestationID)
137136
return err
138137
}
139138

app/cli/internal/action/attestation_push.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ func NewAttestationPush(cfg *AttestationPushOpts) (*AttestationPush, error) {
6060
}, nil
6161
}
6262

63-
func (action *AttestationPush) Run(attestationID string, runtimeAnnotations map[string]string) (*AttestationResult, error) {
64-
if initialized := action.c.AlreadyInitialized(attestationID); !initialized {
63+
func (action *AttestationPush) Run(ctx context.Context, attestationID string, runtimeAnnotations map[string]string) (*AttestationResult, error) {
64+
if initialized := action.c.AlreadyInitialized(ctx, attestationID); !initialized {
6565
return nil, ErrAttestationNotInitialized
6666
}
6767

68-
if err := action.c.LoadCraftingState(attestationID); err != nil {
68+
if err := action.c.LoadCraftingState(ctx, attestationID); err != nil {
6969
action.Logger.Err(err).Msg("loading existing attestation")
7070
return nil, err
7171
}
@@ -133,36 +133,36 @@ func (action *AttestationPush) Run(attestationID string, runtimeAnnotations map[
133133
if action.c.CraftingState.DryRun {
134134
action.Logger.Info().Msg("dry-run completed, push skipped")
135135
// We are done, remove the existing att state
136-
if err := action.c.Reset(attestationID); err != nil {
136+
if err := action.c.Reset(ctx, attestationID); err != nil {
137137
return nil, err
138138
}
139139

140140
return attestationResult, nil
141141
}
142142

143-
attestationResult.Digest, err = pushToControlPlane(action.ActionsOpts.CPConnection, envelope, action.c.CraftingState.Attestation.GetWorkflow().GetWorkflowRunId())
143+
attestationResult.Digest, err = pushToControlPlane(ctx, action.ActionsOpts.CPConnection, envelope, action.c.CraftingState.Attestation.GetWorkflow().GetWorkflowRunId())
144144
if err != nil {
145145
return nil, fmt.Errorf("pushing to control plane: %w", err)
146146
}
147147

148148
action.Logger.Info().Msg("push completed")
149149

150150
// We are done, remove the existing att state
151-
if err := action.c.Reset(attestationID); err != nil {
151+
if err := action.c.Reset(ctx, attestationID); err != nil {
152152
return nil, err
153153
}
154154

155155
return attestationResult, nil
156156
}
157157

158-
func pushToControlPlane(conn *grpc.ClientConn, envelope *dsse.Envelope, workflowRunID string) (string, error) {
158+
func pushToControlPlane(ctx context.Context, conn *grpc.ClientConn, envelope *dsse.Envelope, workflowRunID string) (string, error) {
159159
encodedAttestation, err := encodeEnvelope(envelope)
160160
if err != nil {
161161
return "", fmt.Errorf("encoding attestation: %w", err)
162162
}
163163

164164
client := pb.NewAttestationServiceClient(conn)
165-
resp, err := client.Store(context.Background(), &pb.AttestationServiceStoreRequest{
165+
resp, err := client.Store(ctx, &pb.AttestationServiceStoreRequest{
166166
Attestation: encodedAttestation,
167167
WorkflowRunId: workflowRunID,
168168
})

app/cli/internal/action/attestation_reset.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ func NewAttestationReset(opts *ActionsOpts) (*AttestationReset, error) {
4545
return &AttestationReset{ActionsOpts: opts, c: c}, nil
4646
}
4747

48-
func (action *AttestationReset) Run(attestationID, trigger, reason string) error {
49-
if initialized := action.c.AlreadyInitialized(attestationID); !initialized {
48+
func (action *AttestationReset) Run(ctx context.Context, attestationID, trigger, reason string) error {
49+
if initialized := action.c.AlreadyInitialized(ctx, attestationID); !initialized {
5050
return ErrAttestationNotInitialized
5151
}
5252

53-
if err := action.c.LoadCraftingState(attestationID); err != nil {
53+
if err := action.c.LoadCraftingState(ctx, attestationID); err != nil {
5454
action.Logger.Err(err).Msg("loading existing attestation")
5555
return err
5656
}
@@ -70,7 +70,7 @@ func (action *AttestationReset) Run(attestationID, trigger, reason string) error
7070
}
7171
}
7272

73-
return action.c.Reset(attestationID)
73+
return action.c.Reset(ctx, attestationID)
7474
}
7575

7676
func parseTrigger(in string) pb.AttestationServiceCancelRequest_TriggerType {

app/cli/internal/action/attestation_status.go

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

1818
import (
19+
"context"
1920
"fmt"
2021
"time"
2122

@@ -66,14 +67,14 @@ func NewAttestationStatus(cfg *AttestationStatusOpts) (*AttestationStatus, error
6667
return &AttestationStatus{ActionsOpts: cfg.ActionsOpts, c: c}, nil
6768
}
6869

69-
func (action *AttestationStatus) Run(attestationID string) (*AttestationStatusResult, error) {
70+
func (action *AttestationStatus) Run(ctx context.Context, attestationID string) (*AttestationStatusResult, error) {
7071
c := action.c
7172

72-
if initialized := c.AlreadyInitialized(attestationID); !initialized {
73+
if initialized := c.AlreadyInitialized(ctx, attestationID); !initialized {
7374
return nil, ErrAttestationNotInitialized
7475
}
7576

76-
if err := c.LoadCraftingState(attestationID); err != nil {
77+
if err := c.LoadCraftingState(ctx, attestationID); err != nil {
7778
action.Logger.Err(err).Msg("loading existing attestation")
7879
return nil, err
7980
}

0 commit comments

Comments
 (0)