From 9b06574e4f3db10bc4bfb5bbbec4a28dc56984cc Mon Sep 17 00:00:00 2001 From: Asif Mohammad Mollah Date: Tue, 24 Sep 2024 13:42:01 +0400 Subject: [PATCH] chore: system env support added (#5) --- README.md | 16 +++++++++++++--- main.go | 9 ++++++++- utils/common.go | 38 +++++++++++++++++++++++++++++++++++--- 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c3f1663..a08246b 100644 --- a/README.md +++ b/README.md @@ -32,21 +32,21 @@ Download and install executable binary from GitHub releases page. ### Linux Installation ```sh -curl -sL https://github.com/tech-thinker/chatz/releases/download/v1.1.0/chatz-linux-amd64 -o chatz +curl -sL https://github.com/tech-thinker/chatz/releases/download/v1.1.1/chatz-linux-amd64 -o chatz chmod +x chatz sudo mv chatz /usr/bin ``` ### MacOS Installation ```sh -curl -sL https://github.com/tech-thinker/chatz/releases/download/v1.1.0/chatz-darwin-amd64 -o chatz +curl -sL https://github.com/tech-thinker/chatz/releases/download/v1.1.1/chatz-darwin-amd64 -o chatz chmod +x chatz sudo mv chatz /usr/bin ``` ### Windows Installation ```sh -curl -sL https://github.com/tech-thinker/chatz/releases/download/v1.1.0/chatz-windows-amd64.exe -o chatz.exe +curl -sL https://github.com/tech-thinker/chatz/releases/download/v1.1.1/chatz-windows-amd64.exe -o chatz.exe chatz.exe ``` @@ -102,6 +102,16 @@ CONNECTION_URL= CHANNEL_ID= ``` +### System Environment Support +Chatz also support system environment variable. To use system environment variable then use `--from-env` or `-e` flag. +Name of the environment variable is same as `chatz.ini` config key, for example `export PROVIDER=slack`. + +```sh +chatz -e "Test" +# Or +chatz --from-env "Test" +``` + ## Usage - Send message using `default` profile ```sh diff --git a/main.go b/main.go index fda7a14..77b33bb 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,7 @@ func main() { var profile string var threadId string var output bool + var fromEnv bool app := cli.NewApp() app.Name = "chatz" @@ -52,6 +53,12 @@ func main() { Usage: "Print the version number", Destination: &version, }, + &cli.BoolFlag{ + Name: "from-env", + Aliases: []string{"e"}, + Usage: "To use config from environment variables", + Destination: &fromEnv, + }, } app.Action = func(ctx *cli.Context) error { if version { @@ -75,7 +82,7 @@ func main() { message = fmt.Sprintf("%s %s",message, a) } - env, err := utils.LoadEnv(profile) + env, err := utils.LoadEnv(profile, fromEnv) if err!=nil { return nil } diff --git a/utils/common.go b/utils/common.go index 5944bf2..bd92cb0 100644 --- a/utils/common.go +++ b/utils/common.go @@ -10,7 +10,39 @@ import ( ) -func LoadEnv(profile string) (*config.Config, error) { +func LoadEnv(profile string, fromEnv bool) (*config.Config, error) { + if fromEnv { + return loadEnvFromSystemEnv() + } else { + return loadEnvFromFile(profile) + } +} + +func loadEnvFromSystemEnv() (*config.Config, error) { + v := viper.New() + v.AutomaticEnv() + + // Get values from system environment + provider := v.GetString("PROVIDER") + token := v.GetString("TOKEN") + channelId := v.GetString("CHANNEL_ID") + webHookURL := v.GetString("WEB_HOOK_URL") + chatId := v.GetString("CHAT_ID") + connectionURL := v.GetString("CONNECTION_URL") + + var env config.Config + + env.Provider = provider + env.WebHookURL = webHookURL + env.Token = token + env.ChannelId = channelId + env.ChatId = chatId + env.ConnectionURL = connectionURL + + return &env, nil +} + +func loadEnvFromFile(profile string) (*config.Config, error) { // Get the home directory of the user homeDir, err := os.UserHomeDir() if err != nil { @@ -34,7 +66,7 @@ func LoadEnv(profile string) (*config.Config, error) { // Get values from the INI file provider := viper.GetString(fmt.Sprintf("%s.PROVIDER", profile)) - slackToken := viper.GetString(fmt.Sprintf("%s.TOKEN", profile)) + token := viper.GetString(fmt.Sprintf("%s.TOKEN", profile)) channelId := viper.GetString(fmt.Sprintf("%s.CHANNEL_ID", profile)) webHookURL := viper.GetString(fmt.Sprintf("%s.WEB_HOOK_URL", profile)) chatId := viper.GetString(fmt.Sprintf("%s.CHAT_ID", profile)) @@ -44,7 +76,7 @@ func LoadEnv(profile string) (*config.Config, error) { var env config.Config env.Provider = provider env.WebHookURL = webHookURL - env.Token = slackToken + env.Token = token env.ChannelId = channelId env.ChatId = chatId env.ConnectionURL = connectionURL