From 4e614107626d50394eb09a973e29b58d1949d494 Mon Sep 17 00:00:00 2001 From: dd84ai Date: Thu, 2 Nov 2023 00:35:41 +0100 Subject: [PATCH] refactor: all fmt.prints to logus --- app/configurator/channel_test.go | 5 ++--- app/configurator/fixtures.go | 4 ++-- app/configurator/tags.go | 7 ++----- app/configurator/tags_test.go | 4 ++-- app/consoler/commands/alerts.go | 30 +++++++++++++++++------------- app/consoler/commands/root.go | 2 +- app/consoler/commands/tags.go | 13 +++++++------ app/management/anounce.go | 2 +- app/management/check.go | 7 ------- app/scrappy/base/storage_test.go | 3 +-- app/scrappy/baseattack/api_test.go | 4 ++-- app/scrappy/player/storage_test.go | 3 +-- app/settings/logus/params.go | 18 ++++++++++++++++++ app/settings/utils/shell.go | 2 +- app/viewer/templ/base_test.go | 2 +- app/viewer/templ/player_test.go | 18 +++++++++--------- 16 files changed, 67 insertions(+), 57 deletions(-) diff --git a/app/configurator/channel_test.go b/app/configurator/channel_test.go index c7d333c..5128553 100644 --- a/app/configurator/channel_test.go +++ b/app/configurator/channel_test.go @@ -3,7 +3,6 @@ package configurator import ( "darkbot/app/settings/logus" "darkbot/app/settings/types" - "fmt" "testing" "github.com/stretchr/testify/assert" @@ -25,13 +24,13 @@ func TestChannels(t *testing.T) { cg.Remove("3") channels, _ = cg.List() - fmt.Println(channels) + logus.Debug("", logus.Items(channels, "channels")) assert.Len(t, channels, 2) cg.Add("3") channels, _ = cg.List() - fmt.Println(channels) + logus.Debug("", logus.Items(channels, "channels")) assert.Len(t, channels, 3) }) } diff --git a/app/configurator/fixtures.go b/app/configurator/fixtures.go index 2e81a65..3dc1081 100644 --- a/app/configurator/fixtures.go +++ b/app/configurator/fixtures.go @@ -2,9 +2,9 @@ package configurator import ( "darkbot/app/settings" + "darkbot/app/settings/logus" "darkbot/app/settings/types" "darkbot/app/settings/utils" - "fmt" "os" ) @@ -17,7 +17,7 @@ func FixtureMigrator(callback func(dbpath types.Dbpath)) Configurator { dbname := utils.TokenHex(8) dbpath := types.Dbpath(settings.NewDBPath(dbname)) // setup - fmt.Println(dbpath) + logus.Debug("", logus.Dbpath(dbpath)) os.Remove(string(dbpath)) os.Remove(string(dbpath) + "-shm") os.Remove(string(dbpath) + "-wal") diff --git a/app/configurator/tags.go b/app/configurator/tags.go index e84776b..d4aaed9 100644 --- a/app/configurator/tags.go +++ b/app/configurator/tags.go @@ -5,7 +5,6 @@ import ( "darkbot/app/settings/logus" "darkbot/app/settings/types" "darkbot/app/settings/utils" - "fmt" ) type IConfiguratorTags interface { @@ -101,10 +100,8 @@ func (c ConfiguratorTags[T]) TagsList(channelID types.DiscordChannelID) ([]types func (c ConfiguratorTags[T]) TagsClear(channelID types.DiscordChannelID) error { tags := []T{} result := c.db.Unscoped().Where("channel_id = ?", channelID).Find(&tags) - fmt.Println("Clear.Find.rowsAffected=", result.RowsAffected) - fmt.Println("Clear.Find.err=", result.Error) + logus.Debug("Clear.Find", logus.GormResult(result)) result = c.db.Unscoped().Delete(&tags) - fmt.Println("Clear.Delete.rowsAffected=", result.RowsAffected) - fmt.Println("Clear.Delete.err=", result.Error) + logus.Debug("Clear.Detete", logus.GormResult(result)) return result.Error } diff --git a/app/configurator/tags_test.go b/app/configurator/tags_test.go index eac6efb..353fd64 100644 --- a/app/configurator/tags_test.go +++ b/app/configurator/tags_test.go @@ -1,8 +1,8 @@ package configurator import ( + "darkbot/app/settings/logus" "darkbot/app/settings/types" - "fmt" "testing" "github.com/stretchr/testify/assert" @@ -44,7 +44,7 @@ func TestCanWriteRepeatedTagsPerChannels(t *testing.T) { assert.Error(t, err, "expected error to get in test") assert.Contains(t, err.Error(), "database already has those items") - fmt.Println("err=", err.Error()) + logus.Debug("err=", logus.OptError(err)) // make a test to check? :thinking: tags, _ = cg.TagsList("c2") diff --git a/app/consoler/commands/alerts.go b/app/consoler/commands/alerts.go index 418763b..cbf7090 100644 --- a/app/consoler/commands/alerts.go +++ b/app/consoler/commands/alerts.go @@ -4,7 +4,7 @@ import ( "darkbot/app/configurator" "darkbot/app/consoler/commands/cmdgroup" "darkbot/app/consoler/printer" - "fmt" + "darkbot/app/settings/logus" "strconv" "strings" @@ -33,17 +33,21 @@ func (t *alertThresholdCommands[T]) CreateSetAlertCmd() { Short: "Set alert (Works as set {number})", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { - fmt.Println("CreateSetAlertCmd.consoler running with args=", args) + logus.Debug("CreateSetAlertCmd.consoler running with args=", logus.Args(args)) printer.Println(cmd, "Attempting to parse input into integer number") rawInteger := args[0] integer, err := strconv.Atoi(rawInteger) + if logus.CheckWarn(err, "Atoi result with warning", logus.OptError(err)) { + printer.Println(cmd, "failed to parse value to integer. Value="+rawInteger) + } + printer.Println(cmd, "Parsed integer = "+strconv.Itoa(integer)) err = t.cfgTags.Set(t.GetChannelID(), integer) if err != nil { cmd.OutOrStdout().Write([]byte("ERR msg=" + err.Error())) return } - fmt.Println(len(args)) + logus.Debug("checking args again?", logus.Args(args)) printer.Println(cmd, "OK alert threshold is set") }, @@ -56,7 +60,7 @@ func (t *alertThresholdCommands[T]) CreateUnsetCmd() { Use: "unset", Short: "Unsert alert / Clear alert", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("CreateUnsetCmd.consoler running with args=", args) + logus.Debug("CreateUnsetCmd.consoler running with args=", logus.Args(args)) err := t.cfgTags.Unset(t.GetChannelID()) if err != nil { cmd.OutOrStdout().Write([]byte("ERR msg=" + err.Error())) @@ -73,7 +77,7 @@ func (t *alertThresholdCommands[T]) CreateStatusCmd() { Use: "status", Short: "Status of alert", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("CreateStatusCmd.consoler running with args=", args) + logus.Debug("CreateStatusCmd.consoler running with args=", logus.Args(args)) integer, err := t.cfgTags.Status(t.GetChannelID()) if err != nil { errMsg := err.Error() @@ -114,13 +118,13 @@ func (t *AlertBoolCommands[T]) CreateEnableCmd() { Use: "enable", Short: "Enable alert", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("CreateEnableCmd.consoler running with args=", args) + logus.Debug("CreateEnableCmd.consoler running with args=", logus.Args(args)) err := t.cfgTags.Enable(t.GetChannelID()) if err != nil { cmd.OutOrStdout().Write([]byte("ERR msg=" + err.Error())) return } - fmt.Println(len(args)) + logus.Debug("Create Enable is finished", logus.Args(args)) printer.Println(cmd, "OK alert is enabled") }, @@ -133,7 +137,7 @@ func (t *AlertBoolCommands[T]) CreateDisableCmd() { Use: "disable", Short: "Disable alert / Clear alert", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("CreateDisableCmd.consoler running with args=", args) + logus.Debug("CreateDisableCmd.consoler running with args=", logus.Args(args)) err := t.cfgTags.Disable(t.GetChannelID()) if err != nil { cmd.OutOrStdout().Write([]byte("ERR msg=" + err.Error())) @@ -150,7 +154,7 @@ func (t *AlertBoolCommands[T]) CreateStatusCmd() { Use: "status", Short: "Status of alert", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("CreateStatusCmd.consoler running with args=", args) + logus.Debug("CreateStatusCmd.consoler running with args=", logus.Args(args)) _, err := t.cfgTags.Status(t.GetChannelID()) if err != nil { errMsg := err.Error() @@ -194,14 +198,14 @@ func (t *AlertSetStringCommand[T]) CreateSetCmd() { Short: "Set Value (provide 'set StringValue')", Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { - fmt.Println("CreateSetAlertCmd.consoler running with args=", args) + logus.Debug("CreateSetAlertCmd.consoler running with args=", logus.Args(args)) str := args[0] err := t.cfgTags.Set(t.GetChannelID(), str) if err != nil { cmd.OutOrStdout().Write([]byte("ERR msg=" + err.Error())) return } - fmt.Println(len(args)) + logus.Debug("finished CreateSetCmd", logus.Args(args)) printer.Println(cmd, "OK value is set") }, @@ -214,7 +218,7 @@ func (t *AlertSetStringCommand[T]) CreateUnsetCmd() { Use: "unset", Short: "Unsert / Clear ", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("CreateUnsetCmd.consoler running with args=", args) + logus.Debug("CreateUnsetCmd.consoler running with args=", logus.Args(args)) err := t.cfgTags.Unset(t.GetChannelID()) if err != nil { cmd.OutOrStdout().Write([]byte("ERR msg=" + err.Error())) @@ -231,7 +235,7 @@ func (t *AlertSetStringCommand[T]) CreateStatusCmd() { Use: "status", Short: "Status", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("CreateStatusCmd.consoler running with args=", args) + logus.Debug("CreateStatusCmd.consoler running with args=", logus.Args(args)) str, err := t.cfgTags.Status(t.GetChannelID()) if err != nil { errMsg := err.Error() diff --git a/app/consoler/commands/root.go b/app/consoler/commands/root.go index 41898e7..ca92397 100644 --- a/app/consoler/commands/root.go +++ b/app/consoler/commands/root.go @@ -186,7 +186,7 @@ func (r *rootCommands) CreatePing() { Use: "ping", Short: "Check stuff is working", Run: func(cmd *cobra.Command, args []string) { - logus.Debug("ping called with args=", logus.Items(args, "args")) + logus.Debug("ping called with args=", logus.Args(args)) cmd.OutOrStdout().Write([]byte("Pong! from consoler")) }, } diff --git a/app/consoler/commands/tags.go b/app/consoler/commands/tags.go index eb54e01..a3936ad 100644 --- a/app/consoler/commands/tags.go +++ b/app/consoler/commands/tags.go @@ -4,6 +4,7 @@ import ( "darkbot/app/configurator" "darkbot/app/consoler/commands/cmdgroup" "darkbot/app/consoler/printer" + "darkbot/app/settings/logus" "darkbot/app/settings/types" "fmt" "strings" @@ -31,13 +32,13 @@ func (t *tagCommands) CreateTagAdd() { Short: "Add tags", Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { - fmt.Println("CreateTagAdd.consoler running with args=", args) + logus.Debug("CreateTagAdd.consoler running with args=", logus.Args(args)) err := t.cfgTags.TagsAdd(t.GetChannelID(), types.Tag(strings.Join(args, " "))) if err != nil { cmd.OutOrStdout().Write([]byte("ERR msg=" + err.Error())) return } - fmt.Println(len(args)) + logus.Debug("CreateTagAdd", logus.Args(args)) printer.Println(cmd, "OK tags are added") }, @@ -51,7 +52,7 @@ func (t *tagCommands) CreateTagRemove() { Short: "Remove tags", Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { - fmt.Println("CreateTagRemove.consoler running with args=", args) + logus.Debug("CreateTagRemove.consoler running with args=", logus.Args(args)) err := t.cfgTags.TagsRemove(t.GetChannelID(), types.Tag(strings.Join(args, " "))) if err != nil { cmd.OutOrStdout().Write([]byte("ERR msg=" + err.Error())) @@ -69,7 +70,7 @@ func (t *tagCommands) CreateTagClear() { Use: "clear", Short: "Clear tags", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("CreateTagClear.consoler running with args=", args) + logus.Debug("CreateTagClear.consoler running with args=", logus.Args(args)) err := t.cfgTags.TagsClear(t.GetChannelID()) if err != nil { cmd.OutOrStdout().Write([]byte("ERR msg=" + err.Error())) @@ -87,7 +88,7 @@ func (t *tagCommands) CreateTagList() { Use: "list", Short: "List tags", Run: func(cmd *cobra.Command, args []string) { - fmt.Println("CreateTagList.consoler running with args=", args) + logus.Debug("CreateTagList.consoler running with args=", logus.Args(args)) tags, cfgErr := t.cfgTags.TagsList(t.GetChannelID()) err := cfgErr if err != nil { @@ -95,7 +96,7 @@ func (t *tagCommands) CreateTagList() { return } - fmt.Println("tags=", tags) + logus.Debug("CreateTagList continuied", logus.Tags(tags)) var sb strings.Builder for number, tag := range tags { sb.WriteString(fmt.Sprintf("\"%s\"", tag)) diff --git a/app/management/anounce.go b/app/management/anounce.go index d3a4a85..cbf3531 100644 --- a/app/management/anounce.go +++ b/app/management/anounce.go @@ -19,7 +19,7 @@ var amounceCmd = &cobra.Command{ Use: "anounce", Short: "Anounce something", Run: func(cmd *cobra.Command, args []string) { - logus.Info("Anounce is called with args=", logus.Items(args, "args")) + logus.Info("Anounce is called with args=", logus.Args(args)) dg := discorder.NewClient() // go listener.Run() diff --git a/app/management/check.go b/app/management/check.go index 8e7ddb9..1fb619d 100644 --- a/app/management/check.go +++ b/app/management/check.go @@ -16,13 +16,6 @@ var checkCmd = &cobra.Command{ Short: "Experimental command", Run: func(cmd *cobra.Command, args []string) { logus.Info("check called") - // fmt.Println("result=", consoler.Consoler{}.New("").Execute().String()) - - // db := configurator.GetConnection() - // channel1 := configurator.Channel{gorm.Model{ID: 123}, ChannelID: "123"} - // db.FirstOrCreate(&channel1) - // fmt.Println(channel1) - dg := discorder.NewClient() // dg.SengMessage("838802002582175756", "123message") dg.GetLatestMessages("838802002582175756") diff --git a/app/scrappy/base/storage_test.go b/app/scrappy/base/storage_test.go index 8272e8e..4a82c22 100644 --- a/app/scrappy/base/storage_test.go +++ b/app/scrappy/base/storage_test.go @@ -2,7 +2,6 @@ package base import ( "darkbot/app/settings/logus" - "fmt" "testing" "github.com/stretchr/testify/assert" @@ -20,7 +19,7 @@ func TestGetBases(t *testing.T) { logus.CheckFatal(err, "not found latest base record") assert.True(t, len(bases.List) > 0) - fmt.Println(bases.List) + logus.Debug("", logus.Items(bases.List, "bases.List")) } func TestAddManyRecords(t *testing.T) { diff --git a/app/scrappy/baseattack/api_test.go b/app/scrappy/baseattack/api_test.go index fe45b1a..7e3feb4 100644 --- a/app/scrappy/baseattack/api_test.go +++ b/app/scrappy/baseattack/api_test.go @@ -1,7 +1,7 @@ package baseattack import ( - "fmt" + "darkbot/app/settings/logus" "strings" "testing" @@ -12,7 +12,7 @@ func TestAPI(t *testing.T) { api := NewBaseAttackAPI() result, _ := api.GetBaseAttackData() data := string(result) - fmt.Println(data) + logus.Debug(data) } func TestDetectLPAttack(t *testing.T) { diff --git a/app/scrappy/player/storage_test.go b/app/scrappy/player/storage_test.go index bc97bea..5a03852 100644 --- a/app/scrappy/player/storage_test.go +++ b/app/scrappy/player/storage_test.go @@ -2,7 +2,6 @@ package player import ( "darkbot/app/settings/logus" - "fmt" "testing" "github.com/stretchr/testify/assert" @@ -20,5 +19,5 @@ func TestGetPlayers(t *testing.T) { logus.CheckFatal(err, "not found latest base record") assert.True(t, len(bases.List) > 0) - fmt.Println(bases.List) + logus.Debug("", logus.Items(bases.List, "bases.List")) } diff --git a/app/settings/logus/params.go b/app/settings/logus/params.go index 747c90c..595985b 100644 --- a/app/settings/logus/params.go +++ b/app/settings/logus/params.go @@ -4,6 +4,8 @@ import ( "darkbot/app/settings/types" "fmt" "log/slog" + + "gorm.io/gorm" ) func logGroupFiles() slog.Attr { @@ -85,6 +87,14 @@ func Records[T any](value []T) slogParam { return Items[T](value, "records") } +func Args(value []string) slogParam { + return Items[string](value, "args") +} + +func Tags(value []types.Tag) slogParam { + return Items[types.Tag](value, "tags") +} + func APIUrl(value types.APIurl) slogParam { return func(c *slogGroup) { c.params["api_url"] = string(value) @@ -151,3 +161,11 @@ func Tag(value types.Tag) slogParam { c.params["tag"] = string(value) } } + +func GormResult(result *gorm.DB) slogParam { + return func(c *slogGroup) { + c.params["result.rows_affected"] = fmt.Sprintf("%d", result.RowsAffected) + c.params["result.error_msg"] = fmt.Sprintf("%v", result.Error) + c.params["result.error_type"] = fmt.Sprintf("%T", result.Error) + } +} diff --git a/app/settings/utils/shell.go b/app/settings/utils/shell.go index 27afa10..63020da 100644 --- a/app/settings/utils/shell.go +++ b/app/settings/utils/shell.go @@ -8,7 +8,7 @@ import ( ) func ShellRunArgs(program string, args ...string) { - fmt.Printf("OK attempting to run: %s %s\n", program, args) + logus.Debug(fmt.Sprintf("OK attempting to run: %s", program), logus.Args(args)) executable, _ := exec.LookPath(program) args = append([]string{""}, args...) diff --git a/app/viewer/templ/base_test.go b/app/viewer/templ/base_test.go index 1185127..245ba02 100644 --- a/app/viewer/templ/base_test.go +++ b/app/viewer/templ/base_test.go @@ -124,7 +124,7 @@ func TestBaseViewerRealData(t *testing.T) { base := NewTemplateBase(channelID, dbpath) base.Render() - fmt.Println(base.main.Content) + logus.Debug("base.main.Content=" + base.main.Content) }) } diff --git a/app/viewer/templ/player_test.go b/app/viewer/templ/player_test.go index e1a1f69..4f82bf4 100644 --- a/app/viewer/templ/player_test.go +++ b/app/viewer/templ/player_test.go @@ -5,8 +5,8 @@ import ( "darkbot/app/scrappy" "darkbot/app/scrappy/player" "darkbot/app/scrappy/shared/records" + "darkbot/app/settings/logus" "darkbot/app/settings/types" - "fmt" "testing" "github.com/stretchr/testify/assert" @@ -33,10 +33,10 @@ func TestPlayerViewerMadeUpData(t *testing.T) { playerView := NewTemplatePlayers(channelID, dbpath) playerView.Render() - fmt.Println(playerView.friends.MainTable.Content) - fmt.Println(playerView.enemies.MainTable.Content) - fmt.Println(playerView.neutral.MainTable.Content) - fmt.Println("test TestPlayerViewer is finished") + logus.Debug(playerView.friends.MainTable.Content) + logus.Debug(playerView.enemies.MainTable.Content) + logus.Debug(playerView.neutral.MainTable.Content) + logus.Debug("test TestPlayerViewer is finished") assert.NotEmpty(t, playerView.friends.MainTable.Content) assert.NotEmpty(t, playerView.enemies.MainTable.Content) @@ -77,10 +77,10 @@ func TestPlayerViewerRealData(t *testing.T) { playerView := NewTemplatePlayers(channelID, dbpath) playerView.Render() - fmt.Println(playerView.friends.MainTable.Content) - fmt.Println(playerView.enemies.MainTable.Content) - fmt.Println(playerView.neutral.MainTable.Content) + logus.Debug(playerView.friends.MainTable.Content) + logus.Debug(playerView.enemies.MainTable.Content) + logus.Debug(playerView.neutral.MainTable.Content) - fmt.Println("test TestPlayerViewer is finished") + logus.Debug("test TestPlayerViewer is finished") }) }