-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
68 lines (49 loc) · 1.39 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
package config
import (
"time"
)
type Config struct {
Debug bool `json:"debug" yaml:"debug"`
OutputFormat string `json:"outputFormat,omitempty" yaml:"outputFormat,omitempty"`
Timeout time.Duration `json:"timeout,omitempty" yaml:"timeout,omitempty"`
DefaultProfile string `json:"defaultProfile,omitempty" yaml:"defaultProfile,omitempty"`
Profiles NamedProfiles `json:"profiles,omitempty" yaml:"profiles,omitempty"`
}
func (config *Config) setDefaultProfile(name string) error {
if _, ok := config.Profiles[name]; !ok {
return ErrUnknownProfile
}
config.DefaultProfile = name
return nil
}
func (config *Config) getProfile(name string) (*Profile, error) {
profile, ok := config.Profiles[name]
if !ok {
return nil, ErrUnknownProfile
}
return &profile, nil
}
func (config *Config) addProfile(profile Profile) error {
name := profile.Name
if config.Profiles == nil {
config.Profiles = NamedProfiles{}
} else if _, ok := config.Profiles[name]; ok {
return ErrDuplicateProfile
}
config.Profiles[name] = profile
return nil
}
func (config *Config) removeProfile(name string) error {
delete(config.Profiles, name)
if config.DefaultProfile == name {
config.DefaultProfile = ""
}
return nil
}
func (config *Config) profileNames() []string {
profiles := []string{}
for name := range config.Profiles {
profiles = append(profiles, name)
}
return profiles
}