Skip to content

Commit

Permalink
refactor: simplify update a checksum file
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Jan 31, 2025
1 parent d9d49de commit 1dd7fb7
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 135 deletions.
20 changes: 20 additions & 0 deletions pkg/checksum/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"sync"

"github.com/sirupsen/logrus"
"github.com/spf13/afero"
)

Expand All @@ -29,6 +30,25 @@ func New() *Checksums {
}
}

func Open(logE *logrus.Entry, fs afero.Fs, cfgFilePath string, enabled bool) (*Checksums, func(), error) {
if !enabled {
return nil, func() {}, nil
}
checksumFilePath, err := GetChecksumFilePathFromConfigFilePath(fs, cfgFilePath)
if err != nil {
return nil, nil, err //nolint:wrapcheck

Check failure on line 39 in pkg/checksum/type.go

View workflow job for this annotation

GitHub Actions / test / test

directive `//nolint:wrapcheck` is unused for linter "wrapcheck" (nolintlint)
}
checksums := New()
if err := checksums.ReadFile(fs, checksumFilePath); err != nil {
return nil, nil, fmt.Errorf("read a checksum JSON: %w", err)
}
return checksums, func() {
if err := checksums.UpdateFile(fs, checksumFilePath); err != nil {
logE.WithError(err).Error("update a checksum file")
}
}, nil
}

func (c *Checksums) EnableOutput() {
c.stdout = os.Stdout
}
Expand Down
21 changes: 6 additions & 15 deletions pkg/controller/cp/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,13 @@ import (
)

