From d3fe8b6f5d9b1566e265d32a740806bd29a5c039 Mon Sep 17 00:00:00 2001 From: dd84ai Date: Sat, 8 Jun 2024 08:12:50 +0200 Subject: [PATCH] refactor: purge third party env libs --- .vscode/settings.example.json | 19 +++--- app/consoler/commands/root.go | 2 +- app/consoler/commands/shared.go | 2 +- app/consoler/consoler.go | 2 +- app/consoler/consoler_test.go | 8 +-- app/discorder/main.go | 2 +- app/listener/main.go | 4 +- app/management/run.go | 4 +- app/scrappy/base/api.go | 2 +- app/scrappy/baseattack/api.go | 2 +- app/scrappy/main.go | 2 +- app/scrappy/player/api.go | 2 +- app/settings/main.go | 100 ++++++++++++-------------------- app/settings/main_test.go | 9 --- app/viewer/viewer.go | 2 +- go.mod | 4 +- go.sum | 6 +- 17 files changed, 63 insertions(+), 109 deletions(-) delete mode 100644 app/settings/main_test.go diff --git a/.vscode/settings.example.json b/.vscode/settings.example.json index 12f12e3c..eeb333a8 100644 --- a/.vscode/settings.example.json +++ b/.vscode/settings.example.json @@ -3,30 +3,25 @@ "-v" ], "go.testEnvVars": { + "SCRAPPY_PLAYER_URL": "**********", // Not required for dev env if u use "DEVENV_MOCK_API": "true" + "SCRAPPY_BASE_URL": "**********", // Not required for dev env if u use "DEVENV_MOCK_API": "true" + // "DARK_TEST_REGENERATE": "false", "DEV_ENV": "true", - "TF_VAR_production_hcloud_token": "**********", // Needed only for deployment - "DOCKERHUB_USERNAME": "darkwind8", // Needed only for deployment - "DOCKERHUB_TOKEN": "**********", // Needed only for deployment - "DISCORDER_BOT_TOKEN": "**********", "DARKBOT_LOG_LEVEL": "INFO", "CONFIGURATOR_DBNAME": "dev", "DEVENV_MOCK_API": "true", - "SCRAPPY_PLAYER_URL": "**********", // Not required for dev env if u use "DEVENV_MOCK_API": "true" - "SCRAPPY_BASE_URL": "**********" // Not required for dev env if u use "DEVENV_MOCK_API": "true" + "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 "DEVENV_MOCK_API": "true" + "SCRAPPY_BASE_URL": "**********", // Not required for dev env if u use "DEVENV_MOCK_API": "true" // "DARK_TEST_REGENERATE": "false", "DEV_ENV": "true", - "TF_VAR_production_hcloud_token": "**********", // Needed only for deployment - "DOCKERHUB_USERNAME": "darkwind8", // Needed only for deployment - "DOCKERHUB_TOKEN": "**********", // Needed only for deployment - "DISCORDER_BOT_TOKEN": "**********", "DARKBOT_LOG_LEVEL": "INFO", "CONFIGURATOR_DBNAME": "dev", "DEVENV_MOCK_API": "true", - "SCRAPPY_PLAYER_URL": "**********", // Not required for dev env if u use "DEVENV_MOCK_API": "true" - "SCRAPPY_BASE_URL": "**********" // Not required for dev env if u use "DEVENV_MOCK_API": "true" + "DISCORDER_BOT_TOKEN": "**********", // Get your own app token at https://discord.com/developers/applications } } \ No newline at end of file diff --git a/app/consoler/commands/root.go b/app/consoler/commands/root.go index b118b7aa..2bb2f684 100644 --- a/app/consoler/commands/root.go +++ b/app/consoler/commands/root.go @@ -43,7 +43,7 @@ func CreateConsoler( consolerCmd, channelInfo, configur, - cmdgroup.Command(settings.Config.ConsolerPrefix), + cmdgroup.Command(settings.Env.ConsolerPrefix), cmdgroup.ShortDesc("Welcome to darkbot!"), ) root := newRootCommands(&rootGroup) diff --git a/app/consoler/commands/shared.go b/app/consoler/commands/shared.go index b97a00d5..549fe185 100644 --- a/app/consoler/commands/shared.go +++ b/app/consoler/commands/shared.go @@ -20,7 +20,7 @@ func CheckCommandAllowedToRun(cmd *cobra.Command, channels configurator.Configur } if !isChannelEnabled { - printer.Println(cmd, fmt.Sprintf("darkbot is not connected to this channel. Run `%s connect`", settings.Config.ConsolerPrefix)) + printer.Println(cmd, fmt.Sprintf("darkbot is not connected to this channel. Run `%s connect`", settings.Env.ConsolerPrefix)) return false } diff --git a/app/consoler/consoler.go b/app/consoler/consoler.go index 0943d3ab..9bce9329 100644 --- a/app/consoler/consoler.go +++ b/app/consoler/consoler.go @@ -36,7 +36,7 @@ func (c *Consoler) Execute( channelID types.DiscordChannelID, ) string { // only commands starting from prefix are allowed - if !strings.HasPrefix(cmd, settings.Config.ConsolerPrefix) { + if !strings.HasPrefix(cmd, settings.Env.ConsolerPrefix) { return "" } diff --git a/app/consoler/consoler_test.go b/app/consoler/consoler_test.go index e53c31f1..9d87eecc 100644 --- a/app/consoler/consoler_test.go +++ b/app/consoler/consoler_test.go @@ -13,7 +13,7 @@ import ( func TestGettingOutput(t *testing.T) { configurator.FixtureMigrator(func(dbpath types.Dbpath) { channelID, _ := configurator.FixtureChannel(dbpath) - assert.Contains(t, NewConsoler(dbpath).Execute(settings.Config.ConsolerPrefix+" ping", channelID), "Pong!") + assert.Contains(t, NewConsoler(dbpath).Execute(settings.Env.ConsolerPrefix+" ping", channelID), "Pong!") }) } @@ -21,7 +21,7 @@ func TestGrabStdout(t *testing.T) { configurator.FixtureMigrator(func(dbpath types.Dbpath) { channelID, _ := configurator.FixtureChannel(dbpath) c := NewConsoler(dbpath) - result := c.Execute(settings.Config.ConsolerPrefix+" ping --help", channelID) + result := c.Execute(settings.Env.ConsolerPrefix+" ping --help", channelID) assert.Contains(t, result, "\nFlags:\n -h, --help ") }) @@ -30,7 +30,7 @@ func TestGrabStdout(t *testing.T) { func TestAddBaseTag(t *testing.T) { configurator.FixtureMigrator(func(dbpath types.Dbpath) { channelID, _ := configurator.FixtureChannel(dbpath) - assert.Contains(t, NewConsoler(dbpath).Execute(settings.Config.ConsolerPrefix+` base tags add "bla bla" sdf`, channelID), "OK tags are added") + assert.Contains(t, NewConsoler(dbpath).Execute(settings.Env.ConsolerPrefix+` base tags add "bla bla" sdf`, channelID), "OK tags are added") }) } @@ -38,7 +38,7 @@ func TestSystemCommands(t *testing.T) { configurator.FixtureMigrator(func(dbpath types.Dbpath) { channelID, _ := configurator.FixtureChannel(dbpath) cons := NewConsoler(dbpath) - result := cons.Execute(settings.Config.ConsolerPrefix+` player --help`, channelID) + result := cons.Execute(settings.Env.ConsolerPrefix+` player --help`, channelID) _ = result assert.Contains(t, result, "System commands") }) diff --git a/app/discorder/main.go b/app/discorder/main.go index 2058d9e2..e42a5f5a 100644 --- a/app/discorder/main.go +++ b/app/discorder/main.go @@ -29,7 +29,7 @@ func (d *Discorder) GetDiscordSession() *discordgo.Session { func NewClient() *Discorder { d := &Discorder{} - dg, err := discordgo.New("Bot " + settings.Config.DiscorderBotToken) + dg, err := discordgo.New("Bot " + settings.Env.DiscorderBotToken) logus.Log.CheckFatal(err, "failed to init discord") dg.Identify.Intents = discordgo.IntentsGuildMessages diff --git a/app/listener/main.go b/app/listener/main.go index 372c5ea7..f8fa1f64 100644 --- a/app/listener/main.go +++ b/app/listener/main.go @@ -19,7 +19,7 @@ import ( ) func Run() { - dg, err := discordgo.New("Bot " + settings.Config.DiscorderBotToken) + dg, err := discordgo.New("Bot " + settings.Env.DiscorderBotToken) logus.Log.CheckFatal(err, "failed to init discord") // Register the messageCreate func as a callback for MessageCreate events. @@ -43,7 +43,7 @@ func allowedMessage(s *discordgo.Session, m *discordgo.MessageCreate) bool { messageAuthorID := m.Author.ID botCreatorID := "370435997974134785" - if !strings.HasPrefix(m.Content, settings.Config.ConsolerPrefix) { + if !strings.HasPrefix(m.Content, settings.Env.ConsolerPrefix) { return false } diff --git a/app/management/run.go b/app/management/run.go index 05f092a1..4740633a 100644 --- a/app/management/run.go +++ b/app/management/run.go @@ -35,7 +35,7 @@ var runCmd = &cobra.Command{ forumenacer := forumer.NewForumer(settings.Dbpath) var scrappy_storage *scrappy.ScrappyStorage - if settings.Config.DevEnvMockApi == "true" { + if settings.Env.DevEnvMockApi { scrappy_storage = scrappy.FixtureMockedStorage() } else { scrappy_storage = scrappy.NewScrappyWithApis() @@ -48,7 +48,7 @@ var runCmd = &cobra.Command{ go forumenacer.Run() // profiler - if settings.Config.ProfilingEnabled == settings.EnvTrue { + if settings.Env.ProfilingEnabled { p := profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.NoShutdownHook) defer p.Stop() diff --git a/app/scrappy/base/api.go b/app/scrappy/base/api.go index 8a98616a..1125c239 100644 --- a/app/scrappy/base/api.go +++ b/app/scrappy/base/api.go @@ -21,6 +21,6 @@ type IbaseAPI interface { func NewBaseApi() IbaseAPI { b := basesAPI{} - b.url = types.APIurl(settings.Config.ScrappyBaseUrl) + b.url = types.APIurl(settings.Env.ScrappyBaseUrl) return b } diff --git a/app/scrappy/baseattack/api.go b/app/scrappy/baseattack/api.go index 8d529ecb..ded60519 100644 --- a/app/scrappy/baseattack/api.go +++ b/app/scrappy/baseattack/api.go @@ -22,7 +22,7 @@ type IbaseAttackAPI interface { func NewBaseAttackAPI() IbaseAttackAPI { a := basesattackAPI{} - a.url = types.APIurl(settings.Config.ScrappyBaseAttackUrl) + a.url = types.APIurl(settings.Env.ScrappyBaseAttackUrl) return a } diff --git a/app/scrappy/main.go b/app/scrappy/main.go index 16c9b6e8..de3c2bf6 100644 --- a/app/scrappy/main.go +++ b/app/scrappy/main.go @@ -54,7 +54,7 @@ func (s *ScrappyStorage) Run() { logus.Log.Info("starting scrappy infinity update loop") for { s.Update() - time.Sleep(time.Duration(settings.ScrappyLoopDelay) * time.Second) + time.Sleep(time.Duration(settings.Env.ScrappyLoopDelay) * time.Second) } logus.Log.Info("gracefully shutdown scrappy infinity loop") } diff --git a/app/scrappy/player/api.go b/app/scrappy/player/api.go index e9840fd2..b6b335fc 100644 --- a/app/scrappy/player/api.go +++ b/app/scrappy/player/api.go @@ -21,6 +21,6 @@ type IPlayerAPI interface { func NewPlayerAPI() PlayerAPI { a := PlayerAPI{} - a.url = types.APIurl(settings.Config.ScrappyPlayerUrl) + a.url = types.APIurl(settings.Env.ScrappyPlayerUrl) return a } diff --git a/app/settings/main.go b/app/settings/main.go index 073bc869..a7e91aaa 100644 --- a/app/settings/main.go +++ b/app/settings/main.go @@ -2,92 +2,64 @@ package settings import ( "path/filepath" - "strconv" "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/utils_settings" - - "github.com/caarlos0/env/v6" - "github.com/joho/godotenv" -) - -const ( - EnvFalse = "false" - EnvTrue = "true" ) -type ConfigScheme struct { - ScrappyBaseUrl types.APIurl `env:"SCRAPPY_BASE_URL" envDefault:"undefined"` - ScrappyPlayerUrl types.APIurl `env:"SCRAPPY_PLAYER_URL" envDefault:"undefined"` - ScrappyBaseAttackUrl types.APIurl `env:"SCRAPPY_BASE_ATTACK_URL" envDefault:"https://discoverygc.com/forums/showthread.php?tid=110046&action=lastpost"` - - DiscorderBotToken string `env:"DISCORDER_BOT_TOKEN" envDefault:"undefined"` - - ConfiguratorDbname string `env:"CONFIGURATOR_DBNAME" envDefault:"dev"` +type DarkbotEnv struct { + utils_settings.UtilsEnvs - ConsolerPrefix string `env:"CONSOLER_PREFIX" envDefault:";"` - ProfilingEnabled string `env:"PROFILING" envDefault:"false"` + ScrappyBaseUrl string + ScrappyPlayerUrl string + ScrappyBaseAttackUrl string - ScrappyLoopDelay string `env:"SCRAPPY_LOOP_DELAY" envDefault:"10"` - ViewerLoopDelay string `env:"VIEWER_LOOP_DELAY" envDefault:"10"` - DevEnvMockApi string `env:"DEVENV_MOCK_API" envDefault:"true"` -} + DiscorderBotToken string -var ScrappyLoopDelay types.ScrappyLoopDelay -var ViewerLoopDelay types.ViewerLoopDelay -var Config ConfigScheme + ConfiguratorDbname string -var Dbpath types.Dbpath -var Workdir string + ConsolerPrefix string + ProfilingEnabled bool -func NewDBPath(dbname string) types.Dbpath { - return types.Dbpath(filepath.Join(Workdir, "data", dbname+".sqlite3")) + ScrappyLoopDelay int + ViewerLoopDelay int + DevEnvMockApi bool } -func load() { - logus.Log.Info("identifying folder of settings") - Workdir = filepath.Dir(filepath.Dir(utils.GetCurrentFolder().ToString())) - - err := godotenv.Load(filepath.Join(Workdir, ".env")) - if err == nil { - logus.Log.Info("loadded settings from .env") - } - - opts := env.Options{RequiredIfNoDef: true} - err = env.Parse(&Config, opts) - - logus.Log.CheckFatal(err, "settings have unset variable") +var Env DarkbotEnv - logus.Log.Debug("settings were downloaded. Scrappy base url=", logus.APIUrl(Config.ScrappyBaseUrl)) +func init() { + logus.Log.Info("attempt to load settings") - Dbpath = NewDBPath(Config.ConfiguratorDbname) + envs := utils_env.NewEnvConfig() + Env = DarkbotEnv{ + UtilsEnvs: utils_settings.Envs, + ScrappyBaseUrl: envs.GetEnvWithDefault("SCRAPPY_BASE_URL", "undefined"), + ScrappyPlayerUrl: envs.GetEnvWithDefault("SCRAPPY_PLAYER_URL", "undefined"), + ScrappyBaseAttackUrl: envs.GetEnvWithDefault("SCRAPPY_BASE_ATTACK_URL", "https://discoverygc.com/forums/showthread.php?tid=110046&action=lastpost"), - scrappy_loop_delay, err := strconv.Atoi(Config.ScrappyLoopDelay) - logus.Log.CheckFatal(err, "failed to parse ScrappyLoopDelay") - ScrappyLoopDelay = types.ScrappyLoopDelay(scrappy_loop_delay) + DiscorderBotToken: envs.GetEnvWithDefault("DISCORDER_BOT_TOKEN", "undefined"), - viewer_loop_delay, err := strconv.Atoi(Config.ViewerLoopDelay) - logus.Log.CheckFatal(err, "failed to parse ViewerLoopDelay") - ViewerLoopDelay = types.ViewerLoopDelay(viewer_loop_delay) + ConfiguratorDbname: envs.GetEnvWithDefault("CONFIGURATOR_DBNAME", "dev"), - logus.Log.Info("settings.ScrappyLoopDelay=", logus.ScrappyLoopDelay(ScrappyLoopDelay)) -} + ConsolerPrefix: envs.GetEnvWithDefault("CONSOLER_PREFIX", ";"), + ProfilingEnabled: envs.GetEnvBool("PROFILING"), -type DarkbotEnv struct { - utils_settings.UtilsEnvs + ScrappyLoopDelay: envs.GetIntWithDefault("SCRAPPY_LOOP_DELAY", 10), + ViewerLoopDelay: envs.GetIntWithDefault("VIEWER_LOOP_DELAY", 10), + DevEnvMockApi: envs.GetEnvBoolWithDefault("DEVENV_MOCK_API", true), + } + Workdir = filepath.Dir(filepath.Dir(utils.GetCurrentFolder().ToString())) + Dbpath = NewDBPath(Env.ConfiguratorDbname) } -var Env DarkbotEnv - -func init() { - logus.Log.Info("attempt to load settings") - //legacy - load() +var Dbpath types.Dbpath +var Workdir string - Env = DarkbotEnv{ - UtilsEnvs: utils_settings.Envs, - } +func NewDBPath(dbname string) types.Dbpath { + return types.Dbpath(filepath.Join(Workdir, "data", dbname+".sqlite3")) } diff --git a/app/settings/main_test.go b/app/settings/main_test.go deleted file mode 100644 index d9231387..00000000 --- a/app/settings/main_test.go +++ /dev/null @@ -1,9 +0,0 @@ -package settings - -import ( - "testing" -) - -func TestMain(t *testing.T) { - load() -} diff --git a/app/viewer/viewer.go b/app/viewer/viewer.go index 873d4edc..705c8871 100644 --- a/app/viewer/viewer.go +++ b/app/viewer/viewer.go @@ -30,7 +30,7 @@ func NewViewer(dbpath types.Dbpath, scrappy_storage *scrappy.ScrappyStorage) *Vi api: api, delays: ViewerDelays{ betweenChannels: 10, - betweenLoops: settings.ViewerLoopDelay, + betweenLoops: types.ViewerLoopDelay(settings.Env.ViewerLoopDelay), }, } diff --git a/go.mod b/go.mod index 83d53508..43273647 100644 --- a/go.mod +++ b/go.mod @@ -5,10 +5,8 @@ go 1.21.1 require ( github.com/anaskhan96/soup v1.2.5 github.com/bwmarrin/discordgo v0.27.1 - github.com/caarlos0/env/v6 v6.10.1 github.com/darklab8/go-typelog v0.3.3 - github.com/darklab8/go-utils v0.14.2 - github.com/joho/godotenv v1.5.1 + github.com/darklab8/go-utils v0.15.0 github.com/pkg/profile v1.7.0 github.com/spf13/cobra v1.8.0 github.com/stretchr/testify v1.8.4 diff --git a/go.sum b/go.sum index 31fced9a..14c067b6 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,6 @@ github.com/anaskhan96/soup v1.2.5 h1:V/FHiusdTrPrdF4iA1YkVxsOpdNcgvqT1hG+YtcZ5hM github.com/anaskhan96/soup v1.2.5/go.mod h1:6YnEp9A2yywlYdM4EgDz9NEHclocMepEtku7wg6Cq3s= github.com/bwmarrin/discordgo v0.27.1 h1:ib9AIc/dom1E/fSIulrBwnez0CToJE113ZGt4HoliGY= github.com/bwmarrin/discordgo v0.27.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= -github.com/caarlos0/env/v6 v6.10.1 h1:t1mPSxNpei6M5yAeu1qtRdPAK29Nbcf/n3G7x+b3/II= -github.com/caarlos0/env/v6 v6.10.1/go.mod h1:hvp/ryKXKipEkcuYjs9mI4bBCg+UI0Yhgm5Zu0ddvwc= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= @@ -13,6 +11,8 @@ github.com/darklab8/go-typelog v0.3.3 h1:ZBj26swTbqe0LtutxG2abnX5JJHz1QWOIvxM8GI github.com/darklab8/go-typelog v0.3.3/go.mod h1:AwwOf3dkp/tpevHFNbkB+PbwlDrUUgO1CVFkEnj+q5w= github.com/darklab8/go-utils v0.14.2 h1:wI3VzrX7nFqNWPwMFS86rvbXeUgyUN65Q5PZwoZ8Mis= github.com/darklab8/go-utils v0.14.2/go.mod h1:zApy0zIFiGCw496OIot7dl8i2cAsQMyPyf3PCMMpS3g= +github.com/darklab8/go-utils v0.15.0 h1:Xsgl7chxJoUYgWwu5NnVo8jSqggKoxazT3I8T7407tM= +github.com/darklab8/go-utils v0.15.0/go.mod h1:zApy0zIFiGCw496OIot7dl8i2cAsQMyPyf3PCMMpS3g= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -31,8 +31,6 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= -github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= -github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=