From eeb2567dc7515cad0b4003c68ac1aab19fbe3bbe Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Sat, 1 Feb 2025 08:25:30 +0900 Subject: [PATCH] refactor: simplify update a checksum file (#3493) * refactor: simplify update a checksum file * refactor: fix lint errors * refactor: simplify * refactor: simplify --- pkg/checksum/type.go | 20 ++++++++++++++++++++ pkg/config/checksum.go | 5 +++++ pkg/controller/cp/install.go | 21 ++++++--------------- pkg/controller/exec/exec.go | 21 ++++++--------------- pkg/controller/generate/generate.go | 20 +++++--------------- pkg/controller/install/install.go | 20 +++++--------------- pkg/controller/list/list.go | 23 +++++++---------------- pkg/controller/remove/remove.go | 21 ++++++--------------- pkg/controller/update/update.go | 21 ++++++--------------- pkg/controller/vacuum/initialize/init.go | 23 +++++++---------------- pkg/controller/which/which.go | 21 ++++++--------------- 11 files changed, 79 insertions(+), 137 deletions(-) diff --git a/pkg/checksum/type.go b/pkg/checksum/type.go index 5f11865e2..e4c5d99c3 100644 --- a/pkg/checksum/type.go +++ b/pkg/checksum/type.go @@ -10,6 +10,7 @@ import ( "strings" "sync" + "github.com/sirupsen/logrus" "github.com/spf13/afero" ) @@ -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 } diff --git a/pkg/config/checksum.go b/pkg/config/checksum.go index 4664ac32c..ba80e9e46 100644 --- a/pkg/config/checksum.go +++ b/pkg/config/checksum.go @@ -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 { diff --git a/pkg/controller/cp/install.go b/pkg/controller/cp/install.go index 432b1ef55..cbf503c85 100644 --- a/pkg/controller/cp/install.go +++ b/pkg/controller/cp/install.go @@ -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, diff --git a/pkg/controller/exec/exec.go b/pkg/controller/exec/exec.go index 2de3dd498..033a42576 100644 --- a/pkg/controller/exec/exec.go +++ b/pkg/controller/exec/exec.go @@ -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, diff --git a/pkg/controller/generate/generate.go b/pkg/controller/generate/generate.go index a80be9a2b..50a42f6c2 100644 --- a/pkg/controller/generate/generate.go +++ b/pkg/controller/generate/generate.go @@ -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 { diff --git a/pkg/controller/install/install.go b/pkg/controller/install/install.go index ef1d8a88e..992ed5c93 100644 --- a/pkg/controller/install/install.go +++ b/pkg/controller/install/install.go @@ -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 { diff --git a/pkg/controller/list/list.go b/pkg/controller/list/list.go index 2e263db94..0016e1c92 100644 --- a/pkg/controller/list/list.go +++ b/pkg/controller/list/list.go @@ -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) } @@ -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 { diff --git a/pkg/controller/remove/remove.go b/pkg/controller/remove/remove.go index 94c807a07..de74ac09a 100644 --- a/pkg/controller/remove/remove.go +++ b/pkg/controller/remove/remove.go @@ -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 { diff --git a/pkg/controller/update/update.go b/pkg/controller/update/update.go index 15a8293da..cee8f3d2e 100644 --- a/pkg/controller/update/update.go +++ b/pkg/controller/update/update.go @@ -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. diff --git a/pkg/controller/vacuum/initialize/init.go b/pkg/controller/vacuum/initialize/init.go index 6309e5cc4..7eaf2d778 100644 --- a/pkg/controller/vacuum/initialize/init.go +++ b/pkg/controller/vacuum/initialize/init.go @@ -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 @@ -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 { diff --git a/pkg/controller/which/which.go b/pkg/controller/which/which.go index a97b58711..7e31ec64b 100644 --- a/pkg/controller/which/which.go +++ b/pkg/controller/which/which.go @@ -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 {