Skip to content

Commit

Permalink
refactor: simplify update a checksum file (#3493)
Browse files Browse the repository at this point in the history
* refactor: simplify update a checksum file

* refactor: fix lint errors

* refactor: simplify

* refactor: simplify
  • Loading branch information
suzuki-shunsuke authored Jan 31, 2025
1 parent d9d49de commit eeb2567
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 137 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
}
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
5 changes: 5 additions & 0 deletions pkg/config/checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ import (
"net/url"
"path"

"github.com/aquaproj/aqua/v2/pkg/config/aqua"
"github.com/aquaproj/aqua/v2/pkg/runtime"
"github.com/aquaproj/aqua/v2/pkg/template"
)

var errUnknownChecksumFileType = errors.New("unknown checksum type")

func (p *Param) ChecksumEnabled(cfg *aqua.Config) bool {
return cfg.ChecksumEnabled(p.EnforceChecksum, p.Checksum)
}

func (p *Package) ChecksumID(rt *runtime.Runtime) (string, error) {
assetName, err := p.RenderAsset(rt)
if err != nil {
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,
param.ChecksumEnabled(findResult.Config))
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,
param.ChecksumEnabled(findResult.Config))
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
20 changes: 5 additions & 15 deletions pkg/controller/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,12 @@ 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, param.ChecksumEnabled(cfg))
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
20 changes: 5 additions & 15 deletions pkg/controller/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,12 @@ 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, param.ChecksumEnabled(cfg))
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
23 changes: 7 additions & 16 deletions pkg/controller/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/sirupsen/logrus"
)

func (c *Controller) List(ctx context.Context, logE *logrus.Entry, param *config.Param) error { //nolint:cyclop
func (c *Controller) List(ctx context.Context, logE *logrus.Entry, param *config.Param) error {
if param.Installed {
return c.listInstalled(logE, param)
}
Expand All @@ -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,
param.ChecksumEnabled(cfg))
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,
param.ChecksumEnabled(cfg))
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,
param.ChecksumEnabled(cfg))
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
23 changes: 7 additions & 16 deletions pkg/controller/vacuum/initialize/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c *Controller) Init(ctx context.Context, logE *logrus.Entry, param *config
return nil
}

func (c *Controller) create(ctx context.Context, logE *logrus.Entry, cfgFilePath string, param *config.Param) error { //nolint:cyclop
func (c *Controller) create(ctx context.Context, logE *logrus.Entry, cfgFilePath string, param *config.Param) error {
cfg := &aqua.Config{}
if cfgFilePath == "" {
return finder.ErrConfigFileNotFound
Expand All @@ -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,
param.ChecksumEnabled(cfg))
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,
param.ChecksumEnabled(cfg))
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 eeb2567

Please sign in to comment.