Skip to content

Commit

Permalink
chore: system env support added (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrasif authored Sep 24, 2024
1 parent 732318c commit 9b06574
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down Expand Up @@ -102,6 +102,16 @@ CONNECTION_URL=<redis-connection-url>
CHANNEL_ID=<redis-publish-channel>
```

### 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
Expand Down
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func main() {
var profile string
var threadId string
var output bool
var fromEnv bool

app := cli.NewApp()
app.Name = "chatz"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
38 changes: 35 additions & 3 deletions utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
Expand All @@ -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
Expand Down

0 comments on commit 9b06574

Please sign in to comment.