-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
config.go
87 lines (71 loc) · 2.13 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package controller
import (
"errors"
"fmt"
"io"
"os"
"github.com/suzuki-shunsuke/go-findconfig/findconfig"
"github.com/suzuki-shunsuke/go-template-unmarshaler/text"
"gopkg.in/yaml.v2"
)
type Config struct {
Packages []*Package `validate:"dive"`
InlineRepository []*PackageInfo `yaml:"inline_repository" validate:"dive"`
}
type Package struct {
Name string `validate:"required"`
Repository string `validate:"required"`
Version string `validate:"required"`
}
type PackageInfo struct {
Name string `validate:"required"`
Type string `validate:"required"`
RepoOwner string `yaml:"repo_owner" validate:"required"`
RepoName string `yaml:"repo_name" validate:"required"`
Artifact *text.Template `validate:"required"`
ArchiveType string `yaml:"archive_type"`
Files []*File `validate:"required,dive"`
}
type File struct {
Name string `validate:"required"`
Src *text.Template
}
type Param struct {
ConfigFilePath string
LogLevel string
OnlyLink bool
}
var errConfigFileNotFound = errors.New("configuration file isn't found")
func (ctrl *Controller) getConfigFilePath(wd, configFilePath string) string {
if configFilePath != "" {
return configFilePath
}
return ctrl.ConfigFinder.Find(wd)
}
func (ctrl *Controller) readConfig(configFilePath string, cfg *Config) error {
file, err := ctrl.ConfigReader.Read(configFilePath)
if err != nil {
return err //nolint:wrapcheck
}
defer file.Close()
decoder := yaml.NewDecoder(file)
decoder.SetStrict(true)
if err := decoder.Decode(&cfg); err != nil {
return fmt.Errorf("parse a configuration file as YAML %s: %w", configFilePath, err)
}
return nil
}
type ConfigFinder interface {
Find(wd string) string
}
type configFinder struct{}
func (finder *configFinder) Find(wd string) string {
return findconfig.Find(wd, findconfig.Exist, "aqua.yaml", "aqua.yml", ".aqua.yaml", ".aqua.yml")
}
type ConfigReader interface {
Read(p string) (io.ReadCloser, error)
}
type configReader struct{}
func (reader *configReader) Read(p string) (io.ReadCloser, error) {
return os.Open(p) //nolint:wrapcheck
}