Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move context.TODO to context.Background() (4) #2749

Merged
merged 2 commits into from
Aug 2, 2024
Merged
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
25 changes: 12 additions & 13 deletions src/pkg/packager/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,22 @@
)

// Run runs all provided actions.
func Run(defaultCfg types.ZarfComponentActionDefaults, actions []types.ZarfComponentAction, variableConfig *variables.VariableConfig) error {
func Run(ctx context.Context, defaultCfg types.ZarfComponentActionDefaults, actions []types.ZarfComponentAction, variableConfig *variables.VariableConfig) error {

Check warning on line 25 in src/pkg/packager/actions/actions.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/actions/actions.go#L25

Added line #L25 was not covered by tests
if variableConfig == nil {
variableConfig = template.GetZarfVariableConfig()
}

for _, a := range actions {
if err := runAction(defaultCfg, a, variableConfig); err != nil {
if err := runAction(ctx, defaultCfg, a, variableConfig); err != nil {

Check warning on line 31 in src/pkg/packager/actions/actions.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/actions/actions.go#L31

Added line #L31 was not covered by tests
return err
}
}
return nil
}

// Run commands that a component has provided.
func runAction(defaultCfg types.ZarfComponentActionDefaults, action types.ZarfComponentAction, variableConfig *variables.VariableConfig) error {
func runAction(ctx context.Context, defaultCfg types.ZarfComponentActionDefaults, action types.ZarfComponentAction, variableConfig *variables.VariableConfig) error {

Check warning on line 39 in src/pkg/packager/actions/actions.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/actions/actions.go#L39

Added line #L39 was not covered by tests
var (
ctx context.Context
cancel context.CancelFunc
cmdEscaped string
out string
err error
Expand All @@ -56,7 +54,7 @@
}

// Convert the wait to a command.
if cmd, err = convertWaitToCmd(*action.Wait, action.MaxTotalSeconds); err != nil {
if cmd, err = convertWaitToCmd(ctx, *action.Wait, action.MaxTotalSeconds); err != nil {

Check warning on line 57 in src/pkg/packager/actions/actions.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/actions/actions.go#L57

Added line #L57 was not covered by tests
return err
}

Expand Down Expand Up @@ -85,9 +83,9 @@
// Persist the spinner output so it doesn't get overwritten by the command output.
spinner.EnablePreserveWrites()

actionDefaults := actionGetCfg(defaultCfg, action, variableConfig.GetAllTemplates())
actionDefaults := actionGetCfg(ctx, defaultCfg, action, variableConfig.GetAllTemplates())

Check warning on line 86 in src/pkg/packager/actions/actions.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/actions/actions.go#L86

Added line #L86 was not covered by tests

if cmd, err = actionCmdMutation(cmd, actionDefaults.Shell); err != nil {
if cmd, err = actionCmdMutation(ctx, cmd, actionDefaults.Shell); err != nil {

Check warning on line 88 in src/pkg/packager/actions/actions.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/actions/actions.go#L88

Added line #L88 was not covered by tests
spinner.Errorf(err, "Error mutating command: %s", cmdEscaped)
}

Expand Down Expand Up @@ -128,7 +126,8 @@
// If no timeout is set, run the command and return or continue retrying.
if actionDefaults.MaxTotalSeconds < 1 {
spinner.Updatef("Waiting for \"%s\" (no timeout)", cmdEscaped)
if err := tryCmd(context.TODO()); err != nil {
//TODO (schristoff): Make it so tryCmd can take a normal ctx
if err := tryCmd(context.Background()); err != nil {

Check warning on line 130 in src/pkg/packager/actions/actions.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/actions/actions.go#L130

Added line #L130 was not covered by tests
schristoff marked this conversation as resolved.
Show resolved Hide resolved
continue retryCmd
}

Expand All @@ -144,7 +143,7 @@

// Otherwise, try running the command.
default:
ctx, cancel = context.WithTimeout(context.Background(), duration)
ctx, cancel := context.WithTimeout(ctx, duration)

Check warning on line 146 in src/pkg/packager/actions/actions.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/actions/actions.go#L146

Added line #L146 was not covered by tests
defer cancel()
if err := tryCmd(ctx); err != nil {
continue retryCmd
Expand All @@ -169,7 +168,7 @@
}

// convertWaitToCmd will return the wait command if it exists, otherwise it will return the original command.
func convertWaitToCmd(wait types.ZarfComponentActionWait, timeout *int) (string, error) {
func convertWaitToCmd(_ context.Context, wait types.ZarfComponentActionWait, timeout *int) (string, error) {

Check warning on line 171 in src/pkg/packager/actions/actions.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/actions/actions.go#L171

Added line #L171 was not covered by tests
// Build the timeout string.
timeoutString := fmt.Sprintf("--timeout %ds", *timeout)

Expand Down Expand Up @@ -205,7 +204,7 @@
}

// Perform some basic string mutations to make commands more useful.
func actionCmdMutation(cmd string, shellPref exec.Shell) (string, error) {
func actionCmdMutation(_ context.Context, cmd string, shellPref exec.Shell) (string, error) {

Check warning on line 207 in src/pkg/packager/actions/actions.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/actions/actions.go#L207

Added line #L207 was not covered by tests
zarfCommand, err := utils.GetFinalExecutableCommand()
if err != nil {
return cmd, err
Expand Down Expand Up @@ -236,7 +235,7 @@
}

// Merge the ActionSet defaults with the action config.
func actionGetCfg(cfg types.ZarfComponentActionDefaults, a types.ZarfComponentAction, vars map[string]*variables.TextTemplate) types.ZarfComponentActionDefaults {
func actionGetCfg(_ context.Context, cfg types.ZarfComponentActionDefaults, a types.ZarfComponentAction, vars map[string]*variables.TextTemplate) types.ZarfComponentActionDefaults {

Check warning on line 238 in src/pkg/packager/actions/actions.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/actions/actions.go#L238

Added line #L238 was not covered by tests
if a.Mute != nil {
cfg.Mute = *a.Mute
}
Expand Down
8 changes: 4 additions & 4 deletions src/pkg/packager/creator/normal.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
onCreate := component.Actions.OnCreate

onFailure := func() {
if err := actions.Run(onCreate.Defaults, onCreate.OnFailure, nil); err != nil {
if err := actions.Run(ctx, onCreate.Defaults, onCreate.OnFailure, nil); err != nil {

Check warning on line 139 in src/pkg/packager/creator/normal.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/creator/normal.go#L139

Added line #L139 was not covered by tests
message.Debugf("unable to run component failure action: %s", err.Error())
}
}
Expand All @@ -146,7 +146,7 @@
return fmt.Errorf("unable to add component %q: %w", component.Name, err)
}

if err := actions.Run(onCreate.Defaults, onCreate.OnSuccess, nil); err != nil {
if err := actions.Run(ctx, onCreate.Defaults, onCreate.OnSuccess, nil); err != nil {

Check warning on line 149 in src/pkg/packager/creator/normal.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/creator/normal.go#L149

Added line #L149 was not covered by tests
onFailure()
return fmt.Errorf("unable to run component success action: %w", err)
}
Expand Down Expand Up @@ -359,7 +359,7 @@
}

onCreate := component.Actions.OnCreate
if err := actions.Run(onCreate.Defaults, onCreate.Before, nil); err != nil {
if err := actions.Run(ctx, onCreate.Defaults, onCreate.Before, nil); err != nil {

Check warning on line 362 in src/pkg/packager/creator/normal.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/creator/normal.go#L362

Added line #L362 was not covered by tests
return fmt.Errorf("unable to run component before action: %w", err)
}

Expand Down Expand Up @@ -521,7 +521,7 @@
spinner.Success()
}

if err := actions.Run(onCreate.Defaults, onCreate.After, nil); err != nil {
if err := actions.Run(ctx, onCreate.Defaults, onCreate.After, nil); err != nil {

Check warning on line 524 in src/pkg/packager/creator/normal.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/creator/normal.go#L524

Added line #L524 was not covered by tests
return fmt.Errorf("unable to run component after action: %w", err)
}

Expand Down
8 changes: 4 additions & 4 deletions src/pkg/packager/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
onDeploy := component.Actions.OnDeploy

onFailure := func() {
if err := actions.Run(onDeploy.Defaults, onDeploy.OnFailure, p.variableConfig); err != nil {
if err := actions.Run(ctx, onDeploy.Defaults, onDeploy.OnFailure, p.variableConfig); err != nil {

Check warning on line 197 in src/pkg/packager/deploy.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/deploy.go#L197

Added line #L197 was not covered by tests
message.Debugf("unable to run component failure action: %s", err.Error())
}
}
Expand Down Expand Up @@ -222,7 +222,7 @@
}
}

if err := actions.Run(onDeploy.Defaults, onDeploy.OnSuccess, p.variableConfig); err != nil {
if err := actions.Run(ctx, onDeploy.Defaults, onDeploy.OnSuccess, p.variableConfig); err != nil {

Check warning on line 225 in src/pkg/packager/deploy.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/deploy.go#L225

Added line #L225 was not covered by tests
onFailure()
return deployedComponents, fmt.Errorf("unable to run component success action: %w", err)
}
Expand Down Expand Up @@ -324,7 +324,7 @@
return charts, err
}

if err = actions.Run(onDeploy.Defaults, onDeploy.Before, p.variableConfig); err != nil {
if err = actions.Run(ctx, onDeploy.Defaults, onDeploy.Before, p.variableConfig); err != nil {

Check warning on line 327 in src/pkg/packager/deploy.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/deploy.go#L327

Added line #L327 was not covered by tests
return charts, fmt.Errorf("unable to run component before action: %w", err)
}

Expand Down Expand Up @@ -359,7 +359,7 @@
}
}

if err = actions.Run(onDeploy.Defaults, onDeploy.After, p.variableConfig); err != nil {
if err = actions.Run(ctx, onDeploy.Defaults, onDeploy.After, p.variableConfig); err != nil {

Check warning on line 362 in src/pkg/packager/deploy.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/deploy.go#L362

Added line #L362 was not covered by tests
return charts, fmt.Errorf("unable to run component after action: %w", err)
}

Expand Down
8 changes: 4 additions & 4 deletions src/pkg/packager/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@

onRemove := c.Actions.OnRemove
onFailure := func() {
if err := actions.Run(onRemove.Defaults, onRemove.OnFailure, nil); err != nil {
if err := actions.Run(ctx, onRemove.Defaults, onRemove.OnFailure, nil); err != nil {

Check warning on line 172 in src/pkg/packager/remove.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/remove.go#L172

Added line #L172 was not covered by tests
message.Debugf("Unable to run the failure action: %s", err)
}
}

if err := actions.Run(onRemove.Defaults, onRemove.Before, nil); err != nil {
if err := actions.Run(ctx, onRemove.Defaults, onRemove.Before, nil); err != nil {

Check warning on line 177 in src/pkg/packager/remove.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/remove.go#L177

Added line #L177 was not covered by tests
onFailure()
return nil, fmt.Errorf("unable to run the before action for component (%s): %w", c.Name, err)
}
Expand Down Expand Up @@ -206,12 +206,12 @@
}
}

if err := actions.Run(onRemove.Defaults, onRemove.After, nil); err != nil {
if err := actions.Run(ctx, onRemove.Defaults, onRemove.After, nil); err != nil {

Check warning on line 209 in src/pkg/packager/remove.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/remove.go#L209

Added line #L209 was not covered by tests
onFailure()
return deployedPackage, fmt.Errorf("unable to run the after action: %w", err)
}

if err := actions.Run(onRemove.Defaults, onRemove.OnSuccess, nil); err != nil {
if err := actions.Run(ctx, onRemove.Defaults, onRemove.OnSuccess, nil); err != nil {

Check warning on line 214 in src/pkg/packager/remove.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/remove.go#L214

Added line #L214 was not covered by tests
onFailure()
return deployedPackage, fmt.Errorf("unable to run the success action: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/packager/sources/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@

spinner.Success()

if err := ValidatePackageSignature(dst, s.PublicKeyPath); err != nil {
if err := ValidatePackageSignature(ctx, dst, s.PublicKeyPath); err != nil {

Check warning on line 81 in src/pkg/packager/sources/oci.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/sources/oci.go#L81

Added line #L81 was not covered by tests
return pkg, nil, err
}
}
Expand Down Expand Up @@ -140,7 +140,7 @@
spinner.Success()
}

if err := ValidatePackageSignature(dst, s.PublicKeyPath); err != nil {
if err := ValidatePackageSignature(ctx, dst, s.PublicKeyPath); err != nil {

Check warning on line 143 in src/pkg/packager/sources/oci.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/sources/oci.go#L143

Added line #L143 was not covered by tests
if errors.Is(err, ErrPkgSigButNoKey) && skipValidation {
message.Warn("The package was signed but no public key was provided, skipping signature validation")
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/pkg/packager/sources/tarball.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type TarballSource struct {
}

// LoadPackage loads a package from a tarball.
func (s *TarballSource) LoadPackage(_ context.Context, dst *layout.PackagePaths, filter filters.ComponentFilterStrategy, unarchiveAll bool) (pkg types.ZarfPackage, warnings []string, err error) {
func (s *TarballSource) LoadPackage(ctx context.Context, dst *layout.PackagePaths, filter filters.ComponentFilterStrategy, unarchiveAll bool) (pkg types.ZarfPackage, warnings []string, err error) {
spinner := message.NewProgressSpinner("Loading package from %q", s.PackageSource)
defer spinner.Stop()

Expand Down Expand Up @@ -106,7 +106,7 @@ func (s *TarballSource) LoadPackage(_ context.Context, dst *layout.PackagePaths,

spinner.Success()

if err := ValidatePackageSignature(dst, s.PublicKeyPath); err != nil {
if err := ValidatePackageSignature(ctx, dst, s.PublicKeyPath); err != nil {
return pkg, nil, err
}
}
Expand Down Expand Up @@ -138,7 +138,7 @@ func (s *TarballSource) LoadPackage(_ context.Context, dst *layout.PackagePaths,
}

// LoadPackageMetadata loads a package's metadata from a tarball.
func (s *TarballSource) LoadPackageMetadata(_ context.Context, dst *layout.PackagePaths, wantSBOM bool, skipValidation bool) (pkg types.ZarfPackage, warnings []string, err error) {
func (s *TarballSource) LoadPackageMetadata(ctx context.Context, dst *layout.PackagePaths, wantSBOM bool, skipValidation bool) (pkg types.ZarfPackage, warnings []string, err error) {
if s.Shasum != "" {
if err := helpers.SHAsMatch(s.PackageSource, s.Shasum); err != nil {
return pkg, nil, err
Expand Down Expand Up @@ -184,7 +184,7 @@ func (s *TarballSource) LoadPackageMetadata(_ context.Context, dst *layout.Packa
spinner.Success()
}

if err := ValidatePackageSignature(dst, s.PublicKeyPath); err != nil {
if err := ValidatePackageSignature(ctx, dst, s.PublicKeyPath); err != nil {
if errors.Is(err, ErrPkgSigButNoKey) && skipValidation {
message.Warn("The package was signed but no public key was provided, skipping signature validation")
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/pkg/packager/sources/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import (
"bufio"
"context"
"errors"
"fmt"
"io/fs"
Expand All @@ -28,7 +29,7 @@
)

// ValidatePackageSignature validates the signature of a package
func ValidatePackageSignature(paths *layout.PackagePaths, publicKeyPath string) error {
func ValidatePackageSignature(ctx context.Context, paths *layout.PackagePaths, publicKeyPath string) error {
// If the insecure flag was provided ignore the signature validation
if config.CommonOptions.Insecure {
return nil
Expand All @@ -52,7 +53,7 @@
}

// Validate the signature with the key we were provided
if err := utils.CosignVerifyBlob(paths.ZarfYAML, paths.Signature, publicKeyPath); err != nil {
if err := utils.CosignVerifyBlob(ctx, paths.ZarfYAML, paths.Signature, publicKeyPath); err != nil {

Check warning on line 56 in src/pkg/packager/sources/validate.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/packager/sources/validate.go#L56

Added line #L56 was not covered by tests
return fmt.Errorf("package signature did not match the provided key: %w", err)
}

Expand Down
4 changes: 2 additions & 2 deletions src/pkg/utils/cosign.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
}

// CosignVerifyBlob verifies the zarf.yaml.sig was signed with the key provided by the flag
func CosignVerifyBlob(blobRef string, sigRef string, keyPath string) error {
func CosignVerifyBlob(ctx context.Context, blobRef string, sigRef string, keyPath string) error {

Check warning on line 182 in src/pkg/utils/cosign.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/utils/cosign.go#L182

Added line #L182 was not covered by tests
keyOptions := options.KeyOpts{KeyRef: keyPath}
cmd := &verify.VerifyBlobCmd{
KeyOpts: keyOptions,
Expand All @@ -188,7 +188,7 @@
Offline: true,
IgnoreTlog: true,
}
err := cmd.Exec(context.TODO(), blobRef)
err := cmd.Exec(ctx, blobRef)

Check warning on line 191 in src/pkg/utils/cosign.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/utils/cosign.go#L191

Added line #L191 was not covered by tests
if err == nil {
message.Successf("Package signature validated!")
}
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/utils/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@

// Cmd executes a given command with given config.
func Cmd(command string, args ...string) (string, string, error) {
return CmdWithContext(context.TODO(), Config{}, command, args...)
return CmdWithContext(context.Background(), Config{}, command, args...)

Check warning on line 45 in src/pkg/utils/exec/exec.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/utils/exec/exec.go#L45

Added line #L45 was not covered by tests
}

// CmdWithPrint executes a given command with given config and prints the command.
func CmdWithPrint(command string, args ...string) error {
_, _, err := CmdWithContext(context.TODO(), PrintCfg(), command, args...)
_, _, err := CmdWithContext(context.Background(), PrintCfg(), command, args...)

Check warning on line 50 in src/pkg/utils/exec/exec.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/utils/exec/exec.go#L50

Added line #L50 was not covered by tests
return err
}

Expand Down
Loading