-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support initializing timestamp files
- Loading branch information
1 parent
c4a67f2
commit cc1859f
Showing
6 changed files
with
238 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package initialize | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/aquaproj/aqua/v2/pkg/checksum" | ||
"github.com/aquaproj/aqua/v2/pkg/config" | ||
"github.com/aquaproj/aqua/v2/pkg/config/aqua" | ||
"github.com/aquaproj/aqua/v2/pkg/config/registry" | ||
"github.com/aquaproj/aqua/v2/pkg/runtime" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/afero" | ||
) | ||
|
||
type Controller struct { | ||
rootDir string | ||
runtime *runtime.Runtime | ||
fs afero.Fs | ||
vacuum Vacuum | ||
configFinder ConfigFinder | ||
configReader ConfigReader | ||
registryInstaller RegistryInstaller | ||
} | ||
|
||
func New(param *config.Param, rt *runtime.Runtime, fs afero.Fs, vc Vacuum, configFinder ConfigFinder, configReader ConfigReader, registryInstaller RegistryInstaller) *Controller { | ||
return &Controller{ | ||
rootDir: param.RootDir, | ||
runtime: rt, | ||
fs: fs, | ||
vacuum: vc, | ||
configFinder: configFinder, | ||
configReader: configReader, | ||
registryInstaller: registryInstaller, | ||
} | ||
} | ||
|
||
type Vacuum interface { | ||
Create(pkgPath string, timestamp time.Time) error | ||
} | ||
|
||
type ConfigReader interface { | ||
Read(logE *logrus.Entry, configFilePath string, cfg *aqua.Config) error | ||
} | ||
|
||
type ConfigFinder interface { | ||
Finds(wd, configFilePath string) []string | ||
} | ||
|
||
type RegistryInstaller interface { | ||
InstallRegistries(ctx context.Context, logE *logrus.Entry, cfg *aqua.Config, cfgFilePath string, checksums *checksum.Checksums) (map[string]*registry.Config, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package initialize | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/aquaproj/aqua/v2/pkg/checksum" | ||
"github.com/aquaproj/aqua/v2/pkg/config" | ||
finder "github.com/aquaproj/aqua/v2/pkg/config-finder" | ||
"github.com/aquaproj/aqua/v2/pkg/config/aqua" | ||
"github.com/sirupsen/logrus" | ||
"github.com/suzuki-shunsuke/logrus-error/logerr" | ||
) | ||
|
||
func (c *Controller) Init(ctx context.Context, logE *logrus.Entry, param *config.Param) error { | ||
for _, cfgFilePath := range c.configFinder.Finds(param.PWD, param.ConfigFilePath) { | ||
if err := c.create(ctx, logE, cfgFilePath, param); err != nil { | ||
return err | ||
} | ||
} | ||
for _, cfgFilePath := range param.GlobalConfigFilePaths { | ||
if _, err := c.fs.Stat(cfgFilePath); err != nil { | ||
continue | ||
} | ||
if err := c.create(ctx, logE, cfgFilePath, param); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (c *Controller) create(ctx context.Context, logE *logrus.Entry, cfgFilePath string, param *config.Param) error { //nolint:cyclop | ||
cfg := &aqua.Config{} | ||
if cfgFilePath == "" { | ||
return finder.ErrConfigFileNotFound | ||
} | ||
if err := c.configReader.Read(logE, cfgFilePath, cfg); err != nil { | ||
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") | ||
} | ||
}() | ||
} | ||
|
||
registryContents, err := c.registryInstaller.InstallRegistries(ctx, logE, cfg, cfgFilePath, checksums) | ||
if err != nil { | ||
return err //nolint:wrapcheck | ||
} | ||
|
||
pkgs, _ := config.ListPackages(logE, cfg, c.runtime, registryContents) | ||
now := time.Now() | ||
for _, pkg := range pkgs { | ||
pkgPath, err := pkg.PkgPath(c.runtime) | ||
if err != nil { | ||
logerr.WithError(logE, err).Warn("get a package path") | ||
continue | ||
} | ||
if err := c.vacuum.Create(pkgPath, now); err != nil { | ||
logerr.WithError(logE, err).Warn("create a timestamp file") | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters