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

refactor: refactor read config #3361

Merged
merged 3 commits into from
Dec 15, 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
3 changes: 0 additions & 3 deletions pkg/config-reader/go_version_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import (
var goVersionPattern = regexp.MustCompile(`(?m)^go (\d+\.\d+.\d+)$`)

func readGoVersionFile(fs afero.Fs, filePath string, pkg *aqua.Package) error {
if pkg.GoVersionFile == "" {
return nil
}
p := filepath.Join(filepath.Dir(filePath), pkg.GoVersionFile)
b, err := afero.ReadFile(fs, p)
if err != nil {
Expand Down
86 changes: 52 additions & 34 deletions pkg/config-reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ func (r *ConfigReader) Read(logE *logrus.Entry, configFilePath string, cfg *aqua
if err := yaml.NewDecoder(file).Decode(cfg); err != nil {
return fmt.Errorf("parse a configuration file as YAML: %w", err)
}
var configFileDir string
configFileDir := filepath.Dir(configFilePath)
if err := r.readRegistries(configFileDir, cfg); err != nil {
return err
}
r.readPackages(logE, configFilePath, cfg)
return nil
}

func (r *ConfigReader) readRegistries(configFileDir string, cfg *aqua.Config) error {
for _, rgst := range cfg.Registries {
if rgst.Type == "local" {
if strings.HasPrefix(rgst.Path, homePrefix) {
Expand All @@ -52,54 +60,64 @@ func (r *ConfigReader) Read(logE *logrus.Entry, configFilePath string, cfg *aqua
}
rgst.Path = filepath.Join(r.homeDir, rgst.Path[6:])
}
if configFileDir == "" {
configFileDir = filepath.Dir(configFilePath)
}
rgst.Path = osfile.Abs(configFileDir, rgst.Path)
}
}
r.readImports(logE, configFilePath, cfg)
return nil
}

func (r *ConfigReader) readImports(logE *logrus.Entry, configFilePath string, cfg *aqua.Config) {
func (r *ConfigReader) readPackages(logE *logrus.Entry, configFilePath string, cfg *aqua.Config) {
pkgs := []*aqua.Package{}
for _, pkg := range cfg.Packages {
if pkg == nil {
continue
}
if pkg.Import == "" {
if err := readGoVersionFile(r.fs, configFilePath, pkg); err != nil {
logerr.WithError(logE, err).Error("read a go version file")
continue
}
pkgs = append(pkgs, pkg)
continue
}
logE := logE.WithField("import", pkg.Import)
p := filepath.Join(filepath.Dir(configFilePath), pkg.Import)
filePaths, err := afero.Glob(r.fs, p)
subPkgs, err := r.readPackage(logE, configFilePath, pkg)
if err != nil {
logerr.WithError(logE, err).Error("read files with glob pattern")
logerr.WithError(logE, err).Error("read a package")
continue
}
sort.Strings(filePaths)
for _, filePath := range filePaths {
logE := logE.WithField("imported_file", filePath)
subCfg := &aqua.Config{}
if err := r.Read(logE, filePath, subCfg); err != nil {
logerr.WithError(logE, err).Error("read an import file")
continue
}
for _, pkg := range subCfg.Packages {
pkg.FilePath = filePath
if err := readGoVersionFile(r.fs, filePath, pkg); err != nil {
logerr.WithError(logE, err).Error("read a go version file")
continue
}
pkgs = append(pkgs, pkg)
}
if subPkgs == nil {
pkg.FilePath = configFilePath
pkgs = append(pkgs, pkg)
continue
}
pkgs = append(pkgs, subPkgs...)
}
cfg.Packages = pkgs
}

func (r *ConfigReader) readPackage(logE *logrus.Entry, configFilePath string, pkg *aqua.Package) ([]*aqua.Package, error) {
if pkg.GoVersionFile != "" {
// go_version_file
if err := readGoVersionFile(r.fs, configFilePath, pkg); err != nil {
return nil, fmt.Errorf("read a go version file: %w", logerr.WithFields(err, logrus.Fields{
"go_version_file": pkg.GoVersionFile,
}))
}
return nil, nil
}
if pkg.Import == "" {
// version
return nil, nil
}
// import
logE = logE.WithField("import", pkg.Import)
p := filepath.Join(filepath.Dir(configFilePath), pkg.Import)
filePaths, err := afero.Glob(r.fs, p)
if err != nil {
return nil, fmt.Errorf("find files with a glob pattern: %w", err)
}
sort.Strings(filePaths)
pkgs := []*aqua.Package{}
for _, filePath := range filePaths {
logE := logE.WithField("imported_file", filePath)
subCfg := &aqua.Config{}
if err := r.Read(logE, filePath, subCfg); err != nil {
logerr.WithError(logE, err).Error("read an import file")
continue
}
pkgs = append(pkgs, subCfg.Packages...)
}
return pkgs, nil
}
1 change: 1 addition & 0 deletions pkg/config-reader/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ packages:
Name: "suzuki-shunsuke/ci-info",
Registry: "standard",
Version: "v1.0.0",
FilePath: "/home/workspace/foo/aqua.yaml",
},
{
Name: "aquaproj/aqua-installer",
Expand Down
5 changes: 5 additions & 0 deletions pkg/controller/which/which_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ packages:
Name: "aquaproj/aqua-installer",
Registry: "standard",
Version: "v1.0.0",
FilePath: "/home/foo/workspace/aqua.yaml",
},
PackageInfo: &cfgRegistry.PackageInfo{
Type: "github_content",
Expand All @@ -92,6 +93,7 @@ packages:
Name: "aquaproj/aqua-installer",
Registry: "standard",
Version: "v1.0.0",
FilePath: "/home/foo/workspace/aqua.yaml",
},
},
Registries: aqua.Registries{
Expand Down Expand Up @@ -189,6 +191,7 @@ packages:
Name: "aquaproj/aqua-installer",
Registry: "standard",
Version: "v1.0.0",
FilePath: "/etc/aqua/aqua.yaml",
},
PackageInfo: &cfgRegistry.PackageInfo{
Type: "github_content",
Expand All @@ -211,11 +214,13 @@ packages:
Name: "suzuki-shunsuke/ci-info",
Registry: "standard",
Version: "v1.0.0",
FilePath: "/etc/aqua/aqua.yaml",
},
{
Name: "aquaproj/aqua-installer",
Registry: "standard",
Version: "v1.0.0",
FilePath: "/etc/aqua/aqua.yaml",
},
},
Registries: aqua.Registries{
Expand Down
Loading