func (c *Controller) install(ctx context.Context, logE *logrus.Entry, findResult *which.FindResult, policyConfigs []*policy.Config, param *config.Param) error {
var checksums *checksum.Checksums
if findResult.Config.ChecksumEnabled(param.EnforceChecksum, param.Checksum) {
checksums = checksum.New()
checksumFilePath, err := checksum.GetChecksumFilePathFromConfigFilePath(c.fs, findResult.ConfigFilePath)
if err != nil {
return err //nolint:wrapcheck
}
if err := checksums.ReadFile(c.fs, checksumFilePath); err != nil {
return fmt.Errorf("read a checksum JSON: %w", err)
}
defer func() {
if err := checksums.UpdateFile(c.fs, checksumFilePath); err != nil {
logE.WithError(err).Error("update a checksum file")
}
}()
checksums, updateChecksum, err := checksum.Open(
logE, c.fs, findResult.ConfigFilePath,
findResult.Config.ChecksumEnabled(param.EnforceChecksum, param.Checksum))
if err != nil {
return fmt.Errorf("read a checksum JSON: %w", err)
}
defer updateChecksum()

if err := c.packageInstaller.InstallPackage(ctx, logE, &installpackage.ParamInstallPackage{
Pkg: findResult.Package,
Expand Down
21 changes: 6 additions & 15 deletions pkg/controller/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,13 @@ func (c *Controller) updateTimestamp(pkg *config.Package) error {
}

func (c *Controller) install(ctx context.Context, logE *logrus.Entry, findResult *which.FindResult, policies []*policy.Config, param *config.Param) error {
var checksums *checksum.Checksums
if findResult.Config.ChecksumEnabled(param.EnforceChecksum, param.Checksum) {
checksums = checksum.New()
checksumFilePath, err := checksum.GetChecksumFilePathFromConfigFilePath(c.fs, findResult.ConfigFilePath)
if err != nil {
return err //nolint:wrapcheck
}
if err := checksums.ReadFile(c.fs, checksumFilePath); err != nil {
return fmt.Errorf("read a checksum JSON: %w", err)
}
defer func() {
if err := checksums.UpdateFile(c.fs, checksumFilePath); err != nil {
logE.WithError(err).Error("update a checksum file")
}
}()
checksums, updateChecksum, err := checksum.Open(
logE, c.fs, findResult.ConfigFilePath,
findResult.Config.ChecksumEnabled(param.EnforceChecksum, param.Checksum))
if err != nil {
return fmt.Errorf("read a checksum JSON: %w", err)
}
defer updateChecksum()

if err := c.packageInstaller.InstallPackage(ctx, logE, &installpackage.ParamInstallPackage{
Pkg: findResult.Package,
Expand Down
21 changes: 6 additions & 15 deletions pkg/controller/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,13 @@ func (c *Controller) getConfigFile(param *config.Param) (string, error) {
}

func (c *Controller) listPkgs(ctx context.Context, logE *logrus.Entry, param *config.Param, cfg *aqua.Config, cfgFilePath string, args ...string) ([]*aqua.Package, error) {
var checksums *checksum.Checksums
if cfg.ChecksumEnabled(param.EnforceChecksum, param.Checksum) {
checksums = checksum.New()
checksumFilePath, err := checksum.GetChecksumFilePathFromConfigFilePath(c.fs, cfgFilePath)
if err != nil {
return nil, err //nolint:wrapcheck
}
if err := checksums.ReadFile(c.fs, checksumFilePath); err != nil {
return nil, fmt.Errorf("read a checksum JSON: %w", err)
}
defer func() {
if err := checksums.UpdateFile(c.fs, checksumFilePath); err != nil {
logE.WithError(err).Error("update a checksum file")
}
}()
checksums, updateChecksum, err := checksum.Open(
logE, c.fs, cfgFilePath,
cfg.ChecksumEnabled(param.EnforceChecksum, param.Checksum))
if err != nil {
return nil, fmt.Errorf("read a checksum JSON: %w", err)
}
defer updateChecksum()

registryContents, err := c.registryInstaller.InstallRegistries(ctx, logE, cfg, cfgFilePath, checksums)
if err != nil {
Expand Down
19 changes: 4 additions & 15 deletions pkg/controller/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,11 @@ func (c *Controller) install(ctx context.Context, logE *logrus.Entry, cfgFilePat
return err //nolint:wrapcheck
}

var checksums *checksum.Checksums
if cfg.ChecksumEnabled(param.EnforceChecksum, param.Checksum) {
checksums = checksum.New()
checksumFilePath, err := checksum.GetChecksumFilePathFromConfigFilePath(c.fs, cfgFilePath)
if err != nil {
return err //nolint:wrapcheck
}
if err := checksums.ReadFile(c.fs, checksumFilePath); err != nil {
return fmt.Errorf("read a checksum JSON: %w", err)
}
defer func() {
if err := checksums.UpdateFile(c.fs, checksumFilePath); err != nil {
logE.WithError(err).Error("update a checksum file")
}
}()
checksums, updateChecksum, err := checksum.Open(logE, c.fs, cfgFilePath, cfg.ChecksumEnabled(param.EnforceChecksum, param.Checksum))
if err != nil {
return fmt.Errorf("read a checksum JSON: %w", err)
}
defer updateChecksum()

registryContents, err := c.registryInstaller.InstallRegistries(ctx, logE, cfg, cfgFilePath, checksums)
if err != nil {
Expand Down
21 changes: 6 additions & 15 deletions pkg/controller/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,13 @@ func (c *Controller) List(ctx context.Context, logE *logrus.Entry, param *config
return err //nolint:wrapcheck
}

var checksums *checksum.Checksums
if cfg.ChecksumEnabled(param.EnforceChecksum, param.Checksum) {
checksums = checksum.New()
checksumFilePath, err := checksum.GetChecksumFilePathFromConfigFilePath(c.fs, cfgFilePath)
if err != nil {
return err //nolint:wrapcheck
}
if err := checksums.ReadFile(c.fs, checksumFilePath); err != nil {
return fmt.Errorf("read a checksum JSON: %w", err)
}
defer func() {
if err := checksums.UpdateFile(c.fs, checksumFilePath); err != nil {
logE.WithError(err).Error("update a checksum file")
}
}()
checksums, updateChecksum, err := checksum.Open(
logE, c.fs, cfgFilePath,
cfg.ChecksumEnabled(param.EnforceChecksum, param.Checksum))
if err != nil {
return fmt.Errorf("read a checksum JSON: %w", err)
}
defer updateChecksum()

registryContents, err := c.registryInstaller.InstallRegistries(ctx, logE, cfg, cfgFilePath, checksums)
if err != nil {
Expand Down
21 changes: 6 additions & 15 deletions pkg/controller/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,13 @@ func (c *Controller) Remove(ctx context.Context, logE *logrus.Entry, param *conf
return fmt.Errorf("read a configuration file: %w", err)
}

var checksums *checksum.Checksums
if cfg.ChecksumEnabled(param.EnforceChecksum, param.Checksum) {
checksums = checksum.New()
checksumFilePath, err := checksum.GetChecksumFilePathFromConfigFilePath(c.fs, cfgFilePath)
if err != nil {
return err //nolint:wrapcheck
}
if err := checksums.ReadFile(c.fs, checksumFilePath); err != nil {
return fmt.Errorf("read a checksum JSON: %w", err)
}
defer func() {
if err := checksums.UpdateFile(c.fs, checksumFilePath); err != nil {
logE.WithError(err).Error("update a checksum file")
}
}()
checksums, updateChecksum, err := checksum.Open(
logE, c.fs, cfgFilePath,
cfg.ChecksumEnabled(param.EnforceChecksum, param.Checksum))
if err != nil {
return fmt.Errorf("read a checksum JSON: %w", err)
}
defer updateChecksum()

registryContents, err := c.registryInstaller.InstallRegistries(ctx, logE, cfg, cfgFilePath, checksums)
if err != nil {
Expand Down
21 changes: 6 additions & 15 deletions pkg/controller/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,13 @@ func (c *Controller) update(ctx context.Context, logE *logrus.Entry, param *conf
return fmt.Errorf("read a configuration file: %w", err)
}

var checksums *checksum.Checksums
if cfg.ChecksumEnabled(param.EnforceChecksum, param.Checksum) {
checksums = checksum.New()
checksumFilePath, err := checksum.GetChecksumFilePathFromConfigFilePath(c.fs, cfgFilePath)
if err != nil {
return err //nolint:wrapcheck
}
if err := checksums.ReadFile(c.fs, checksumFilePath); err != nil {
return fmt.Errorf("read a checksum JSON: %w", err)
}
defer func() {
if err := checksums.UpdateFile(c.fs, checksumFilePath); err != nil {
logE.WithError(err).Error("update a checksum file")
}
}()
checksums, updateChecksum, err := checksum.Open(
logE, c.fs, cfgFilePath,
cfg.ChecksumEnabled(param.EnforceChecksum, param.Checksum))
if err != nil {
return fmt.Errorf("read a checksum JSON: %w", err)
}
defer updateChecksum()

// Update packages before registries because if registries are updated before packages the function needs to install new registries then checksums of new registrires aren't added to aqua-checksums.json.

Expand Down
21 changes: 6 additions & 15 deletions pkg/controller/vacuum/initialize/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,13 @@ func (c *Controller) create(ctx context.Context, logE *logrus.Entry, cfgFilePath
return err //nolint:wrapcheck
}

var checksums *checksum.Checksums
if cfg.ChecksumEnabled(param.EnforceChecksum, param.Checksum) {
checksums = checksum.New()
checksumFilePath, err := checksum.GetChecksumFilePathFromConfigFilePath(c.fs, cfgFilePath)
if err != nil {
return err //nolint:wrapcheck
}
if err := checksums.ReadFile(c.fs, checksumFilePath); err != nil {
return fmt.Errorf("read a checksum JSON: %w", err)
}
defer func() {
if err := checksums.UpdateFile(c.fs, checksumFilePath); err != nil {
logE.WithError(err).Error("update a checksum file")
}
}()
checksums, updateChecksum, err := checksum.Open(
logE, c.fs, cfgFilePath,
cfg.ChecksumEnabled(param.EnforceChecksum, param.Checksum))
if err != nil {
return fmt.Errorf("read a checksum JSON: %w", err)
}
defer updateChecksum()

registryContents, err := c.registryInstaller.InstallRegistries(ctx, logE, cfg, cfgFilePath, checksums)
if err != nil {
Expand Down
21 changes: 6 additions & 15 deletions pkg/controller/which/which.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,13 @@ func (c *Controller) findExecFile(ctx context.Context, logE *logrus.Entry, param
return nil, err //nolint:wrapcheck
}

var checksums *checksum.Checksums
if cfg.ChecksumEnabled(param.EnforceChecksum, param.Checksum) {
checksums = checksum.New()
checksumFilePath, err := checksum.GetChecksumFilePathFromConfigFilePath(c.fs, cfgFilePath)
if err != nil {
return nil, err //nolint:wrapcheck
}
if err := checksums.ReadFile(c.fs, checksumFilePath); err != nil {
return nil, fmt.Errorf("read a checksum JSON: %w", err)
}
defer func() {
if err := checksums.UpdateFile(c.fs, checksumFilePath); err != nil {
logE.WithError(err).Error("update a checksum file")
}
}()
checksums, updateChecksum, err := checksum.Open(
logE, c.fs, cfgFilePath,
cfg.ChecksumEnabled(param.EnforceChecksum, param.Checksum))
if err != nil {
return nil, fmt.Errorf("read a checksum JSON: %w", err)
}
defer updateChecksum()

registryContents, err := c.registryInstaller.InstallRegistries(ctx, logE, cfg, cfgFilePath, checksums)
if err != nil {
Expand Down

0 comments on commit 1dd7fb7

Please sign in to comment.