forked from railwayapp/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
132 lines (111 loc) · 2.68 KB
/
Copy pathmain.go
File metadata and controls
132 lines (111 loc) · 2.68 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
package configs
import (
"os"
"os/user"
"path"
"path/filepath"
"reflect"
"github.com/spf13/viper"
)
type Config struct {
viper *viper.Viper
configPath string
}
type Configs struct {
projectConfigs *Config
userConfigs *Config
RailwayProductionToken string
RailwayEnvFilePath string
}
func IsDevMode() bool {
environment, exists := os.LookupEnv("RAILWAY_ENV")
return exists && environment == "develop"
}
func (c *Configs) CreatePathIfNotExist(path string) error {
dir := filepath.Dir(path)
if _, err := os.Stat(dir); os.IsNotExist(err) {
err = os.MkdirAll(dir, os.ModePerm)
if err != nil {
return err
}
}
return nil
}
func (c *Configs) unmarshalConfig(config *Config, data interface{}) error {
err := config.viper.ReadInConfig()
if err != nil {
return err
}
return config.viper.Unmarshal(&data)
}
func (c *Configs) marshalConfig(config *Config, cfg interface{}) error {
reflectCfg := reflect.ValueOf(cfg)
for i := 0; i < reflectCfg.NumField(); i++ {
k := reflectCfg.Type().Field(i).Name
v := reflectCfg.Field(i).Interface()
config.viper.Set(k, v)
}
err := c.CreatePathIfNotExist(config.configPath)
if err != nil {
return err
}
err = config.viper.WriteConfig()
return err
}
func fetchHomeDir() (homeDir *string, err error) {
// In OSX BigSur, or terminals where the user isn't set
// user.Current cannot function
// As a backstop (We should be crosscompiling using clang)
// hoist the dirhir from env
defer func() {
if r := recover(); r != nil {
home := os.Getenv("HOME")
homeDir = &home
err = nil
}
}()
user, err := user.Current()
if err != nil {
return nil, err
}
return &user.HomeDir, nil
}
func New() *Configs {
// Configs stored in projects (<project>/.railway)
// Includes projectId, environmentId, etc
projectDir, err := filepath.Abs("./.railway")
if err != nil {
panic(err)
}
projectViper := viper.New()
projectPath := path.Join(projectDir, "./config.json")
projectViper.SetConfigFile(projectPath)
projectViper.ReadInConfig()
if err != nil {
panic(err)
}
projectConfig := &Config{
viper: projectViper,
configPath: projectPath,
}
// Configs stored in root (~/.railway)
// Includes token, etc
userViper := viper.New()
homeDir, err := fetchHomeDir()
if err != nil {
panic(err)
}
userPath := path.Join(*homeDir, ".railway/config.json")
userViper.SetConfigFile(userPath)
userViper.ReadInConfig()
userConfig := &Config{
viper: userViper,
configPath: userPath,
}
return &Configs{
projectConfigs: projectConfig,
userConfigs: userConfig,
RailwayProductionToken: os.Getenv("RAILWAY_TOKEN"),
RailwayEnvFilePath: path.Join(projectDir, "env.json"),
}
}