-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconfig.go
More file actions
170 lines (154 loc) · 4.6 KB
/
config.go
File metadata and controls
170 lines (154 loc) · 4.6 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
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
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/binsquare/envmap/provider"
"gopkg.in/yaml.v3"
)
type ProjectConfig struct {
Project string `yaml:"project"`
DefaultEnv string `yaml:"default_env"`
Envs map[string]EnvConfig `yaml:"envs"`
}
type EnvConfig struct {
Provider string `yaml:"provider"`
Source string `yaml:"source,omitempty"` // deprecated, use Provider
PathPrefix string `yaml:"path_prefix"`
Prefix string `yaml:"prefix"`
Mapping map[string]provider.SecretMapping `yaml:"mapping,omitempty"`
}
func (e EnvConfig) GetProvider() string {
if e.Provider != "" {
return e.Provider
}
return e.Source
}
func (e EnvConfig) ToProviderConfig() provider.EnvConfig {
return provider.EnvConfig{
Provider: e.GetProvider(),
PathPrefix: e.PathPrefix,
Prefix: e.Prefix,
Mapping: e.Mapping,
}
}
type GlobalConfig struct {
Providers map[string]provider.ProviderConfig `yaml:"providers"`
Sources map[string]provider.ProviderConfig `yaml:"sources,omitempty"` // deprecated
}
func (g GlobalConfig) GetProviders() map[string]provider.ProviderConfig {
if len(g.Providers) > 0 {
return g.Providers
}
return g.Sources
}
func LoadProjectConfig(path string) (ProjectConfig, error) {
raw, err := os.ReadFile(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return ProjectConfig{}, fmt.Errorf("no project config found at %s. Run: envmap init", path)
}
return ProjectConfig{}, fmt.Errorf("read project config: %w", err)
}
var cfg ProjectConfig
if err := yaml.Unmarshal(raw, &cfg); err != nil {
return ProjectConfig{}, fmt.Errorf("parse project config: %w", err)
}
if err := cfg.Validate(); err != nil {
return ProjectConfig{}, err
}
return cfg, nil
}
func LoadGlobalConfig(path string) (GlobalConfig, error) {
if path == "" {
path = DefaultGlobalConfigPath()
}
raw, err := os.ReadFile(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return GlobalConfig{}, fmt.Errorf("no global config found at %s. Run: envmap init --global", path)
}
return GlobalConfig{}, fmt.Errorf("read global config: %w", err)
}
var cfg GlobalConfig
if err := yaml.Unmarshal(raw, &cfg); err != nil {
return GlobalConfig{}, fmt.Errorf("parse global config: %w", err)
}
if len(cfg.GetProviders()) == 0 {
return GlobalConfig{}, errors.New("no providers configured in global config; run envmap init --global to add one")
}
return cfg, nil
}
func (c ProjectConfig) Validate() error {
if c.Project == "" {
return errors.New("project config missing project name")
}
if len(c.Envs) == 0 {
return errors.New("project config has no envs defined")
}
if c.DefaultEnv == "" {
return errors.New("project config missing default_env")
}
if _, ok := c.Envs[c.DefaultEnv]; !ok {
return fmt.Errorf("default_env %q not found in envs", c.DefaultEnv)
}
for envName, envCfg := range c.Envs {
if len(envCfg.Mapping) > 0 && envCfg.PathPrefix != "" {
return fmt.Errorf("env %q: mapping and path_prefix are mutually exclusive", envName)
}
}
return nil
}
func ResolveEnv(cfg ProjectConfig, requested string) (string, error) {
if requested != "" {
if _, ok := cfg.Envs[requested]; ok {
return requested, nil
}
return "", fmt.Errorf("unknown env %q; valid envs: %s", requested, joinEnvKeys(cfg))
}
return cfg.DefaultEnv, nil
}
func DefaultProjectConfigPath() string {
return ".envmap.yaml"
}
// FindProjectConfig walks up from startDir to locate a .envmap.yaml.
func FindProjectConfig(startDir string) (string, error) {
if startDir == "" {
var err error
startDir, err = os.Getwd()
if err != nil {
return "", fmt.Errorf("determine working directory: %w", err)
}
}
dir, err := filepath.Abs(startDir)
if err != nil {
return "", fmt.Errorf("resolve path: %w", err)
}
for {
candidate := filepath.Join(dir, DefaultProjectConfigPath())
if info, err := os.Stat(candidate); err == nil && !info.IsDir() {
return candidate, nil
}
parent := filepath.Dir(dir)
if parent == dir {
break
}
dir = parent
}
return "", fmt.Errorf("no project config (.envmap.yaml) found starting from %s", startDir)
}
func DefaultGlobalConfigPath() string {
home, err := os.UserHomeDir()
if err != nil {
return filepath.Join(string(os.PathSeparator), "unknown", ".envmap", "config.yaml")
}
return filepath.Join(home, ".envmap", "config.yaml")
}
func joinEnvKeys(cfg ProjectConfig) string {
keys := make([]string, 0, len(cfg.Envs))
for k := range cfg.Envs {
keys = append(keys, k)
}
return fmt.Sprintf("%v", keys)
}