/
config.go
207 lines (166 loc) · 4.59 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package config
import (
"bytes"
"fmt"
"os"
"path/filepath"
"github.com/BurntSushi/toml"
"github.com/algolia/cli/pkg/utils"
"github.com/mitchellh/go-homedir"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
type IConfig interface {
InitConfig()
ConfiguredProfiles() []*Profile
ProfileNames() []string
ProfileExists(name string) bool
RemoveProfile(name string) error
SetDefaultProfile(name string) error
ApplicationIDExists(appID string) (bool, string)
Profile() *Profile
Default() *Profile
}
// Config handles all overall configuration for the CLI
type Config struct {
ApplicationName string
CurrentProfile Profile
File string
}
// InitConfig reads in profiles file and ENV variables if set.
func (c *Config) InitConfig() {
if c.File != "" {
viper.SetConfigFile(c.File)
} else {
configFolder := c.GetConfigFolder(os.Getenv("XDG_CONFIG_HOME"))
configFile := filepath.Join(configFolder, "config.toml")
c.File = configFile
viper.SetConfigType("toml")
viper.SetConfigFile(configFile)
viper.SetConfigPermissions(os.FileMode(0600))
// Try to change permissions manually, because we used to create files
// with default permissions (0644)
err := os.Chmod(configFile, os.FileMode(0600))
if err != nil && !os.IsNotExist(err) {
log.Fatalf("%s", err)
}
}
_ = viper.ReadInConfig()
}
// GetConfigFolder retrieves the folder where the configuration file is stored
// It searches for the xdg environment path first and will secondarily
// place it in the home directory
func (c *Config) GetConfigFolder(xdgPath string) string {
configPath := xdgPath
if configPath == "" {
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
configPath = filepath.Join(home, ".config")
}
return filepath.Join(configPath, "algolia")
}
// ConfiguredProfiles return the profiles in the configuration file
func (c *Config) ConfiguredProfiles() []*Profile {
configs := viper.AllSettings()
applications := make([]*Profile, 0, len(configs))
for appName := range configs {
app := &Profile{
Name: appName,
}
if err := viper.UnmarshalKey(appName, app); err != nil {
log.Fatalf("%s", err)
}
applications = append(applications, app)
}
return applications
}
// Profile returns the current profile
func (c *Config) Profile() *Profile {
return &c.CurrentProfile
}
// Default returns the default profile
func (c *Config) Default() *Profile {
for _, profile := range c.ConfiguredProfiles() {
if profile.Default {
return profile
}
}
return nil
}
// ProfileNames returns the list of name of the configured profiles
func (c *Config) ProfileNames() []string {
return viper.AllKeys()
}
// ProfileExists check if a profile with the given name exists
func (c *Config) ProfileExists(appName string) bool {
return viper.IsSet(appName)
}
// RemoveProfile remove a profile from the configuration
func (c *Config) RemoveProfile(name string) error {
runtimeViper := viper.GetViper()
configMap := runtimeViper.AllSettings()
delete(configMap, name)
buf := new(bytes.Buffer)
encodeErr := toml.NewEncoder(buf).Encode(configMap)
if encodeErr != nil {
return encodeErr
}
nv := viper.New()
nv.SetConfigType("toml") // hint to viper that we've encoded the data as toml
err := nv.ReadConfig(buf)
if err != nil {
return err
}
return c.write(nv)
}
// SetDefaultProfile set the default profile
func (c *Config) SetDefaultProfile(name string) error {
runtimeViper := viper.GetViper()
// Below is necessary if the config file was just created
runtimeViper.SetConfigType("toml")
err := runtimeViper.ReadInConfig()
if err != nil {
return err
}
configs := runtimeViper.AllSettings()
found := false
for profileName := range configs {
runtimeViper := viper.GetViper()
runtimeViper.Set(profileName+".default", false)
if profileName == name {
found = true
runtimeViper.Set(profileName+".default", true)
}
}
if !found {
return fmt.Errorf("profile '%s' not found", name)
}
return c.write(runtimeViper)
}
// ApplicationIDExists check if an application ID exists in any profiles
func (c *Config) ApplicationIDExists(appID string) (bool, string) {
for _, profile := range c.ConfiguredProfiles() {
if profile.ApplicationID == appID {
return true, profile.Name
}
}
return false, ""
}
// write writes the configuration file
func (c *Config) write(runtimeViper *viper.Viper) error {
configFile := viper.ConfigFileUsed()
err := utils.MakePath(configFile)
if err != nil {
return err
}
runtimeViper.SetConfigFile(configFile)
runtimeViper.SetConfigType(filepath.Ext(configFile))
err = runtimeViper.WriteConfig()
if err != nil {
return err
}
return nil
}