/
config.go
203 lines (170 loc) · 5.85 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
package config
import (
"fmt"
"os"
"path/filepath"
"github.com/astronomerio/astro-cli/messages"
"github.com/astronomerio/astro-cli/pkg/fileutil"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
var (
// ConfigFileName is the name of the config files (home / project)
ConfigFileName = "config"
// ConfigFileType is the config file extension
ConfigFileType = "yaml"
// ConfigFileNameWithExt is the config filename with extension
ConfigFileNameWithExt = fmt.Sprintf("%s.%s", ConfigFileName, ConfigFileType)
// ConfigDir is the directory for astro files
ConfigDir = ".astro"
// HomePath is the path to a users home directory
HomePath, _ = fileutil.GetHomeDir()
// HomeConfigPath is the path to the users global config directory
HomeConfigPath = filepath.Join(HomePath, ConfigDir)
// HomeConfigFile is the global config file
HomeConfigFile = filepath.Join(HomeConfigPath, ConfigFileNameWithExt)
// WorkingPath is the path to the working directory
WorkingPath, _ = fileutil.GetWorkingDir()
// CFGStrMap maintains string to cfg mapping
CFGStrMap = make(map[string]cfg)
// CFG Houses configuration meta
CFG = cfgs{
Contexts: newCfg("contexts", ""),
CloudAPIProtocol: newCfg("cloud.api.protocol", "https"),
CloudAPIPort: newCfg("cloud.api.port", "443"),
CloudAPIToken: newCfg("cloud.api.token", ""),
Context: newCfg("context", ""),
LocalHouston: newCfg("local.houston", ""),
LocalOrbit: newCfg("local.orbit", ""),
PostgresUser: newCfg("postgres.user", "postgres"),
PostgresPassword: newCfg("postgres.password", "postgres"),
PostgresHost: newCfg("postgres.host", "postgres"),
PostgresPort: newCfg("postgres.port", "5432"),
ProjectDeployment: newCfg("project.deployment", ""),
ProjectName: newCfg("project.name", ""),
ProjectWorkspace: newCfg("project.workspace", ""),
}
// viperHome is the viper object in the users home directory
viperHome *viper.Viper
// viperProject is the viper object in a project directory
viperProject *viper.Viper
)
// InitConfig initializes the config files
func InitConfig() {
initHome()
initProject()
}
// Init viper for config file in home directory
func initHome() {
viperHome = viper.New()
viperHome.SetConfigName(ConfigFileName)
viperHome.SetConfigType(ConfigFileType)
viperHome.SetConfigFile(HomeConfigFile)
for _, cfg := range CFGStrMap {
if len(cfg.Default) > 0 {
viperHome.SetDefault(cfg.Path, cfg.Default)
}
}
// If home config does not exist, create it
homeConfigExists, _ := fileutil.Exists(HomeConfigFile)
if !homeConfigExists {
err := CreateConfig(viperHome, HomeConfigPath, HomeConfigFile)
if err != nil {
fmt.Printf(messages.CONFIG_CREATE_HOME_ERROR, err)
return
}
}
// Read in home config
err := viperHome.ReadInConfig()
if err != nil {
fmt.Printf(messages.CONFIG_READ_ERROR, err)
return
}
}
// Init viper for config file in project directory
// If project config does not exist, just exit
func initProject() {
// Set up viper object for project config
viperProject = viper.New()
viperProject.SetConfigName(ConfigFileName)
viperProject.SetConfigType(ConfigFileType)
// Construct the path to the config file
workingConfigPath := filepath.Join(WorkingPath, ConfigDir)
workingConfigFile := filepath.Join(workingConfigPath, ConfigFileNameWithExt)
// If path is empty or config file does not exist, just return
workingConfigExists, _ := fileutil.Exists(workingConfigFile)
if len(workingConfigPath) == 0 || workingConfigPath == HomeConfigPath || !workingConfigExists {
return
}
// Add the path we discovered
viperProject.SetConfigFile(workingConfigFile)
// Read in project config
readErr := viperProject.ReadInConfig()
if readErr != nil {
fmt.Printf(messages.CONFIG_READ_ERROR, readErr)
}
}
// CreateProjectConfig creates a project config file
func CreateProjectConfig(projectPath string) {
projectConfigDir := filepath.Join(projectPath, ConfigDir)
projectConfigFile := filepath.Join(projectConfigDir, ConfigFileNameWithExt)
err := CreateConfig(viperProject, projectConfigDir, projectConfigFile)
if err != nil {
fmt.Printf(messages.CONFIG_CREATE_HOME_ERROR, err)
return
}
// Add the new file
viperProject.SetConfigFile(projectConfigFile)
}
// configExists returns a boolean indicating if the config is backed by a file
func configExists(v *viper.Viper) bool {
return len(v.ConfigFileUsed()) > 0
}
// CreateConfig creates a config file in the given directory
func CreateConfig(v *viper.Viper, path, file string) error {
err := os.MkdirAll(path, 0770)
if err != nil {
return errors.Wrap(err, messages.CONFIG_CREATE_DIR_ERROR)
}
_, err = os.Create(file)
if err != nil {
return errors.Wrap(err, messages.CONFIG_CREATE_FILE_ERROR)
}
os.Chmod(file, 0600)
return saveConfig(v, file)
}
// ProjectConfigExists returns a boolean indicating if a project config file exists
func ProjectConfigExists() bool {
return configExists(viperProject)
}
// ProjectRoot returns the path to the nearest project root
// TODO Deprecate if remains unused, removed due to
// https://github.com/astronomerio/astro-cli/issues/103
func ProjectRoot() (string, error) {
configPath, searchErr := fileutil.FindDirInPath(ConfigDir)
if searchErr != nil {
return "", searchErr
}
if len(configPath) == 0 {
return "", nil
}
return filepath.Dir(configPath), nil
}
// IsProjectDir returns a boolean depending on if path is a valid project dir
func IsProjectDir(path string) (bool, error) {
configPath := filepath.Join(path, ConfigDir)
configFile := filepath.Join(configPath, ConfigFileNameWithExt)
// Home directory is not a project directory
if HomePath == path {
return false, nil
}
return fileutil.Exists(configFile)
}
// saveConfig will save the config to a file
func saveConfig(v *viper.Viper, file string) error {
err := v.WriteConfigAs(file)
if err != nil {
return errors.Wrap(err, messages.CONFIG_SAVE_ERROR)
}
return nil
}