-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
93 lines (83 loc) · 2.02 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
88
89
90
91
92
93
package addon
import (
"fmt"
"io/ioutil"
"path/filepath"
"github.com/jenkins-x/jx/pkg/util"
"gopkg.in/yaml.v2"
)
//AddonConfig Addon Configration
type AddonConfig struct {
Name string
Enabled bool
}
//AddonsConfig Addons Configuration
type AddonsConfig struct {
Addons []*AddonConfig
}
// LoadAddonsConfig loads the addons configuration from the `~/.jx/addon.yml` file if it exists
func LoadAddonsConfig() (*AddonsConfig, error) {
var config *AddonsConfig
fileName, err := addonConfigFileName()
if err != nil {
return config, err
}
exists, err := util.FileExists(fileName)
if err != nil {
return config, err
}
config = &AddonsConfig{}
if !exists {
return config, nil
}
data, err := ioutil.ReadFile(fileName)
if err != nil {
return config, fmt.Errorf("Failed to load file %s due to %s", fileName, err)
}
err = yaml.Unmarshal(data, &config)
if err != nil {
return config, fmt.Errorf("Failed to unmarshal YAML file %s due to %s", fileName, err)
}
return config, nil
}
// IsAddonEnabled returns true if the given addon is enabled
func IsAddonEnabled(name string) bool {
configs, err := LoadAddonsConfig()
if err != nil {
return false
}
return configs.GetOrCreate(name).Enabled
}
// Save saves the addons configuration to the `~/.jx/addon.yml` file
func (c *AddonsConfig) Save() error {
fileName, err := addonConfigFileName()
if err != nil {
return err
}
data, err := yaml.Marshal(c)
if err != nil {
return err
}
return ioutil.WriteFile(fileName, data, util.DefaultWritePermissions)
}
// GetOrCreate returns the addon configuration for the given name or creates a new config object
func (c *AddonsConfig) GetOrCreate(name string) *AddonConfig {
for _, addon := range c.Addons {
if addon.Name == name {
return addon
}
}
answer := &AddonConfig{
Name: name,
}
c.Addons = append(c.Addons, answer)
return answer
}
func addonConfigFileName() (string, error) {
dir, err := util.ConfigDir()
if err != nil {
return "", err
}
fileName := filepath.Join(dir, "addons.yml")
return fileName, nil
}