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

Commit 8eb3c85

Browse files
committed
ENH:add env setup, split notify and write logic
1 parent 80bd159 commit 8eb3c85

File tree

1 file changed

+70
-55
lines changed

1 file changed

+70
-55
lines changed

owlly.go

Lines changed: 70 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@ import (
1717
)
1818

1919
var (
20-
watcher *fsnotify.Watcher
20+
watcher *fsnotify.Watcher
2121
api *slack.Client
2222
onlyOnce sync.Once
2323
envList = []string{".env", ".env.test", ".env.development", ".env.production"}
24-
customList = []string{}
25-
slackChannelList = []string{""}
2624
)
2725

2826
func nilChecker(err error) {
@@ -39,20 +37,16 @@ func initWatcher() {
3937
watcher = _watcher
4038
}
4139

42-
func getEnvList() []string {
40+
func getEnvList() []string {
4341
return envList
4442
}
4543

46-
func getCustomList() []string {
47-
return customList
48-
}
49-
5044
// @dev start watching multiple envs
5145
func registerEnvs() {
5246
for _, v := range getEnvList() {
5347
wd, _ := os.Getwd()
5448
filePath := strings.Join([]string{wd, "\\", v}, "")
55-
log.Println(filePath)
49+
5650
wErr := watcher.Add(filePath)
5751
nilChecker(wErr)
5852
color.Blue(fmt.Sprintf("watching: %v", v))
@@ -76,77 +70,96 @@ func initSlack() {
7670
api = _api
7771
}
7872

79-
func updateEnvs() {
73+
func updateEnvs() {
8074
wd, _ := os.Getwd()
81-
75+
8276
for _, v := range getEnvList() {
8377
data, rErr := os.ReadFile(v)
8478
nilChecker(rErr)
85-
79+
8680
wrapDirName := "config"
8781
wrapDirPath := strings.Join([]string{wd, "\\", wrapDirName, "\\"}, "")
8882
wrapEnvName := strings.Join([]string{v, ".", wrapDirName}, "")
8983
wrapEnvFile := strings.Join([]string{wrapDirPath, wrapEnvName}, "")
90-
84+
9185
// @dev owning user has a read and write permission: 0644
9286
os.WriteFile(wrapEnvFile, data, fs.FileMode(config.OWNER_PERM))
9387
}
9488
}
9589

96-
func updateAndNotify() {
97-
var fullPath string
90+
func cleanupEnvs() {
91+
wd, _ := os.Getwd()
92+
for _, v := range getEnvList() {
93+
fullPathForWrapEnv := strings.Join([]string{wd, "\\", "config", "\\", v, ".config"}, "")
94+
_, sErr := os.Stat(fullPathForWrapEnv)
9895

99-
for _, v := range envList {
100-
path, _ := os.Getwd()
101-
fullPath = strings.Join([]string{path, "\\config\\", v, ".config"}, "")
102-
log.Println("path is: ", fullPath)
103-
bytes, readErr := os.ReadFile(fullPath)
104-
nilChecker(readErr)
105-
106-
// TODO fix not writing file bug
107-
filePermission := 0644
108-
writeErr := os.WriteFile(fullPath, bytes, fs.FileMode(filePermission))
109-
nilChecker(writeErr)
110-
}
96+
if ok := os.IsExist(sErr); ok {
97+
rErr := os.Remove(fullPathForWrapEnv)
98+
nilChecker(rErr)
99+
}
100+
101+
_, cErr := os.Create(fullPathForWrapEnv)
102+
nilChecker(cErr)
103+
104+
_data, rErr := os.ReadFile(v)
105+
nilChecker(rErr)
111106

112-
// if done send a DM to slack channel
113-
if isDone := isUpdateFinished(); isDone {
114-
envString := convertEnvMapToString(fullPath)
115-
onlyOnce.Do(func() {
116-
notifyEnvChange(envString)
117-
})
107+
wErr := os.WriteFile(fullPathForWrapEnv, _data, fs.FileMode(config.OWNER_PERM))
108+
nilChecker(wErr)
118109
}
110+
111+
color.Red("envs config setup done")
119112
}
120113

121-
func isUpdateFinished() bool {
122-
var isDone bool
114+
func sendSlackDM() {
115+
wd, _ := os.Getwd()
116+
117+
// has
118+
envStringMapForWrapEnv := make(map[string]string)
119+
envStringForWrapEnv := "DEFAULT_VALUE"
120+
121+
for _, v := range getEnvList() {
122+
if isDone := isUpdateFinished(); isDone[v] {
123+
fullPathForWrapEnv := strings.Join([]string{wd, "\\", "config", "\\", v, ".config"}, "")
124+
wrapEnvName := strings.Join([]string{v, ".config"}, "")
123125

124-
for _, v := range envList {
125-
dir, _ := os.Getwd()
126-
fullPath := strings.Join([]string{dir, "\\", v}, "")
127-
_data, _readErr := os.ReadFile(fullPath)
126+
envStringForWrapEnv = convertEnvMapToString(fullPathForWrapEnv, wrapEnvName)
127+
envStringMapForWrapEnv[wrapEnvName] = envStringForWrapEnv
128128

129-
nilChecker(_readErr)
129+
notifyEnvChange(envStringMapForWrapEnv[wrapEnvName], v)
130+
}
131+
}
132+
}
133+
134+
func isUpdateFinished() map[string]bool {
135+
// @dev initialze a map with make
136+
isDone := make(map[string]bool)
130137

138+
for _, v := range getEnvList() {
139+
wd, _ := os.Getwd()
140+
fullPath := strings.Join([]string{wd, "\\", v}, "")
141+
142+
_data, _rErr := os.ReadFile(fullPath)
143+
nilChecker(_rErr)
131144
data := string(_data)
132-
isDone = strings.Contains(data, config.RESERVED)
133145

134-
log.Printf(".env in root DONE value: %v", isDone)
146+
hasOwllyTrigger := strings.Contains(data, config.RESERVED)
147+
isDone[v] = hasOwllyTrigger
135148
}
136149

137150
return isDone
138151
}
139152

140-
/// @dev textify
141-
func convertEnvMapToString(envPath string) string {
153+
func convertEnvMapToString(envPath string, wrapEnvName string) string {
142154
dotenvMap, readErr := godotenv.Read(envPath)
143155
nilChecker(readErr)
144156

145157
// marshaling map: ascending sort
146158
marshalMsg, marshalErr := godotenv.Marshal(dotenvMap)
147159
nilChecker(marshalErr)
148160

149-
color.Green(marshalMsg)
161+
parsedMsg := fmt.Sprintf("file parsed: %v", wrapEnvName)
162+
color.Green(parsedMsg)
150163

151164
return marshalMsg
152165
}
@@ -162,11 +175,12 @@ func getUpdateMetadata() (string, string) {
162175
return date, dirPath // yyyy-mm-dd hh-mm
163176
}
164177

165-
func notifyEnvChange(envString string) {
178+
func notifyEnvChange(envString string, envFileName string) {
166179
date, dirPath := getUpdateMetadata()
167-
dateMsg := fmt.Sprintf(".env updated at: %v", date)
168-
pathMsg := fmt.Sprintf(".env directory is: %v", dirPath)
180+
dateMsg := fmt.Sprintf("%v updated at: %v", envFileName, date)
181+
pathMsg := fmt.Sprintf("directory is: %v", dirPath)
169182

183+
// @dev assert a parsed env value is thread-safe
170184
attachedEnv := slack.AttachmentField{
171185
Title: "Copy and paste below texts to update",
172186
Value: envString,
@@ -197,10 +211,12 @@ func notifyEnvChange(envString string) {
197211
}
198212

199213
func main() {
214+
cleanupEnvs()
200215

201-
202-
initSlack()
203-
initWatcher()
216+
onlyOnce.Do(func() {
217+
initSlack()
218+
initWatcher()
219+
})
204220

205221
/// @dev starts monitoring the path for changes.
206222
registerEnvs()
@@ -214,8 +230,6 @@ func main() {
214230
}
215231
if event.Has(fsnotify.Write) {
216232
log.Println("modified file: ", event.Name)
217-
// TODO split update and notify
218-
// updateAndNotify()
219233
updateEnvs()
220234
}
221235
case err, ok := <-watcher.Errors:
@@ -227,6 +241,7 @@ func main() {
227241
}
228242
}()
229243

230-
// Block main goroutine forever.
231-
<-make(chan struct{})
244+
sendSlackDM()
245+
color.Blue("WORK DONE! Exit Owlly 👋")
246+
os.Exit(1)
232247
}

0 commit comments

Comments
 (0)