Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Commit 763a825

Browse files
Merge pull request #4 from asunlabs/feature/addTomlConfig
Feature/add toml config
2 parents 6c0ea41 + 11e6380 commit 763a825

File tree

8 files changed

+102
-85
lines changed

8 files changed

+102
-85
lines changed

Diff for: .gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ owlly-for-mac
1111
owlly-for-mac-m1
1212
owlly.exe
1313

14+
# Owlly config
15+
owlly.toml
16+
1417
# Pre-feature
1518
src
1619

1720
config/*
1821
!config/database.go
19-
!config/keycode.go
22+
!config/setup.go
2023
bin/*
2124

2225
# Binaries for programs and plugins

Diff for: config/database.go

-32
This file was deleted.

Diff for: config/keycode.go

-14
This file was deleted.

Diff for: config/setup.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package config
2+
3+
import (
4+
"log"
5+
"os"
6+
"strings"
7+
8+
"github.com/BurntSushi/toml"
9+
)
10+
11+
var (
12+
Owlly *OwllyConfig
13+
)
14+
15+
type OwllyConfig struct {
16+
TriggerName string;
17+
SlackBotOauthToken string;
18+
SlackChannelID string;
19+
SlackUserID string;
20+
SlackUserName string;
21+
}
22+
23+
func New() {
24+
var _Owlly OwllyConfig
25+
26+
path, _ := os.Getwd()
27+
fileName := "owlly.toml"
28+
fullPath := strings.Join([]string{path, "/", fileName}, "")
29+
30+
_, fErr := os.Stat(fullPath)
31+
32+
if fErr != nil {
33+
log.Fatal(fErr.Error())
34+
}
35+
36+
_, dErr := toml.DecodeFile(fullPath, &_Owlly)
37+
38+
if dErr != nil {
39+
log.Fatal(fErr.Error())
40+
}
41+
42+
Owlly = &_Owlly
43+
}

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
)
1313

1414
require (
15+
github.com/BurntSushi/toml v1.2.1 // indirect
1516
github.com/gorilla/websocket v1.4.2 // indirect
1617
github.com/jinzhu/inflection v1.0.0 // indirect
1718
github.com/jinzhu/now v1.1.5 // indirect

Diff for: go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
2+
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
13
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
24
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
35
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

Diff for: owlly.example.toml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
TriggerName = "OWLLY_DONE"
2+
SlackBotOauthToken="xoxb-some-value-here"
3+
SlackChannelID ="xxx-123-yyy"
4+
SlackUserID = "aaa-000-zzz"
5+
SlackUserName = "Jake Sung"

Diff for: owlly.go

+47-38
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ var (
2121
api *slack.Client
2222
onlyOnce sync.Once
2323
envList = []string{
24-
".env",
25-
".env.test",
26-
".env.development",
27-
".env.production",
24+
".env",
25+
".env.test",
26+
".env.development",
27+
".env.production",
2828
".env.dev",
29+
".env.stage",
2930
".env.prod",
3031
}
3132
)
@@ -69,7 +70,7 @@ func registerEnvs() {
6970
for _, v := range getEnvList() {
7071
wd, _ := os.Getwd()
7172
filePath := strings.Join([]string{wd, "/", v}, "")
72-
73+
7374
// add only existing envs
7475
ok := hasNamedEnvFiles(filePath)
7576

@@ -87,41 +88,42 @@ func initSlack() {
8788
wd, _ := os.Getwd()
8889
filePath := strings.Join([]string{wd, "/", v}, "")
8990
ok := hasNamedEnvFiles(filePath)
90-
91+
9192
if ok {
9293
lErr := godotenv.Load(v)
9394
nilChecker(lErr)
9495
}
9596
}
96-
97-
_api := slack.New(os.Getenv("SLACK_BOT_USER_OAUTH_TOKEN"))
97+
98+
_api := slack.New(config.Owlly.SlackBotOauthToken)
9899
res, err := _api.AuthTest()
99100
nilChecker(err)
100101

101102
connectionMsg := fmt.Sprintf("slack API connected to: %s", res.Team)
102103
color.Green(connectionMsg)
103-
104+
104105
api = _api
105106
}
106107

107108
func updateEnvs() {
108109
for _, v := range getEnvList() {
109110
wd, _ := os.Getwd()
110111
filePath := strings.Join([]string{wd, "/", v}, "")
111-
112+
112113
ok := hasNamedEnvFiles(filePath)
113-
114+
114115
if ok {
115116
data, rErr := os.ReadFile(v)
116117
nilChecker(rErr)
117-
118+
118119
wrapDirName := "config"
119120
wrapDirPath := strings.Join([]string{wd, "/", wrapDirName, "/"}, "")
120121
wrapEnvName := strings.Join([]string{v, ".", wrapDirName}, "")
121122
wrapEnvFile := strings.Join([]string{wrapDirPath, wrapEnvName}, "")
122-
123+
123124
// @dev owning user has a read and write permission: 0644
124-
os.WriteFile(wrapEnvFile, data, fs.FileMode(config.OWNER_PERM))
125+
ownerPerm := 0644
126+
os.WriteFile(wrapEnvFile, data, fs.FileMode(ownerPerm))
125127
}
126128
}
127129
}
@@ -139,25 +141,26 @@ func cleanupEnvs() {
139141

140142
for _, v := range userEnvList {
141143
fullPathForWrapEnv := strings.Join([]string{wd, "/", "config", "/", v, ".config"}, "")
142-
144+
143145
rErr := os.Remove(fullPathForWrapEnv)
144146
nilChecker(rErr)
145-
147+
146148
_, cErr := os.Create(fullPathForWrapEnv)
147149
nilChecker(cErr)
148-
150+
149151
_data, rErr := os.ReadFile(v)
150152
nilChecker(rErr)
151-
152-
wErr := os.WriteFile(fullPathForWrapEnv, _data, fs.FileMode(config.OWNER_PERM))
153+
154+
ownerPerm := 0644
155+
wErr := os.WriteFile(fullPathForWrapEnv, _data, fs.FileMode(ownerPerm))
153156
nilChecker(wErr)
154157
}
155158
color.Red("envs config setup done")
156159
}
157160

158161
func sendSlackDM() {
159162
wd, _ := os.Getwd()
160-
163+
161164
envStringMapForWrapEnv := make(map[string]string)
162165
envStringForWrapEnv := "DEFAULT_VALUE"
163166

@@ -168,15 +171,18 @@ func sendSlackDM() {
168171

169172
if ok {
170173
if isDone := isUpdateFinished(); isDone[v] {
171-
172-
fullPathForWrapEnv := strings.Join([]string{wd, "/", "config", "/", v, ".config"}, "")
174+
175+
fullPathForWrapEnv := strings.Join(
176+
[]string{wd, "/", "config", "/", v, ".config"},
177+
"",
178+
)
173179
wrapEnvName := strings.Join([]string{v, ".config"}, "")
174-
180+
175181
envStringForWrapEnv = convertEnvMapToString(fullPathForWrapEnv, wrapEnvName)
176182
envStringMapForWrapEnv[wrapEnvName] = envStringForWrapEnv
177-
183+
178184
notifyEnvChange(envStringMapForWrapEnv[wrapEnvName], v)
179-
}
185+
}
180186
}
181187
}
182188
}
@@ -195,8 +201,8 @@ func isUpdateFinished() map[string]bool {
195201
_data, _rErr := os.ReadFile(fullPath)
196202
nilChecker(_rErr)
197203
data := string(_data)
198-
199-
hasOwllyTrigger := strings.Contains(data, config.RESERVED)
204+
205+
hasOwllyTrigger := strings.Contains(data, config.Owlly.TriggerName)
200206
isDone[v] = hasOwllyTrigger
201207
}
202208
}
@@ -231,31 +237,32 @@ func getUpdateMetadata() (string, string) {
231237

232238
func notifyEnvChange(envString string, envFileName string) {
233239
date, dirPath := getUpdateMetadata()
234-
dateMsg := fmt.Sprintf("%v updated at: %v", envFileName, date)
235-
pathMsg := fmt.Sprintf("directory is: %v", dirPath)
236240

241+
pathMsg := fmt.Sprintf("directory is: %v", dirPath)
242+
footer := fmt.Sprintf("%v at: %v", envFileName, pathMsg)
243+
237244
// @dev assert a parsed env value is thread-safe
238245
attachedEnv := slack.AttachmentField{
239-
Title: "Copy and paste below texts to update",
246+
Title: "🔐🔐🔐🔐🔐🔐🔐🔐🔐🔐🔐🔐",
240247
Value: envString,
241248
Short: false,
242249
}
243250

251+
postBy := fmt.Sprintf("%v updated %v", config.Owlly.SlackUserName, envFileName)
252+
244253
attachment := slack.Attachment{
245-
Title: "ENV update",
254+
Title: postBy,
246255
Fields: []slack.AttachmentField{attachedEnv},
247-
Pretext: dateMsg,
248-
Footer: pathMsg,
256+
Footer: footer,
257+
AuthorID: config.Owlly.SlackUserID,
249258
}
250-
251-
postTo := os.Getenv("SLACK_CHANNEL_ID")
259+
260+
postTo := config.Owlly.SlackChannelID
252261
postWhat := slack.MsgOptionAttachments(attachment)
253-
postAs := slack.MsgOptionAsUser(true)
254-
262+
255263
channelID, _, msgErr := api.PostMessage(
256264
postTo,
257265
postWhat,
258-
postAs,
259266
)
260267

261268
nilChecker(msgErr)
@@ -265,6 +272,8 @@ func notifyEnvChange(envString string, envFileName string) {
265272
}
266273

267274
func main() {
275+
config.New()
276+
268277
cleanupEnvs()
269278

270279
onlyOnce.Do(func() {

0 commit comments

Comments
 (0)