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

Commit bb6c7b5

Browse files
Merge pull request #3 from asunlabs/feature/watchOnlyExistingEnvs
Feature/watch only existing envs
2 parents c9ae168 + 985fde7 commit bb6c7b5

File tree

3 files changed

+128
-59
lines changed

3 files changed

+128
-59
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
# Configs
1+
# env file list
22
.env
33
.env.production
44
.env.development
55
.env.test
6+
.env.dev
7+
.env.prod
8+
9+
# Owlly binaries
610
owlly-for-mac
711
owlly-for-mac-m1
812
owlly.exe

README.md

+27-16
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<div align="center">
2-
<img src="https://user-images.githubusercontent.com/83855174/200222357-3e74436c-36be-41f9-8da6-519052db15bf.png" alt="owlly banner" width="100%" />
2+
<img src="https://user-images.githubusercontent.com/83855174/200222357-3e74436c-36be-41f9-8da6-519052db15bf.png" alt="banner" width="100%" />
33
</div>
44

55
# 🦉 Owlly
66

7-
<img src="https://img.shields.io/badge/version-v0.1.3-red" alt="version 0.1.3" />
7+
<img src="https://img.shields.io/badge/version-v0.1.4-red" alt="version 0.1.4" />
88

99
A file-based .env change notifier for your slack team.
1010

11-
<img src="https://user-images.githubusercontent.com/83855174/198875029-f20bba16-66e6-48d2-9d06-9feaea8fe175.gif" height="400px" alt="owlly banner" width="100%" />
11+
<img src="https://user-images.githubusercontent.com/83855174/200312951-eec9e105-1772-4afa-b0ae-233d5fbda28d.gif" alt="demo" width="100%" />
1212

13-
## Contents
13+
## Table of Contents
1414

