-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig.go
154 lines (125 loc) · 3.91 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
package slss
import (
"encoding/json"
"io/ioutil"
"os"
"github.com/pkg/errors"
)
const projectConfigPath = "./lambda/project.json"
// Amazon AWS configuration
type awsConfig struct {
AccessKeyID string `json:"access_key_id"`
SecretAccessKey string `json:"secret_access_key"`
Region string `json:"region"`
Role string `json:"role"`
}
// Shadowsocks configuration
type shadowsocksConfig struct {
LocalPort string `json:"local_port"`
Timeout int `json:"timeout"`
Method string `json:"method"`
Password string `json:"password"`
}
type ngrokConfig struct {
AuthToken string `json:"auth_token"`
}
// LambdaShadowSocksConfig represents the configuration needed for lambda
type LambdaShadowSocksConfig struct {
Port string `json:"port"`
Method string `json:"method"`
Password string `json:"password"`
ProxyURL string `json:"proxyURL"`
NgrokToken string `json:"ngrokToken"`
}
// Config represents the project's configuration
type Config struct {
AWS *awsConfig `json:"AWS"`
Shadowsocks *shadowsocksConfig `json:"shadowsocks"`
Ngrok *ngrokConfig `json:"ngrok"`
LocalServerPort string `json:"local_server_port"`
}
// FuncConfig represents the slss lambda function configuration
type FuncConfig struct {
Name string `json:"name"`
Description string `json:"description"`
Runtime string `json:"runtime"`
Memory int `json:"memory"`
Timeout int `json:"timeout"`
}
// ProjectConfig represents the apex project configuration
type ProjectConfig struct {
Name string `json:"name"`
Description string `json:"description"`
Role string `json:"role"`
Memory int `json:"memory"`
}
// LoadFuncConfig loads the lambda function configuration from a specified path
func LoadFuncConfig(path string) (*FuncConfig, error) {
var config = new(FuncConfig)
content, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.WithStack(err)
}
if err := json.Unmarshal(content, config); err != nil {
return nil, errors.WithStack(err)
}
if config.Timeout < 60 {
return nil, errors.New("timeout in function configuration should >= 60")
}
return config, nil
}
// UpdateProjectConfigRole updates the "role" filed in apex project
// configuration
func UpdateProjectConfigRole(role string) error {
var config = new(ProjectConfig)
content, err := ioutil.ReadFile(projectConfigPath)
if err != nil {
return errors.WithStack(err)
}
if err := json.Unmarshal(content, config); err != nil {
return errors.WithStack(err)
}
config.Role = role
contentToUpdate, err := json.Marshal(config)
if err != nil {
return errors.WithStack(err)
}
return ioutil.WriteFile(projectConfigPath, []byte(contentToUpdate), 0644)
}
// LoadConfig loads the configuration object from a specified path
func LoadConfig(path string) (*Config, error) {
var config = new(Config)
content, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.WithStack(err)
}
if err := json.Unmarshal(content, config); err != nil {
return nil, errors.WithStack(err)
}
if config.AWS == nil || config.Ngrok == nil || config.Shadowsocks == nil {
return nil, errors.New("empty configuration")
}
// Try to find AWS configuration from environment variables
if config.AWS.AccessKeyID == "" {
config.AWS.AccessKeyID = os.Getenv("AWS_ACCESS_KEY_ID")
}
if config.AWS.SecretAccessKey == "" {
config.AWS.SecretAccessKey = os.Getenv("AWS_SECRET_ACCESS_KEY")
}
if config.AWS.Region == "" {
config.AWS.Region = os.Getenv("AWS_REGION")
}
if config.AWS.AccessKeyID == "" ||
config.AWS.SecretAccessKey == "" ||
config.AWS.Region == "" ||
config.AWS.Role == "" {
return nil, errors.New("empty AWS configuration")
}
if config.Ngrok.AuthToken == "" {
return nil, errors.New("empty ngrok configuration")
}
if config.LocalServerPort == "" {
return nil, errors.New("empty local_server_port configuration")
}
return config, nil
}