-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
77 lines (59 loc) · 1.31 KB
/
config.go
File metadata and controls
77 lines (59 loc) · 1.31 KB
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
package reql
import (
"errors"
"os"
"path/filepath"
"github.com/BurntSushi/toml"
)
type Config struct {
Root string `toml:"root"`
DefaultEnv string `toml:"default_env"`
Aliases map[string]string `toml:"aliases"`
Environments map[string]Env `toml:"environments"`
}
type Env map[string]string
func ParseConfig(path string) (*Config, error) {
if path == "" {
path = "./.reqrc"
}
var c Config
_, err := toml.DecodeFile(path, &c)
if os.IsNotExist(err) {
return defaultConfig(), nil
} else if err != nil {
return nil, err
}
for k, v := range c.Aliases {
c.Aliases[k] = filepath.Clean(v)
}
return &c, nil
}
func defaultConfig() *Config {
return &Config{
Aliases: map[string]string{},
Environments: map[string]Env{},
}
}
func (c *Config) NewEnv(env string) error {
if _, ok := c.Environments[env]; ok {
return errors.New("env already exists")
}
c.Environments[env] = make(Env)
return nil
}
func (c *Config) SetEnvValue(env, key, value string) error {
envMap, ok := c.Environments[env]
if !ok {
return errors.New("unknown env")
}
envMap[key] = value
return nil
}
func (c *Config) DeleteEnvValue(env, key string) error {
envMap, ok := c.Environments[env]
if !ok {
return errors.New("unknown env")
}
delete(envMap, key)
return nil
}