-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
96 lines (83 loc) · 1.87 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
package geploy
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/fatih/color"
"github.com/joho/godotenv"
"gopkg.in/yaml.v3"
)
type DeployConfig struct {
Service string `yaml:"service"`
Port string `yaml:"port"`
Image string `yaml:"image"`
Servers []string `yaml:"servers"`
HealthCheck string `yaml:"health_check,omitempty"`
SshConfig struct {
Username string `yaml:"usr"`
} `yaml:"ssh,omitempty"`
Registry struct {
// todo customize registry center
Username any `yaml:"usr"`
Password any `yaml:"pwd"`
} `yaml:"registry"`
Env []string `yaml:"env"`
env map[string]string
}
func LoadDeployConfig() (*DeployConfig, error) {
if err := godotenv.Load(); err != nil {
return nil, err
}
workdir, err := os.Getwd()
if err != nil {
return nil, err
}
data, err := os.ReadFile(filepath.Join(workdir, "deploy.yml"))
if err != nil {
return nil, err
}
cfg := new(DeployConfig)
if err := yaml.Unmarshal(data, cfg); err != nil {
return nil, err
}
cfg.env = make(map[string]string)
for _, env := range cfg.Env {
var key, value string
if strings.Contains(env, "=") {
// set env literally
if parts := strings.SplitN(env, "=", 2); len(parts) == 1 {
// in case of no value
key = parts[0]
} else {
key, value = parts[0], parts[1]
}
} else {
// inherit from current environment
key, value = env, os.Getenv(env)
}
if value != "" {
cfg.env[key] = value
} else {
fmt.Println("Environment variable", color.HiYellowString(key), "is missing")
}
}
// set default values
if cfg.HealthCheck == "" {
cfg.HealthCheck = "/ping"
}
if cfg.SshConfig.Username == "" {
cfg.SshConfig.Username = "root"
}
return cfg, nil
}
func lookup(key any) string {
switch key := key.(type) {
case string:
return key
case []any:
return os.Getenv(key[0].(string))
default:
panic("key not found")
}
}