From 89746cf5ef7562b90a671e61fcba2330da10f263 Mon Sep 17 00:00:00 2001 From: designerasun Date: Mon, 7 Nov 2022 21:24:32 +0900 Subject: [PATCH 1/3] BLD:update .env lists --- .gitignore | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 8cf1738..4ef39e5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,12 @@ -# Configs +# env file list .env .env.production .env.development .env.test +.env.dev +.env.prod + +# Owlly binaries owlly-for-mac owlly-for-mac-m1 owlly.exe From b4acc8ca4ef48b427dce45bcb69fed2c7555545f Mon Sep 17 00:00:00 2001 From: designerasun Date: Mon, 7 Nov 2022 21:24:46 +0900 Subject: [PATCH 2/3] ENH:register only existing .env files --- owlly.go | 138 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 96 insertions(+), 42 deletions(-) diff --git a/owlly.go b/owlly.go index a4a2d62..447dcea 100644 --- a/owlly.go +++ b/owlly.go @@ -20,7 +20,14 @@ var ( watcher *fsnotify.Watcher api *slack.Client onlyOnce sync.Once - envList = []string{".env", ".env.test", ".env.development", ".env.production"} + envList = []string{ + ".env", + ".env.test", + ".env.development", + ".env.production", + ".env.dev", + ".env.prod", + } ) func nilChecker(err error) { @@ -41,23 +48,50 @@ func getEnvList() []string { return envList } +func hasNamedEnvFiles(filePath string) bool { + var result bool + + _, fErr := os.Stat(filePath) + + if os.IsNotExist(fErr) { + result = false + } + + if !os.IsNotExist(fErr) { + result = true + } + + return result +} + // @dev start watching multiple envs func registerEnvs() { for _, v := range getEnvList() { wd, _ := os.Getwd() filePath := strings.Join([]string{wd, "/", v}, "") - wErr := watcher.Add(filePath) - nilChecker(wErr) - color.Blue(fmt.Sprintf("watching: %v", v)) + // add only existing envs + ok := hasNamedEnvFiles(filePath) + + if ok { + wErr := watcher.Add(filePath) + nilChecker(wErr) + color.Blue(fmt.Sprintf("watching: %v", v)) + } } } // @dev load multiple envs and init slack instance func initSlack() { for _, v := range envList { - lErr := godotenv.Load(v) - nilChecker(lErr) + wd, _ := os.Getwd() + filePath := strings.Join([]string{wd, "/", v}, "") + ok := hasNamedEnvFiles(filePath) + + if ok { + lErr := godotenv.Load(v) + nilChecker(lErr) + } } _api := slack.New(os.Getenv("SLACK_BOT_USER_OAUTH_TOKEN")) @@ -66,67 +100,83 @@ func initSlack() { connectionMsg := fmt.Sprintf("slack API connected to: %s", res.Team) color.Green(connectionMsg) - + api = _api } func updateEnvs() { - wd, _ := os.Getwd() - for _, v := range getEnvList() { - data, rErr := os.ReadFile(v) - nilChecker(rErr) - - wrapDirName := "config" - wrapDirPath := strings.Join([]string{wd, "/", wrapDirName, "/"}, "") - wrapEnvName := strings.Join([]string{v, ".", wrapDirName}, "") - wrapEnvFile := strings.Join([]string{wrapDirPath, wrapEnvName}, "") - - // @dev owning user has a read and write permission: 0644 - os.WriteFile(wrapEnvFile, data, fs.FileMode(config.OWNER_PERM)) + wd, _ := os.Getwd() + filePath := strings.Join([]string{wd, "/", v}, "") + + ok := hasNamedEnvFiles(filePath) + + if ok { + data, rErr := os.ReadFile(v) + nilChecker(rErr) + + wrapDirName := "config" + wrapDirPath := strings.Join([]string{wd, "/", wrapDirName, "/"}, "") + wrapEnvName := strings.Join([]string{v, ".", wrapDirName}, "") + wrapEnvFile := strings.Join([]string{wrapDirPath, wrapEnvName}, "") + + // @dev owning user has a read and write permission: 0644 + os.WriteFile(wrapEnvFile, data, fs.FileMode(config.OWNER_PERM)) + } } } func cleanupEnvs() { + userEnvList := []string{} wd, _ := os.Getwd() - for _, v := range getEnvList() { - fullPathForWrapEnv := strings.Join([]string{wd, "/", "config", "/", v, ".config"}, "") - _, sErr := os.Stat(fullPathForWrapEnv) - if ok := os.IsExist(sErr); ok { - rErr := os.Remove(fullPathForWrapEnv) - nilChecker(rErr) + for _, v := range getEnvList() { + ok := hasNamedEnvFiles(strings.Join([]string{wd, "/", v}, "")) + if ok { + userEnvList = append(userEnvList, v) } + } + for _, v := range userEnvList { + fullPathForWrapEnv := strings.Join([]string{wd, "/", "config", "/", v, ".config"}, "") + + rErr := os.Remove(fullPathForWrapEnv) + nilChecker(rErr) + _, cErr := os.Create(fullPathForWrapEnv) nilChecker(cErr) - + _data, rErr := os.ReadFile(v) nilChecker(rErr) - + wErr := os.WriteFile(fullPathForWrapEnv, _data, fs.FileMode(config.OWNER_PERM)) nilChecker(wErr) } - color.Red("envs config setup done") } func sendSlackDM() { wd, _ := os.Getwd() - - // has + envStringMapForWrapEnv := make(map[string]string) envStringForWrapEnv := "DEFAULT_VALUE" - + for _, v := range getEnvList() { - if isDone := isUpdateFinished(); isDone[v] { - fullPathForWrapEnv := strings.Join([]string{wd, "/", "config", "/", v, ".config"}, "") - wrapEnvName := strings.Join([]string{v, ".config"}, "") + filePath := strings.Join([]string{wd, "/", v}, "") - envStringForWrapEnv = convertEnvMapToString(fullPathForWrapEnv, wrapEnvName) - envStringMapForWrapEnv[wrapEnvName] = envStringForWrapEnv + ok := hasNamedEnvFiles(filePath) - notifyEnvChange(envStringMapForWrapEnv[wrapEnvName], v) + if ok { + if isDone := isUpdateFinished(); isDone[v] { + + fullPathForWrapEnv := strings.Join([]string{wd, "/", "config", "/", v, ".config"}, "") + wrapEnvName := strings.Join([]string{v, ".config"}, "") + + envStringForWrapEnv = convertEnvMapToString(fullPathForWrapEnv, wrapEnvName) + envStringMapForWrapEnv[wrapEnvName] = envStringForWrapEnv + + notifyEnvChange(envStringMapForWrapEnv[wrapEnvName], v) + } } } } @@ -139,12 +189,16 @@ func isUpdateFinished() map[string]bool { wd, _ := os.Getwd() fullPath := strings.Join([]string{wd, "/", v}, "") - _data, _rErr := os.ReadFile(fullPath) - nilChecker(_rErr) - data := string(_data) + ok := hasNamedEnvFiles(fullPath) - hasOwllyTrigger := strings.Contains(data, config.RESERVED) - isDone[v] = hasOwllyTrigger + if ok { + _data, _rErr := os.ReadFile(fullPath) + nilChecker(_rErr) + data := string(_data) + + hasOwllyTrigger := strings.Contains(data, config.RESERVED) + isDone[v] = hasOwllyTrigger + } } return isDone From 985fde75614697e943d71d048d431cc288d1f7a0 Mon Sep 17 00:00:00 2001 From: designerasun Date: Mon, 7 Nov 2022 21:43:34 +0900 Subject: [PATCH 3/3] DOC:change demo gif, update prerequisite --- README.md | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index e6a384c..b227414 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,16 @@
- owlly banner + banner
# 🦉 Owlly -version 0.1.3 +version 0.1.4 A file-based .env change notifier for your slack team. -owlly banner +demo -## Contents +## Table of Contents - [Owlly](#owlly) - [Features](#features) @@ -23,7 +23,7 @@ A file-based .env change notifier for your slack team. ## Features - Auto-sync .env file changes -- Watch multiple .env files: .env, .env.test, .env.development, .env.production +- Watch multiple .env files - Auto-post the update to slack channel as attachment - Basic metadata supported: timestamp, .env directory - 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 ## Prerequisite +### Assumption + +Owlly will check if below files exist in your project root and sync if exist. + +```go + envList = []string{ + ".env", + ".env.test", + ".env.development", + ".env.production", + ".env.dev", + ".env.prod", + } +``` + +### Slack + In order to use Owlly, you have to 1. Create a slack bot @@ -66,26 +83,20 @@ SLACK_CHANNEL_ID="channel-id-here" ## Usage -1. Make sure you have all .env files in project root - -``` -.env, .env.test, .env.production, .env.development -``` - -2. Update .env files as you wish. +1. Update .env files as you wish. ```sh FOO="bar" ``` -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. +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. ```sh # length of OWLLY_DONE > 0 ? send a DM : do nothing OWLLY_DONE="true" ``` -4. Run Owlly to watch .env changes. +3. Run Owlly ```sh # if you cloned a repo, @@ -97,9 +108,9 @@ go run owlly.go ./owlly-for-mac-m1 ``` -Check your slack channel if the message is sent. Result will look like below. +Check your slack channel if the message is sent. Result will look like below. -owlly banner +owlly banner **Note**