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
119 lines (99 loc) · 2.61 KB
/
Copy pathmain.go
File metadata and controls
119 lines (99 loc) · 2.61 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
package configs
import (
"fmt"
"os"
"path"
"path/filepath"
"reflect"
"github.com/spf13/viper"
)
type Config struct {
viper *viper.Viper
configPath string
}
type Configs struct {
rootConfigs *Config
projectConfigs *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
}
return config.viper.WriteConfig()
}
func New() *Configs {
// Configs stored in root (~/.railway)
// Includes token, etc
rootViper := viper.New()
rootConfigPartialPath := ".railway/config.json"
if IsDevMode() {
rootConfigPartialPath = ".railway/dev-config.json"
}
rootConfigPath := path.Join(os.Getenv("HOME"), rootConfigPartialPath)
rootViper.SetConfigFile(rootConfigPath)
err := rootViper.ReadInConfig()
if os.IsNotExist(err) {
// That's okay, configs are created as needed
} else if err != nil {
fmt.Printf("Unable to parse railway config! %s\n", err)
}
rootConfig := &Config{
viper: rootViper,
configPath: rootConfigPath,
}
// 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)
err = projectViper.ReadInConfig()
if os.IsNotExist(err) {
// That's okay, configs are created as needed
} else if err != nil {
fmt.Printf("Unable to parse project config! %s\n", err)
}
projectConfig := &Config{
viper: projectViper,
configPath: projectPath,
}
return &Configs{
projectConfigs: projectConfig,
rootConfigs: rootConfig,
RailwayProductionToken: os.Getenv("RAILWAY_TOKEN"),
RailwayEnvFilePath: path.Join(projectDir, "env.json"),
}
}