This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
forked from eFishery/NeMo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
212 lines (174 loc) · 5.57 KB
/
util.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"path/filepath"
"net/http"
"encoding/json"
"io/ioutil"
"strings"
"strconv"
"log"
"os"
"bytes"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/awsutil"
yaml "github.com/goccy/go-yaml"
)
func LoadSetting() *Setting {
pwd, err := os.Getwd()
if err != nil {
log.Println(err)
}
if _, err := os.Stat(pwd + "/coral"); os.IsNotExist(err) {
os.MkdirAll(pwd + "/coral", os.ModePerm)
}
if _, err := os.Stat(pwd + "/.build/sessions/image"); os.IsNotExist(err) {
os.MkdirAll(pwd + "/.build/sessions", os.ModePerm)
}
return &Setting{
UserAgent: getEnvString("USER_AGENT", "nemo/1.0.0"),
RandMin: getEnvInt("RAND_MIN", 2),
RandMax: getEnvInt("RAND_MAX", 4),
LimitRandMax: getEnvInt("LIMIT_RAND_MAX", 5),
SessionsDir: getEnvString("SESSION_DIR", pwd),
CoralDir: getEnvString("CORAL_DIR", pwd + "/coral"),
BuildDir: getEnvString("BUILD_DIR", pwd + "/.build"),
AwsS3RegionName: getEnvString("AWS_S3_REGION_NAME", ""),
AwsS3Dir: getEnvString("AWS_S3_DIR", ""),
AwsS3EndpointUrl: getEnvString("AWS_S3_ENDPOINT_URL", ""),
AwsStorageBucketName: getEnvString("AWS_STORAGE_BUCKET_NAME", ""),
AwsSecretAccessKey: getEnvString("AWS_SECRET_ACCESS_KEY", ""),
AwsAccessKeyId: getEnvString("AWS_ACCESS_KEY_ID", ""),
}
}
func between(value string, a string, b string) string {
// Get substring between two strings.
posFirst := strings.Index(value, a)
if posFirst == -1 {
return ""
}
posLast := strings.Index(value, b)
if posLast == -1 {
return ""
}
posFirstAdjusted := posFirst + len(a)
if posFirstAdjusted >= posLast {
return ""
}
return value[posFirstAdjusted:posLast]
}
func after(value string, a string) string {
// Get substring after a string.
pos := strings.LastIndex(value, a)
if pos == -1 {
return ""
}
adjustedPos := pos + len(a)
if adjustedPos >= len(value) {
return ""
}
return value[adjustedPos:len(value)]
}
func AddFileToS3(fileDir string) string {
creds := credentials.NewStaticCredentials(Settings.AwsAccessKeyId, Settings.AwsSecretAccessKey, "")
_, err := creds.Get()
if err != nil {
log.Println("Error After set the creds")
}
// Create a single AWS session (we can re use this if we're uploading many files)
cfg := aws.NewConfig().WithRegion(Settings.AwsS3RegionName).WithCredentials(creds)
svc := s3.New(session.New(), cfg)
// Upload
// Open the file for use
file, err := os.Open(fileDir)
if err != nil {
log.Println("Error After Open The File " + fileDir)
}
defer file.Close()
// Get file size and read the file content into a buffer
fileInfo, _ := file.Stat()
var size int64 = fileInfo.Size()
buffer := make([]byte, size)
file.Read(buffer)
fileName := filepath.Base(fileDir)
// Config settings: this is where you choose the bucket, filename, content-type etc.
// of the file you're uploading.
resp, err := svc.PutObject(&s3.PutObjectInput{
Bucket: aws.String(Settings.AwsStorageBucketName),
Key: aws.String(Settings.AwsS3Dir + fileName),
Body: bytes.NewReader(buffer),
ContentLength: aws.Int64(size),
ContentType: aws.String(http.DetectContentType(buffer)),
})
if err != nil {
log.Println("Error After Put Object to S3")
}
log.Println(awsutil.StringValue(resp))
return Settings.AwsS3EndpointUrl + Settings.AwsS3Dir + fileName
}
func (c *Coral) getCoral(filename string) *Coral {
yamlFile, err := ioutil.ReadFile( Settings.CoralDir + "/" + filename)
if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
}
err = yaml.Unmarshal(yamlFile, c);
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}
return c
}
func readScheduleFiles() bool{
content, err := ioutil.ReadFile(Settings.BuildDir + "/schedules.json")
if err != nil {
log.Fatal(err)
}
jsonErr := json.Unmarshal(content, &Schedules)
if jsonErr != nil {
log.Fatal(jsonErr)
}
return true
}
func readBuildCommandsFiles() bool{
content, err := ioutil.ReadFile(Settings.BuildDir + "/commands.json")
if err != nil {
os.OpenFile(Settings.BuildDir + "/commands.json", os.O_RDONLY|os.O_CREATE, 0755)
log.Fatal(err)
}
jsonErr := json.Unmarshal(content, &BuildCommands)
if jsonErr != nil {
log.Fatal(jsonErr)
}
return true
}
func readGreetingsFile() bool{
content, err := ioutil.ReadFile(Settings.BuildDir + "/greetings.json")
if err != nil {
os.OpenFile(Settings.BuildDir + "/greetings.json", os.O_RDONLY|os.O_CREATE, 0755)
log.Fatal(err)
}
jsonErr := json.Unmarshal(content, &BuildGreetings)
if jsonErr != nil {
log.Fatal(jsonErr)
}
return true
}
func fileSession(phone_number string) string {
return Settings.BuildDir + "/sessions/" + phone_number + ".session"
}
// Simple helper function to read an environment or return a default value
func getEnvString(key string, defaultVal string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return defaultVal
}
// Simple helper function to read an environment or return a default value
func getEnvInt(key string, defaultVal int) int {
valueStr := getEnvString(key, "")
if value, err := strconv.Atoi(valueStr); err == nil {
return value
}
return defaultVal
}