-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.go
More file actions
173 lines (145 loc) · 4.41 KB
/
config.go
File metadata and controls
173 lines (145 loc) · 4.41 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
171
172
173
package config
import (
_ "embed"
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/unweave/cli/ui"
"github.com/unweave/unweave/tools/gonfig"
)
var (
// Version is added at build time
Version = "dev"
//go:embed templates/config.toml
configEmbed string
configTemplate, _ = template.New("config.toml").Parse(configEmbed)
//go:embed templates/env
envEmbed string
envTemplate, _ = template.New("env").Parse(envEmbed)
//go:embed templates/gitignore
gitignoreEmbed string
unweaveConfigPath = ""
projectConfigPath = ProjectConfigDirName + "/config.toml"
envConfigPath = ProjectConfigDirName + "/.env"
Config = &config{
Unweave: &unweave{
ApiURL: "https://api.unweave.io",
AppURL: "https://app.unweave.io",
User: &user{},
},
Project: &Project{
Env: &Secrets{},
Providers: map[string]provider{},
},
}
)
func GetProjectOwnerAndName() (owner string, name string) {
uri := Config.Project.URI
parts := strings.Split(uri, "/")
if len(parts) != 2 {
ui.Errorf("Invalid project URI: %q. Should be of type '<owner>/<project>", uri)
os.Exit(1)
}
owner = strings.Split(uri, "/")[0]
name = strings.Split(uri, "/")[1]
return owner, name
}
// GetActiveProjectPath returns the active project directory by recursively going up the
// directory tree until it finds a directory that's contains the .unweave/config.toml file
func GetActiveProjectPath() (string, error) {
var activeProjectDir string
pwd, err := os.Getwd()
if err != nil {
return "", err
}
var walk func(path string)
walk = func(path string) {
cfgDir := filepath.Join(path, ProjectConfigDirName)
if _, err = os.Stat(cfgDir); err == nil {
if _, err = os.Stat(filepath.Join(cfgDir, "config.toml")); err == nil {
activeProjectDir = path
return
}
}
parent := filepath.Dir(path)
if parent == "." || parent == "/" {
return
}
walk(parent)
}
walk(pwd)
if activeProjectDir == "" {
return "", fmt.Errorf("no active project found")
}
return activeProjectDir, nil
}
func GetGlobalConfigPath() string {
home, err := os.UserHomeDir()
if err != nil {
ui.Errorf("Could not get user home directory")
os.Exit(1)
}
return filepath.Join(home, GlobalConfigDirName)
}
func InitProjectConfigFrom(projectConfigPath, envConfigPath string) (*Secrets, *Project) {
envConfig := &Secrets{}
projectConfig := &Project{}
if err := readAndUnmarshal(projectConfigPath, &projectConfig); err != nil {
ui.Infof("Failed to read project config at path %q", projectConfigPath)
}
if err := readAndUnmarshal(envConfigPath, envConfig); err != nil {
ui.Infof("Failed to read environment config at path %q", envConfigPath)
}
return envConfig, projectConfig
}
func Init() {
// ----- ProjectConfig -----
envConfig := &Secrets{}
projectConfig := &Project{}
projectDir, err := GetActiveProjectPath()
if err == nil {
projectConfigPath = filepath.Join(projectDir, projectConfigPath)
envConfigPath = filepath.Join(projectDir, envConfigPath)
envConfig, projectConfig = InitProjectConfigFrom(projectConfigPath, envConfigPath)
}
projectConfig.Env = envConfig
// ----- Unweave Config -----
env := "production"
if e, ok := os.LookupEnv("UNWEAVE_ENV"); ok {
env = e
}
apiURL := Config.Unweave.ApiURL
appURL := Config.Unweave.AppURL
switch env {
case "staging", "stg":
unweaveConfigPath = filepath.Join(GetGlobalConfigPath(), "stg-config.toml")
apiURL = "https://api.staging-unweave.io"
appURL = "https://app.staging-unweave.io"
case "development", "dev":
unweaveConfigPath = filepath.Join(GetGlobalConfigPath(), "dev-config.toml")
apiURL = "http://localhost:4000"
appURL = "http://localhost:3000"
case "production", "prod":
unweaveConfigPath = filepath.Join(GetGlobalConfigPath(), "config.toml")
default:
// If anything else, assume production
fmt.Println("Unrecognized environment. Assuming production.")
}
// Load saved config - create the empty config if it doesn't exist
if err = readAndUnmarshal(unweaveConfigPath, Config.Unweave); os.IsNotExist(err) {
err = Config.Unweave.Save()
if err != nil {
ui.Errorf("Failed to create config file: %v", err)
}
} else if err != nil {
ui.Errorf("Failed to read config file: %v", err)
}
// Need to set these after reading the config file so that they can be overridden
Config.Unweave.ApiURL = apiURL
Config.Unweave.AppURL = appURL
Config.Project = projectConfig
// Override with environment variables
gonfig.GetFromEnvVariables(Config)
}