-
-
Notifications
You must be signed in to change notification settings - Fork 562
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow multiple telegram IDs (#52)
* ChatGPT as conversation manager * fix: sending of error from SendMessage. Separate bot into its own package * Resolve conflicts - add edit interval to tgbot.New * Add env config, load with viper * Update config.Init - use viper instance rather than global * Allow multiple ids for TELEGRAM_ID env variable * Add tests for multiple ids being provided * Test action * Remove commented tests * Update LoadEnvConfig to work when no file is provided
- Loading branch information
Showing
9 changed files
with
271 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: Test | ||
on: [push, pull_request] | ||
jobs: | ||
go-test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Check out source code | ||
uses: actions/checkout@v3 | ||
- name: Setup | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version-file: "go.mod" | ||
cache: true | ||
- name: Test | ||
run: go test -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func createFile(name string, content string) (remove func(), err error) { | ||
f, err := os.Create(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
if _, err := f.WriteString(content); err != nil { | ||
return nil, err | ||
} | ||
|
||
return func() { | ||
if err := os.Remove(name); err != nil { | ||
panic(fmt.Sprintf("failed to remove file: %s", err)) | ||
} | ||
}, nil | ||
} | ||
|
||
func setEnvVariables(vals map[string]string) func() { | ||
for k, v := range vals { | ||
os.Setenv(k, v) | ||
} | ||
return func() { | ||
for k := range vals { | ||
os.Unsetenv(k) | ||
} | ||
} | ||
} | ||
|
||
func TestLoadEnvConfig(t *testing.T) { | ||
for label, test := range map[string]struct { | ||
fileContent string | ||
envVars map[string]string | ||
want *EnvConfig | ||
}{ | ||
"all values empty in file and env": { | ||
fileContent: `TELEGRAM_ID= | ||
TELEGRAM_TOKEN= | ||
EDIT_WAIT_SECONDS=`, | ||
want: &EnvConfig{ | ||
TelegramID: []int64{}, | ||
TelegramToken: "", | ||
EditWaitSeconds: 0, | ||
}, | ||
}, | ||
"no file, all values through env": { | ||
envVars: map[string]string{ | ||
"TELEGRAM_ID": "123,456", | ||
"TELEGRAM_TOKEN": "token", | ||
"EDIT_WAIT_SECONDS": "10", | ||
}, | ||
want: &EnvConfig{ | ||
TelegramID: []int64{123, 456}, | ||
TelegramToken: "token", | ||
EditWaitSeconds: 10, | ||
}, | ||
}, | ||
"all values provided in file, single TELEGRAM_ID": { | ||
fileContent: `TELEGRAM_ID=123 | ||
TELEGRAM_TOKEN=abc | ||
EDIT_WAIT_SECONDS=10`, | ||
want: &EnvConfig{ | ||
TelegramID: []int64{123}, | ||
TelegramToken: "abc", | ||
EditWaitSeconds: 10, | ||
}, | ||
}, | ||
"multiple TELEGRAM_IDs provided in file": { | ||
fileContent: `TELEGRAM_ID=123,456 | ||
TELEGRAM_TOKEN=abc | ||
EDIT_WAIT_SECONDS=10`, | ||
envVars: map[string]string{}, | ||
want: &EnvConfig{ | ||
TelegramID: []int64{123, 456}, | ||
TelegramToken: "abc", | ||
EditWaitSeconds: 10, | ||
}, | ||
}, | ||
"env variables should override file values": { | ||
fileContent: `TELEGRAM_ID=123 | ||
TELEGRAM_TOKEN=abc | ||
EDIT_WAIT_SECONDS=10`, | ||
envVars: map[string]string{ | ||
"TELEGRAM_ID": "456", | ||
"TELEGRAM_TOKEN": "def", | ||
"EDIT_WAIT_SECONDS": "20", | ||
}, | ||
want: &EnvConfig{ | ||
TelegramID: []int64{456}, | ||
TelegramToken: "def", | ||
EditWaitSeconds: 20, | ||
}, | ||
}, | ||
"multiple TELEGRAM_IDs provided in env": { | ||
fileContent: `TELEGRAM_ID=123 | ||
TELEGRAM_TOKEN=abc | ||
EDIT_WAIT_SECONDS=10`, | ||
envVars: map[string]string{ | ||
"TELEGRAM_ID": "456,789", | ||
}, | ||
want: &EnvConfig{ | ||
TelegramID: []int64{456, 789}, | ||
TelegramToken: "abc", | ||
EditWaitSeconds: 10, | ||
}, | ||
}, | ||
} { | ||
t.Run(label, func(t *testing.T) { | ||
unset := setEnvVariables(test.envVars) | ||
t.Cleanup(unset) | ||
|
||
if test.fileContent != "" { | ||
remove, err := createFile("test.env", test.fileContent) | ||
require.NoError(t, err) | ||
t.Cleanup(remove) | ||
} | ||
|
||
cfg, err := LoadEnvConfig("test.env") | ||
require.NoError(t, err) | ||
require.Equal(t, test.want, cfg) | ||
}) | ||
} | ||
} |
Oops, something went wrong.