Skip to content

Commit 54a5885

Browse files
refactor: refactor read config (#3361)
* refactor: refactor read config * fix: fix lint errors * fix: set pkg.FilePath
1 parent 05764f8 commit 54a5885

File tree

4 files changed

+58
-37
lines changed

4 files changed

+58
-37
lines changed

pkg/config-reader/go_version_file.go

-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ import (
1515
var goVersionPattern = regexp.MustCompile(`(?m)^go (\d+\.\d+.\d+)$`)
1616

1717
func readGoVersionFile(fs afero.Fs, filePath string, pkg *aqua.Package) error {
18-
if pkg.GoVersionFile == "" {
19-
return nil
20-
}
2118
p := filepath.Join(filepath.Dir(filePath), pkg.GoVersionFile)
2219
b, err := afero.ReadFile(fs, p)
2320
if err != nil {

pkg/config-reader/reader.go

+52-34
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,15 @@ func (r *ConfigReader) Read(logE *logrus.Entry, configFilePath string, cfg *aqua
4343
if err := yaml.NewDecoder(file).Decode(cfg); err != nil {
4444
return fmt.Errorf("parse a configuration file as YAML: %w", err)
4545
}
46-
var configFileDir string
46+
configFileDir := filepath.Dir(configFilePath)
47+
if err := r.readRegistries(configFileDir, cfg); err != nil {
48+
return err
49+
}
50+
r.readPackages(logE, configFilePath, cfg)
51+
return nil
52+
}
53+
54+
func (r *ConfigReader) readRegistries(configFileDir string, cfg *aqua.Config) error {
4755
for _, rgst := range cfg.Registries {
4856
if rgst.Type == "local" {
4957
if strings.HasPrefix(rgst.Path, homePrefix) {
@@ -52,54 +60,64 @@ func (r *ConfigReader) Read(logE *logrus.Entry, configFilePath string, cfg *aqua
5260
}
5361
rgst.Path = filepath.Join(r.homeDir, rgst.Path[6:])
5462
}
55-
if configFileDir == "" {
56-
configFileDir = filepath.Dir(configFilePath)
57-
}
5863
rgst.Path = osfile.Abs(configFileDir, rgst.Path)
5964
}
6065
}
61-
r.readImports(logE, configFilePath, cfg)
6266
return nil
6367
}
6468

65-
func (r *ConfigReader) readImports(logE *logrus.Entry, configFilePath string, cfg *aqua.Config) {
69+
func (r *ConfigReader) readPackages(logE *logrus.Entry, configFilePath string, cfg *aqua.Config) {
6670
pkgs := []*aqua.Package{}
6771
for _, pkg := range cfg.Packages {
6872
if pkg == nil {
6973
continue
7074
}
71-
if pkg.Import == "" {
72-
if err := readGoVersionFile(r.fs, configFilePath, pkg); err != nil {
73-
logerr.WithError(logE, err).Error("read a go version file")
74-
continue
75-
}
76-
pkgs = append(pkgs, pkg)
77-
continue
78-
}
79-
logE := logE.WithField("import", pkg.Import)
80-
p := filepath.Join(filepath.Dir(configFilePath), pkg.Import)
81-
filePaths, err := afero.Glob(r.fs, p)
75+
subPkgs, err := r.readPackage(logE, configFilePath, pkg)
8276
if err != nil {
83-
logerr.WithError(logE, err).Error("read files with glob pattern")
77+
logerr.WithError(logE, err).Error("read a package")
8478
continue
8579
}
86-
sort.Strings(filePaths)
87-
for _, filePath := range filePaths {
88-
logE := logE.WithField("imported_file", filePath)
89-
subCfg := &aqua.Config{}
90-
if err := r.Read(logE, filePath, subCfg); err != nil {
91-
logerr.WithError(logE, err).Error("read an import file")
92-
continue
93-
}
94-
for _, pkg := range subCfg.Packages {
95-
pkg.FilePath = filePath
96-
if err := readGoVersionFile(r.fs, filePath, pkg); err != nil {
97-
logerr.WithError(logE, err).Error("read a go version file")
98-
continue
99-
}
100-
pkgs = append(pkgs, pkg)
101-
}
80+
if subPkgs == nil {
81+
pkg.FilePath = configFilePath
82+
pkgs = append(pkgs, pkg)
83+
continue
10284
}
85+
pkgs = append(pkgs, subPkgs...)
10386
}
10487
cfg.Packages = pkgs
10588
}
89+
90+
func (r *ConfigReader) readPackage(logE *logrus.Entry, configFilePath string, pkg *aqua.Package) ([]*aqua.Package, error) {
91+
if pkg.GoVersionFile != "" {
92+
// go_version_file
93+
if err := readGoVersionFile(r.fs, configFilePath, pkg); err != nil {
94+
return nil, fmt.Errorf("read a go version file: %w", logerr.WithFields(err, logrus.Fields{
95+
"go_version_file": pkg.GoVersionFile,
96+
}))
97+
}
98+
return nil, nil
99+
}
100+
if pkg.Import == "" {
101+
// version
102+
return nil, nil
103+
}
104+
// import
105+
logE = logE.WithField("import", pkg.Import)
106+
p := filepath.Join(filepath.Dir(configFilePath), pkg.Import)
107+
filePaths, err := afero.Glob(r.fs, p)
108+
if err != nil {
109+
return nil, fmt.Errorf("find files with a glob pattern: %w", err)
110+
}
111+
sort.Strings(filePaths)
112+
pkgs := []*aqua.Package{}
113+
for _, filePath := range filePaths {
114+
logE := logE.WithField("imported_file", filePath)
115+
subCfg := &aqua.Config{}
116+
if err := r.Read(logE, filePath, subCfg); err != nil {
117+
logerr.WithError(logE, err).Error("read an import file")
118+
continue
119+
}
120+
pkgs = append(pkgs, subCfg.Packages...)
121+
}
122+
return pkgs, nil
123+
}

pkg/config-reader/reader_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ packages:
8787
Name: "suzuki-shunsuke/ci-info",
8888
Registry: "standard",
8989
Version: "v1.0.0",
90+
FilePath: "/home/workspace/foo/aqua.yaml",
9091
},
9192
{
9293
Name: "aquaproj/aqua-installer",

pkg/controller/which/which_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ packages:
7070
Name: "aquaproj/aqua-installer",
7171
Registry: "standard",
7272
Version: "v1.0.0",
73+
FilePath: "/home/foo/workspace/aqua.yaml",
7374
},
7475
PackageInfo: &cfgRegistry.PackageInfo{
7576
Type: "github_content",
@@ -92,6 +93,7 @@ packages:
9293
Name: "aquaproj/aqua-installer",
9394
Registry: "standard",
9495
Version: "v1.0.0",
96+
FilePath: "/home/foo/workspace/aqua.yaml",
9597
},
9698
},
9799
Registries: aqua.Registries{
@@ -189,6 +191,7 @@ packages:
189191
Name: "aquaproj/aqua-installer",
190192
Registry: "standard",
191193
Version: "v1.0.0",
194+
FilePath: "/etc/aqua/aqua.yaml",
192195
},
193196
PackageInfo: &cfgRegistry.PackageInfo{
194197
Type: "github_content",
@@ -211,11 +214,13 @@ packages:
211214
Name: "suzuki-shunsuke/ci-info",
212215
Registry: "standard",
213216
Version: "v1.0.0",
217+
FilePath: "/etc/aqua/aqua.yaml",
214218
},
215219
{
216220
Name: "aquaproj/aqua-installer",
217221
Registry: "standard",
218222
Version: "v1.0.0",
223+
FilePath: "/etc/aqua/aqua.yaml",
219224
},
220225
},
221226
Registries: aqua.Registries{

0 commit comments

Comments
 (0)