-
Notifications
You must be signed in to change notification settings - Fork 12
/
configuration.go
122 lines (94 loc) · 2.65 KB
/
configuration.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package client
import (
"errors"
"fmt"
"io/ioutil"
"os"
"gopkg.in/yaml.v3"
"github.com/werf/trdl/client/pkg/util"
)
const configurationFileBasename = "config.yaml"
var errRepoConfigurationNotFound = errors.New("configuration not found")
type configuration struct {
Repositories []*RepoConfiguration `yaml:"repositories"`
configPath string
}
func newConfiguration(configPath string) (configuration, error) {
c := configuration{}
c.configPath = configPath
return c, c.load()
}
type RepoConfiguration struct {
Name string `yaml:"name"`
Url string `yaml:"url"`
DefaultChannel string `yaml:"defaultChannel"`
}
func newRepoConfiguration(name, url string) *RepoConfiguration {
return &RepoConfiguration{Name: name, Url: url}
}
func (c configuration) GetRepoConfigurationList() []*RepoConfiguration {
return c.Repositories
}
func (c configuration) GetRepoConfiguration(name string) *RepoConfiguration {
for i := range c.Repositories {
repo := c.Repositories[i]
if repo.Name == name {
return repo
}
}
return nil
}
func (c *configuration) RemoveRepoConfiguration(name string) error {
for i := len(c.Repositories) - 1; i >= 0; i-- {
repo := c.Repositories[i]
if repo.Name == name {
c.Repositories = append(c.Repositories[:i], c.Repositories[i+1:]...)
return nil
}
}
return fmt.Errorf("repo %q not registered", name)
}
func (c *configuration) StageRepoConfiguration(name, url string) {
repo := c.GetRepoConfiguration(name)
if repo == nil {
c.Repositories = append(c.Repositories, newRepoConfiguration(name, url))
return
}
repo.Url = url
}
func (c *configuration) StageRepoDefaultChannel(name, channel string) error {
repo := c.GetRepoConfiguration(name)
if repo == nil {
return errRepoConfigurationNotFound
}
repo.DefaultChannel = channel
return nil
}
func (c *configuration) Reload() error {
return c.load()
}
func (c *configuration) load() error {
if exist, err := util.IsRegularFileExist(c.configPath); err != nil {
return fmt.Errorf("unable to check existence of file %q: %w", c.configPath, err)
} else if !exist {
return nil
}
data, err := ioutil.ReadFile(c.configPath)
if err != nil {
return fmt.Errorf("unable to read file %q: %w", c.configPath, err)
}
if err := yaml.Unmarshal(data, &c); err != nil {
return fmt.Errorf("yaml unmarshalling failed: %w", err)
}
return nil
}
func (c configuration) Save(configPath string) error {
data, err := yaml.Marshal(&c)
if err != nil {
return fmt.Errorf("yaml marshalling failed: %w", err)
}
if err := ioutil.WriteFile(configPath, data, os.ModePerm); err != nil {
return fmt.Errorf("unable to write file %q: %w", configPath, err)
}
return nil
}