-
-
Notifications
You must be signed in to change notification settings - Fork 563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Allow multiple telegram IDs #52
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e4742b8
ChatGPT as conversation manager
dhilman 11b48f4
fix: sending of error from SendMessage. Separate bot into its own pac…
dhilman 52d9d3d
Resolve conflicts - add edit interval to tgbot.New
dhilman ade59c9
Merge branch 'm1guelpf:main' into main
dhilman 598864c
Add env config, load with viper
dhilman e6df945
Update config.Init - use viper instance rather than global
dhilman cec0daa
Allow multiple ids for TELEGRAM_ID env variable
dhilman 77818e4
Merge remote-tracking branch 'upstream/main'
dhilman 9fe6bac
Add tests for multiple ids being provided
dhilman 583db04
Test action
dhilman 2b4018e
Remove commented tests
dhilman 8b58cf9
Update LoadEnvConfig to work when no file is provided
dhilman 3d6318d
Merge remote-tracking branch 'upstream/main'
dhilman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should default to 1 right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defaults to 1 in the
Validate
method