From a1e48cc84b5ecc596fbd046b5acbf1f06c566f3e Mon Sep 17 00:00:00 2001 From: dd84ai Date: Sat, 8 Jun 2024 13:19:15 +0200 Subject: [PATCH] refactor: env vars to enverant --- .gitignore | 5 ++++- .vscode/enverant.example.json | 10 ++++++++++ .vscode/settings.example.json | 27 --------------------------- .vscode/settings.json | 9 +++++++++ README.md | 3 +-- app/forumer/forum_post_test.go | 5 ++--- app/forumer/forum_threads_test.go | 5 ++--- app/settings/main.go | 30 ++++++++++++++++-------------- 8 files changed, 44 insertions(+), 50 deletions(-) create mode 100644 .vscode/enverant.example.json delete mode 100644 .vscode/settings.example.json create mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index 24aed61..e03ac3c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,6 @@ /chromedriver /data/* !/data/.gitkeep -settings.json .terraform/ *.svg.bkp config @@ -16,3 +15,7 @@ __debug_bin cover.out go.work* .venv/ +!.vscode +.vscode/enverant.json +!.vscode/enverant.example.json +!.vscode/settings.json diff --git a/.vscode/enverant.example.json b/.vscode/enverant.example.json new file mode 100644 index 0000000..0c0274d --- /dev/null +++ b/.vscode/enverant.example.json @@ -0,0 +1,10 @@ +{ + // "SCRAPPY_PLAYER_URL": "**********", // Not required for dev env if u use "DEV_ENV_MOCK_API": "true" + // "SCRAPPY_BASE_URL": "**********", // Not required for dev env if u use "DEV_ENV_MOCK_API": "true" + // "DARK_TEST_REGENERATE": "false", + "DEV_ENV": "true", + "DEV_ENV_MOCK_API": "true", + "DARKBOT_LOG_LEVEL": "INFO", + "CONFIGURATOR_DBNAME": "dev", + "DISCORDER_BOT_TOKEN": "**********" // Get your own app token at https://discord.com/developers/applications +} \ No newline at end of file diff --git a/.vscode/settings.example.json b/.vscode/settings.example.json deleted file mode 100644 index ec9a61f..0000000 --- a/.vscode/settings.example.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "go.testFlags": [ - "-v" - ], - "go.testEnvVars": { - "SCRAPPY_PLAYER_URL": "**********", // Not required for dev env if u use "DEV_ENV_MOCK_API": "true" - "SCRAPPY_BASE_URL": "**********", // Not required for dev env if u use "DEV_ENV_MOCK_API": "true" - // "DARK_TEST_REGENERATE": "false", - "DEV_ENV": "true", - "DEV_ENV_MOCK_API": "true", - "DARKBOT_LOG_LEVEL": "INFO", - "CONFIGURATOR_DBNAME": "dev", - "DISCORDER_BOT_TOKEN": "**********", // Get your own app token at https://discord.com/developers/applications - }, - "go.testTimeout": "30000s", - "editor.formatOnSave": true, - "terminal.integrated.env.linux": { - "SCRAPPY_PLAYER_URL": "**********", // Not required for dev env if u use "DEV_ENV_MOCK_API": "true" - "SCRAPPY_BASE_URL": "**********", // Not required for dev env if u use "DEV_ENV_MOCK_API": "true" - // "DARK_TEST_REGENERATE": "false", - "DEV_ENV": "true", - "DEV_ENV_MOCK_API": "true", - "DARKBOT_LOG_LEVEL": "INFO", - "CONFIGURATOR_DBNAME": "dev", - "DISCORDER_BOT_TOKEN": "**********", // Get your own app token at https://discord.com/developers/applications - } -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..50a1fe3 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,9 @@ +{ + "go.testFlags": [ + "-v" + ], + "go.testEnvVars": {}, + "go.testTimeout": "30000s", + "editor.formatOnSave": true, + "terminal.integrated.env.linux": {} +} \ No newline at end of file diff --git a/README.md b/README.md index 60891f4..ae843ae 100755 --- a/README.md +++ b/README.md @@ -33,8 +33,7 @@ - If you are me, then just use your dev env inviting link you already created: - https://discord.com/api/oauth2/authorize?client_id=1071516990348460033&permissions=8&scope=bot -- Ensure you have set [Environment variables](./.vscode/settings.example.json) documented in .vscode - +- Ensure you have set necessary [Environment variables](./.vscode/enverant.example.json) in your own enverant.json file or as real environment variables - [App specific configurations](./app/settings/main.go) can be found here. - Install golang of version no less than it is mention in [test.yml file](.github/workflows/test.yml) - Launch app as `go run . run` (see `go run . --help` for other available commands) diff --git a/app/forumer/forum_post_test.go b/app/forumer/forum_post_test.go index 33c12ad..5e6e168 100644 --- a/app/forumer/forum_post_test.go +++ b/app/forumer/forum_post_test.go @@ -8,8 +8,7 @@ import ( "github.com/darklab8/fl-darkbot/app/forumer/forum_types" "github.com/darklab8/fl-darkbot/app/settings/logus" - - "github.com/darklab8/go-utils/utils" + "github.com/darklab8/go-utils/utils/utils_os" "github.com/stretchr/testify/assert" ) @@ -25,7 +24,7 @@ func FixtureLatestThread() *forum_types.LatestThread { } func FixtureDetailedRequester() func(mt MethodType, u forum_types.Url) (*QueryResult, error) { - detailed_post_content_filepath := filepath.Join(utils.GetCurrentFolder().ToString(), "test_data", "detailed_post_content.html") + detailed_post_content_filepath := filepath.Join(utils_os.GetCurrentFolder().ToString(), "test_data", "detailed_post_content.html") if _, err := os.Stat(detailed_post_content_filepath); err != nil { query, err := NewQuery("GET", "https://discoverygc.com/forums/showthread.php?tid=200175&action=lastpost") logus.Log.CheckFatal(err, "failed to create mock data") diff --git a/app/forumer/forum_threads_test.go b/app/forumer/forum_threads_test.go index e0471e7..2cc637c 100644 --- a/app/forumer/forum_threads_test.go +++ b/app/forumer/forum_threads_test.go @@ -7,14 +7,13 @@ import ( "github.com/darklab8/fl-darkbot/app/forumer/forum_types" "github.com/darklab8/fl-darkbot/app/settings/logus" - - "github.com/darklab8/go-utils/utils" + "github.com/darklab8/go-utils/utils/utils_os" "github.com/stretchr/testify/assert" ) func FixtureMockedThreadsRequester() func(mt MethodType, u forum_types.Url) (*QueryResult, error) { - thread_post_content_filepath := filepath.Join(utils.GetCurrentFolder().ToString(), "test_data", "latest_threads.html") + thread_post_content_filepath := filepath.Join(utils_os.GetCurrentFolder().ToString(), "test_data", "latest_threads.html") if _, err := os.Stat(thread_post_content_filepath); err != nil { query, err := NewQuery("GET", ThreadPageURL) logus.Log.CheckFatal(err, "failed to create mock data") diff --git a/app/settings/main.go b/app/settings/main.go index ee48d3d..4420c00 100644 --- a/app/settings/main.go +++ b/app/settings/main.go @@ -6,8 +6,8 @@ import ( "github.com/darklab8/fl-darkbot/app/settings/logus" "github.com/darklab8/fl-darkbot/app/settings/types" - "github.com/darklab8/go-utils/utils" - "github.com/darklab8/go-utils/utils/utils_env" + "github.com/darklab8/go-utils/utils/enverant" + "github.com/darklab8/go-utils/utils/utils_os" "github.com/darklab8/go-utils/utils/utils_settings" ) @@ -35,25 +35,27 @@ var Env DarkbotEnv func init() { logus.Log.Info("attempt to load settings") - envs := utils_env.NewEnvConfig() + envs := enverant.NewEnverant( + enverant.WithEnvFile(utils_os.GetCurrentFolder().Dir().Dir().Join(".vscode", "enverant.json").ToString()), + ) Env = DarkbotEnv{ UtilsEnvs: utils_settings.Envs, - DevEnvMockApi: envs.GetEnvBoolWithDefault("DEV_ENV_MOCK_API", true), - ScrappyBaseUrl: envs.GetEnvWithDefault("SCRAPPY_BASE_URL", ""), - ScrappyPlayerUrl: envs.GetEnvWithDefault("SCRAPPY_PLAYER_URL", ""), - ScrappyBaseAttackUrl: envs.GetEnvWithDefault("SCRAPPY_BASE_ATTACK_URL", "https://discoverygc.com/forums/showthread.php?tid=110046&action=lastpost"), + DevEnvMockApi: envs.GetBoolOr("DEV_ENV_MOCK_API", true), + ScrappyBaseUrl: envs.GetStrOr("SCRAPPY_BASE_URL", ""), + ScrappyPlayerUrl: envs.GetStrOr("SCRAPPY_PLAYER_URL", ""), + ScrappyBaseAttackUrl: envs.GetStrOr("SCRAPPY_BASE_ATTACK_URL", "https://discoverygc.com/forums/showthread.php?tid=110046&action=lastpost"), - DiscorderBotToken: envs.GetEnvWithDefault("DISCORDER_BOT_TOKEN", ""), + DiscorderBotToken: envs.GetStrOr("DISCORDER_BOT_TOKEN", ""), - ConfiguratorDbname: envs.GetEnvWithDefault("CONFIGURATOR_DBNAME", "dev"), + ConfiguratorDbname: envs.GetStrOr("CONFIGURATOR_DBNAME", "dev"), - ConsolerPrefix: envs.GetEnvWithDefault("CONSOLER_PREFIX", ";"), - ProfilingEnabled: envs.GetEnvBoolWithDefault("PROFILING", false), + ConsolerPrefix: envs.GetStrOr("CONSOLER_PREFIX", ";"), + ProfilingEnabled: envs.GetBoolOr("PROFILING", false), - ScrappyLoopDelay: envs.GetIntWithDefault("SCRAPPY_LOOP_DELAY", 10), - ViewerLoopDelay: envs.GetIntWithDefault("VIEWER_LOOP_DELAY", 10), + ScrappyLoopDelay: envs.GetIntOr("SCRAPPY_LOOP_DELAY", 10), + ViewerLoopDelay: envs.GetIntOr("VIEWER_LOOP_DELAY", 10), } - Workdir = filepath.Dir(filepath.Dir(utils.GetCurrentFolder().ToString())) + Workdir = filepath.Dir(filepath.Dir(utils_os.GetCurrentFolder().ToString())) Dbpath = NewDBPath(Env.ConfiguratorDbname) if !Env.DevEnvMockApi {