Skip to content

Commit

Permalink
refactor: purge third party env libs
Browse files Browse the repository at this point in the history
  • Loading branch information
dd84ai committed Jun 8, 2024
1 parent 482ee76 commit d3fe8b6
Show file tree
Hide file tree
Showing 17 changed files with 63 additions and 109 deletions.
19 changes: 7 additions & 12 deletions .vscode/settings.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion app/consoler/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/consoler/commands/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion app/consoler/consoler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}

Expand Down
8 changes: 4 additions & 4 deletions app/consoler/consoler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ 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!")
})
}

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 ")
})
Expand All @@ -30,15 +30,15 @@ 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")
})
}

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")
})
Expand Down
2 changes: 1 addition & 1 deletion app/discorder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions app/listener/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions app/management/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion app/scrappy/base/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion app/scrappy/baseattack/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion app/scrappy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
2 changes: 1 addition & 1 deletion app/scrappy/player/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
100 changes: 36 additions & 64 deletions app/settings/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
9 changes: 0 additions & 9 deletions app/settings/main_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion app/viewer/viewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
}

Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand All @@ -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=
Expand Down

0 comments on commit d3fe8b6

Please sign in to comment.