1515
- [Owlly](#owlly)
1616
- [Features](#features)
@@ -23,7 +23,7 @@ A file-based .env change notifier for your slack team.
2323
## Features
2424

2525
- Auto-sync .env file changes
26-
- Watch multiple .env files: .env, .env.test, .env.development, .env.production
26+
- Watch multiple .env files
2727
- Auto-post the update to slack channel as attachment
2828
- Basic metadata supported: timestamp, .env directory
2929
- Cross platform supported: Windows, Mac OS(intel, m1 chip)
@@ -48,6 +48,23 @@ Or, simply download binaries from [release](https://github.com/asunlabs/owlly/re
4848

4949
## Prerequisite
5050

51+
### Assumption
52+
53+
Owlly will check if below files exist in your project root and sync if exist.
54+
55+
```go
56+
envList = []string{
57+
".env",
58+
".env.test",
59+
".env.development",
60+
".env.production",
61+
".env.dev",
62+
".env.prod",
63+
}
64+
```
65+
66+
### Slack
67+
5168
In order to use Owlly, you have to
5269

5370
1. Create a slack bot
@@ -66,26 +83,20 @@ SLACK_CHANNEL_ID="channel-id-here"
6683

6784
## Usage
6885

69-
1. Make sure you have all .env files in project root
70-
71-
```
72-
.env, .env.test, .env.production, .env.development
73-
```
74-
75-
2. Update .env files as you wish.
86+
1. Update .env files as you wish.
7687

7788
```sh
7889
FOO="bar"
7990
```
8091

81-
3. Once done, set OWLLY_DONE variable in your .env. This variable will be a key for Owlly to know if your update is done.
92+
2. Once done, set OWLLY_DONE variable in your .env. This variable will be a key for Owlly to know if your update is done.
8293

8394
```sh
8495
# length of OWLLY_DONE > 0 ? send a DM : do nothing
8596
OWLLY_DONE="true"
8697
```
8798

88-
4. Run Owlly to watch .env changes.
99+
3. Run Owlly
89100

90101
```sh
91102
# if you cloned a repo,
@@ -97,9 +108,9 @@ go run owlly.go
97108
./owlly-for-mac-m1
98109
```
99110

100-
Check your slack channel if the message is sent. Result will look like below.
111+
Check your slack channel if the message is sent. Result will look like below.
101112

102-
<img src="hhttps://user-images.githubusercontent.com/83855174/198875580-ba52d111-907a-43bf-8937-23b5558378a4.png" height="400px" alt="owlly banner" width="100%" />
113+
<img src="https://user-images.githubusercontent.com/83855174/200310048-48d918c4-2478-4d60-a68a-906202b1a8db.png" height="400px" alt="owlly banner" width="100%" />
103114

104115
**Note**
105116

owlly.go

+96-42
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ var (
2020
watcher *fsnotify.Watcher
2121
api *slack.Client
2222
onlyOnce sync.Once
23-
envList = []string{".env", ".env.test", ".env.development", ".env.production"}
23+
envList = []string{
24+
".env",
25+
".env.test",
26+
".env.development",
27+
".env.production",
28+
".env.dev",
29+
".env.prod",
30+
}
2431
)
2532

2633
func nilChecker(err error) {
@@ -41,23 +48,50 @@ func getEnvList() []string {
4148
return envList
4249
}
4350

51+
func hasNamedEnvFiles(filePath string) bool {
52+
var result bool
53+
54+
_, fErr := os.Stat(filePath)
55+
56+
if os.IsNotExist(fErr) {
57+
result = false
58+
}
59+
60+
if !os.IsNotExist(fErr) {
61+
result = true
62+
}
63+
64+
return result
65+
}
66+
4467
// @dev start watching multiple envs
4568
func registerEnvs() {
4669
for _, v := range getEnvList() {
4770
wd, _ := os.Getwd()
4871
filePath := strings.Join([]string{wd, "/", v}, "")
4972

50-
wErr := watcher.Add(filePath)
51-
nilChecker(wErr)
52-
color.Blue(fmt.Sprintf("watching: %v", v))
73+
// add only existing envs
74+
ok := hasNamedEnvFiles(filePath)
75+
76+
if ok {
77+
wErr := watcher.Add(filePath)
78+
nilChecker(wErr)
79+
color.Blue(fmt.Sprintf("watching: %v", v))
80+
}
5381
}
5482
}
5583

5684
// @dev load multiple envs and init slack instance
5785
func initSlack() {
5886
for _, v := range envList {
59-
lErr := godotenv.Load(v)
60-
nilChecker(lErr)
87+
wd, _ := os.Getwd()
88+
filePath := strings.Join([]string{wd, "/", v}, "")
89+
ok := hasNamedEnvFiles(filePath)
90+
91+
if ok {
92+
lErr := godotenv.Load(v)
93+
nilChecker(lErr)
94+
}
6195
}
6296

6397
_api := slack.New(os.Getenv("SLACK_BOT_USER_OAUTH_TOKEN"))
@@ -66,67 +100,83 @@ func initSlack() {
66100

67101
connectionMsg := fmt.Sprintf("slack API connected to: %s", res.Team)
68102
color.Green(connectionMsg)
69-
103+
70104
api = _api
71105
}
72106

73107
func updateEnvs() {
74-
wd, _ := os.Getwd()
75-
76108
for _, v := range getEnvList() {
77-
data, rErr := os.ReadFile(v)
78-
nilChecker(rErr)
79-
80-
wrapDirName := "config"
81-
wrapDirPath := strings.Join([]string{wd, "/", wrapDirName, "/"}, "")
82-
wrapEnvName := strings.Join([]string{v, ".", wrapDirName}, "")
83-
wrapEnvFile := strings.Join([]string{wrapDirPath, wrapEnvName}, "")
84-
85-
// @dev owning user has a read and write permission: 0644
86-
os.WriteFile(wrapEnvFile, data, fs.FileMode(config.OWNER_PERM))
109+
wd, _ := os.Getwd()
110+
filePath := strings.Join([]string{wd, "/", v}, "")
111+
112+
ok := hasNamedEnvFiles(filePath)
113+
114+
if ok {
115+
data, rErr := os.ReadFile(v)
116+
nilChecker(rErr)
117+
118+
wrapDirName := "config"
119+
wrapDirPath := strings.Join([]string{wd, "/", wrapDirName, "/"}, "")
120+
wrapEnvName := strings.Join([]string{v, ".", wrapDirName}, "")
121+
wrapEnvFile := strings.Join([]string{wrapDirPath, wrapEnvName}, "")
122+
123+
// @dev owning user has a read and write permission: 0644
124+
os.WriteFile(wrapEnvFile, data, fs.FileMode(config.OWNER_PERM))
125+
}
87126
}
88127
}
89128

90129
func cleanupEnvs() {
130+
userEnvList := []string{}
91131
wd, _ := os.Getwd()
92-
for _, v := range getEnvList() {
93-
fullPathForWrapEnv := strings.Join([]string{wd, "/", "config", "/", v, ".config"}, "")
94-
_, sErr := os.Stat(fullPathForWrapEnv)
95132

96-
if ok := os.IsExist(sErr); ok {
97-
rErr := os.Remove(fullPathForWrapEnv)
98-
nilChecker(rErr)
133+
for _, v := range getEnvList() {
134+
ok := hasNamedEnvFiles(strings.Join([]string{wd, "/", v}, ""))
135+
if ok {
136+
userEnvList = append(userEnvList, v)
99137
}
138+
}
100139

140+
for _, v := range userEnvList {
141+
fullPathForWrapEnv := strings.Join([]string{wd, "/", "config", "/", v, ".config"}, "")
142+
143+
rErr := os.Remove(fullPathForWrapEnv)
144+
nilChecker(rErr)
145+
101146
_, cErr := os.Create(fullPathForWrapEnv)
102147
nilChecker(cErr)
103-
148+
104149
_data, rErr := os.ReadFile(v)
105150
nilChecker(rErr)
106-
151+
107152
wErr := os.WriteFile(fullPathForWrapEnv, _data, fs.FileMode(config.OWNER_PERM))
108153
nilChecker(wErr)
109154
}
110-
111155
color.Red("envs config setup done")
112156
}
113157

114158
func sendSlackDM() {
115159
wd, _ := os.Getwd()
116-
117-
// has
160+
118161
envStringMapForWrapEnv := make(map[string]string)
119162
envStringForWrapEnv := "DEFAULT_VALUE"
120-
163+
121164
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"}, "")
165+
filePath := strings.Join([]string{wd, "/", v}, "")
125166

126-
envStringForWrapEnv = convertEnvMapToString(fullPathForWrapEnv, wrapEnvName)
127-
envStringMapForWrapEnv[wrapEnvName] = envStringForWrapEnv
167+
ok := hasNamedEnvFiles(filePath)
128168

129-
notifyEnvChange(envStringMapForWrapEnv[wrapEnvName], v)
169+
if ok {
170+
if isDone := isUpdateFinished(); isDone[v] {
171+
172+
fullPathForWrapEnv := strings.Join([]string{wd, "/", "config", "/", v, ".config"}, "")
173+
wrapEnvName := strings.Join([]string{v, ".config"}, "")
174+
175+
envStringForWrapEnv = convertEnvMapToString(fullPathForWrapEnv, wrapEnvName)
176+
envStringMapForWrapEnv[wrapEnvName] = envStringForWrapEnv
177+
178+
notifyEnvChange(envStringMapForWrapEnv[wrapEnvName], v)
179+
}
130180
}
131181
}
132182
}
@@ -139,12 +189,16 @@ func isUpdateFinished() map[string]bool {
139189
wd, _ := os.Getwd()
140190
fullPath := strings.Join([]string{wd, "/", v}, "")
141191

142-
_data, _rErr := os.ReadFile(fullPath)
143-
nilChecker(_rErr)
144-
data := string(_data)
192+
ok := hasNamedEnvFiles(fullPath)
145193

146-
hasOwllyTrigger := strings.Contains(data, config.RESERVED)
147-
isDone[v] = hasOwllyTrigger
194+
if ok {
195+
_data, _rErr := os.ReadFile(fullPath)
196+
nilChecker(_rErr)
197+
data := string(_data)
198+
199+
hasOwllyTrigger := strings.Contains(data, config.RESERVED)
200+
isDone[v] = hasOwllyTrigger
201+
}
148202
}
149203

150204
return isDone

0 commit comments

Comments
 (0)