Skip to content

Commit

Permalink
refactor: logus is now in shared lib
Browse files Browse the repository at this point in the history
  • Loading branch information
dd84ai committed Jan 2, 2024
1 parent a44c105 commit a98a331
Show file tree
Hide file tree
Showing 81 changed files with 497 additions and 716 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ heap.*.pprof
.env.secret
__debug_bin
cover.out
go.work*
4 changes: 2 additions & 2 deletions app/configurator/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package configurator

import (
"darkbot/app/configurator/models"
"darkbot/app/settings/logus"
"darkbot/app/settings/darkbot_logus"
"darkbot/app/settings/types"
)

Expand Down Expand Up @@ -169,7 +169,7 @@ func (c IConfiguratorAlertString[T]) Set(channelID types.DiscordChannelID, value
func (c IConfiguratorAlertString[T]) Unset(channelID types.DiscordChannelID) error {
objs := []T{}
result := c.db.Unscoped().Where("channel_id = ?", channelID).Find(&objs)
logus.CheckWarn(result.Error, "attempted to unset with errors", logus.GormResult(result))
darkbot_logus.Log.CheckWarn(result.Error, "attempted to unset with errors", darkbot_logus.GormResult(result))
result = c.db.Unscoped().Delete(&objs)
return result.Error
}
Expand Down
6 changes: 3 additions & 3 deletions app/configurator/alerts_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package configurator

import (
"darkbot/app/settings/logus"
"darkbot/app/settings/darkbot_logus"
"darkbot/app/settings/types"
"fmt"
"testing"
Expand All @@ -17,7 +17,7 @@ func TestAlertTreshold(t *testing.T) {

cfg := NewCfgAlertNeutralPlayersGreaterThan(genericCfg)
status, err := cfg.Status(channelID)
logus.Debug(fmt.Sprintf("status=%v", status))
darkbot_logus.Log.Debug(fmt.Sprintf("status=%v", status))
assert.Error(t, err, ErrorZeroAffectedRowsMsg)
assert.ErrorContains(t, err, ErrorZeroAffectedRowsMsg)

Expand All @@ -41,7 +41,7 @@ func TestAlertBool(t *testing.T) {

cfg := NewCfgAlertBaseIsUnderAttack(genericCfg)
status, _ := cfg.Status(channelID)
logus.Debug(fmt.Sprintf("status=%t", status))
darkbot_logus.Log.Debug(fmt.Sprintf("status=%t", status))
assert.False(t, status, "status is not true. failed aert")

cfg.Enable(channelID)
Expand Down
8 changes: 5 additions & 3 deletions app/configurator/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package configurator

import (
"darkbot/app/configurator/models"
"darkbot/app/settings/logus"
"darkbot/app/settings/darkbot_logus"
"darkbot/app/settings/types"
"darkbot/app/settings/utils"

"github.com/darklab8/darklab_goutils/goutils/utils_logus"
)

func NewConfiguratorChannel(con *Configurator) ConfiguratorChannel {
Expand All @@ -23,13 +25,13 @@ func (c ConfiguratorChannel) Add(channelID types.DiscordChannelID) error {
}

if result.Error != nil {
logus.Info("channels.Add", logus.OptError(result.Error))
darkbot_logus.Log.Info("channels.Add", utils_logus.OptError(result.Error))
}

channel := models.Channel{ChannelID: channelID}
result = c.db.Create(&channel)
if result.Error != nil {
logus.Info("channels.Add.Error2=", logus.OptError(result.Error))
darkbot_logus.Log.Info("channels.Add.Error2=", utils_logus.OptError(result.Error))
}
return result.Error
}
Expand Down
9 changes: 5 additions & 4 deletions app/configurator/channel_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package configurator

import (
"darkbot/app/settings/logus"
"darkbot/app/settings/darkbot_logus"
"darkbot/app/settings/types"
"testing"

"github.com/darklab8/darklab_goutils/goutils/utils_logus"
"github.com/stretchr/testify/assert"
)

Expand All @@ -18,19 +19,19 @@ func TestChannels(t *testing.T) {
cg.Add("3")

channels, _ := cg.List()
logus.Debug("invoked List", logus.Items(channels, "channels"))
darkbot_logus.Log.Debug("invoked List", utils_logus.Items(channels, "channels"))
assert.Len(t, channels, 3)

cg.Remove("3")

channels, _ = cg.List()
logus.Debug("", logus.Items(channels, "channels"))
darkbot_logus.Log.Debug("", utils_logus.Items(channels, "channels"))
assert.Len(t, channels, 2)

cg.Add("3")

channels, _ = cg.List()
logus.Debug("", logus.Items(channels, "channels"))
darkbot_logus.Log.Debug("", utils_logus.Items(channels, "channels"))
assert.Len(t, channels, 3)
})
}
9 changes: 5 additions & 4 deletions app/configurator/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ package configurator
import (
"darkbot/app/configurator/models"
"darkbot/app/settings"
"darkbot/app/settings/logus"
"darkbot/app/settings/darkbot_logus"
"darkbot/app/settings/types"

"github.com/darklab8/darklab_goutils/goutils/utils_logus"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
Expand All @@ -30,7 +31,7 @@ func NewConfigurator(dbpath types.Dbpath) *Configurator {
db, err := gorm.Open(
sqlite.Open(string(dbpath)+"?cache=shared&mode=rwc&_journal_mode=WAL"), &gorm.Config{},
)
logus.CheckFatal(err, "failed to connect database at dbpath=", logus.Dbpath(settings.Dbpath))
darkbot_logus.Log.CheckFatal(err, "failed to connect database at dbpath=", darkbot_logus.Dbpath(settings.Dbpath))

return &Configurator{db: db, dbpath: dbpath}
}
Expand All @@ -57,8 +58,8 @@ func (cg *Configurator) AutoMigrateSchema() *Configurator {
&models.AlertPingMessage{},
&models.ConfigBaseOrderingKey{},
)
if !logus.CheckWarn(err, "AutoMigrateSchema was executed with problems", logus.OptError(err)) {
logus.Info("AutoMigrateSchema was executed fine")
if !darkbot_logus.Log.CheckWarn(err, "AutoMigrateSchema was executed with problems", utils_logus.OptError(err)) {
darkbot_logus.Log.Info("AutoMigrateSchema was executed fine")
}
return cg
}
4 changes: 2 additions & 2 deletions app/configurator/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package configurator

import (
"darkbot/app/settings"
"darkbot/app/settings/logus"
"darkbot/app/settings/darkbot_logus"
"darkbot/app/settings/types"
"darkbot/app/settings/utils"
"os"
Expand All @@ -17,7 +17,7 @@ func FixtureMigrator(callback func(dbpath types.Dbpath)) *Configurator {
dbname := utils.TokenHex(8)
dbpath := types.Dbpath(settings.NewDBPath(dbname))
// setup
logus.Debug("", logus.Dbpath(dbpath))
darkbot_logus.Log.Debug("", darkbot_logus.Dbpath(dbpath))
os.Remove(string(dbpath))
os.Remove(string(dbpath) + "-shm")
os.Remove(string(dbpath) + "-wal")
Expand Down
8 changes: 5 additions & 3 deletions app/configurator/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package configurator

import (
"darkbot/app/discorder"
"darkbot/app/settings/logus"
"darkbot/app/settings/darkbot_logus"
"darkbot/app/settings/types"
"fmt"

"github.com/darklab8/darklab_goutils/goutils/utils_logus"
)

func GetPingingMessage(ChannelID types.DiscordChannelID, configurator *Configurators, Discorder *discorder.Discorder) types.PingMessage {
pingMessageStr, err := configurator.Alerts.PingMessage.Status(ChannelID)
pingMessage := types.PingMessage(pingMessageStr)
logus.Debug("RenderAlertTemplate.PingMessage.Status", logus.OptError(err), logus.PingMessage(pingMessage))
darkbot_logus.Log.Debug("RenderAlertTemplate.PingMessage.Status", utils_logus.OptError(err), darkbot_logus.PingMessage(pingMessage))
if err != nil {
ownerID, err := Discorder.GetOwnerID(ChannelID)
if logus.CheckWarn(err, "unable to acquire Discorder Channel Owner", logus.ChannelID(ChannelID)) {
if darkbot_logus.Log.CheckWarn(err, "unable to acquire Discorder Channel Owner", darkbot_logus.ChannelID(ChannelID)) {
ownerID = "TestOwnerID"
}
pingMessage = types.PingMessage(fmt.Sprintf("<@%s>", ownerID))
Expand Down
14 changes: 7 additions & 7 deletions app/configurator/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package configurator

import (
"darkbot/app/configurator/models"
"darkbot/app/settings/logus"
"darkbot/app/settings/darkbot_logus"
"darkbot/app/settings/types"
"darkbot/app/settings/utils"
)
Expand Down Expand Up @@ -92,14 +92,14 @@ func (c ConfiguratorTags[T]) TagsAdd(channelID types.DiscordChannelID, tags ...t
for _, tag := range presentTags {
for _, newtag := range tags {
if tag == newtag {
logus.Info("TagsAdd. Tag %s is already present in channelID=%s\n", logus.Tag(tag), logus.ChannelID(channelID))
darkbot_logus.Log.Info("TagsAdd. Tag %s is already present in channelID=%s\n", darkbot_logus.Tag(tag), darkbot_logus.ChannelID(channelID))
return StorageErrorExists{items: []string{string(tag)}}
}
}
}

res := c.db.Create(objs)
logus.CheckWarn(res.Error, "unsuccesful result of c.db.Create")
darkbot_logus.Log.CheckWarn(res.Error, "unsuccesful result of c.db.Create")
return res.Error
}

Expand All @@ -108,7 +108,7 @@ func (c ConfiguratorTags[T]) TagsRemove(channelID types.DiscordChannelID, tags .
TotalRowsAffected := 0
for _, tag := range tags {
result := c.db.Where("channel_id = ? AND tag = ?", channelID, tag).Delete(&T{})
logus.CheckWarn(result.Error, "unsuccesful result of c.db.Delete")
darkbot_logus.Log.CheckWarn(result.Error, "unsuccesful result of c.db.Delete")
errors.Append(result.Error)
TotalRowsAffected += int(result.RowsAffected)
}
Expand All @@ -123,7 +123,7 @@ func (c ConfiguratorTags[T]) TagsRemove(channelID types.DiscordChannelID, tags .
func (c ConfiguratorTags[T]) TagsList(channelID types.DiscordChannelID) ([]types.Tag, error) {
objs := []T{}
result := c.db.Where("channel_id = ?", channelID).Find(&objs)
logus.CheckWarn(result.Error, "unsuccesful result of c.db.Find")
darkbot_logus.Log.CheckWarn(result.Error, "unsuccesful result of c.db.Find")

tags := utils.CompL(objs,
func(x T) types.Tag { return x.GetTag() })
Expand All @@ -141,8 +141,8 @@ func (c ConfiguratorTags[T]) TagsClear(channelID types.DiscordChannelID) error {
if len(tags) == 0 {
return ErrorZeroAffectedRows{ExtraMsg: "no tags found"}
}
logus.Debug("Clear.Find", logus.GormResult(result))
darkbot_logus.Log.Debug("Clear.Find", darkbot_logus.GormResult(result))
result = c.db.Unscoped().Delete(&tags)
logus.Debug("Clear.Detete", logus.GormResult(result))
darkbot_logus.Log.Debug("Clear.Detete", darkbot_logus.GormResult(result))
return result.Error
}
5 changes: 3 additions & 2 deletions app/configurator/tags_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package configurator

import (
"darkbot/app/settings/logus"
"darkbot/app/settings/darkbot_logus"
"darkbot/app/settings/types"
"testing"

"github.com/darklab8/darklab_goutils/goutils/utils_logus"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -44,7 +45,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")
logus.Debug("err=", logus.OptError(err))
darkbot_logus.Log.Debug("err=", utils_logus.OptError(err))

// make a test to check? :thinking:
tags, _ = cg.TagsList("c2")
Expand Down
29 changes: 15 additions & 14 deletions app/consoler/commands/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"darkbot/app/configurator"
"darkbot/app/consoler/commands/cmdgroup"
"darkbot/app/consoler/printer"
"darkbot/app/settings/logus"
"darkbot/app/settings/darkbot_logus"
"darkbot/app/settings/types"
"darkbot/app/settings/utils"
"strconv"
"strings"

"github.com/darklab8/darklab_goutils/goutils/utils_logus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -37,15 +38,15 @@ func (t *alertThresholdCommands[T]) CreateSetAlertCmd() {
Short: "Set alert (Works as set {number})",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
logus.Debug("CreateSetAlertCmd.consoler running with args=", logus.Args(args))
darkbot_logus.Log.Debug("CreateSetAlertCmd.consoler running with args=", darkbot_logus.Args(args))
if !CheckCommandAllowedToRun(cmd, t.channels, t.GetChannelID()) {
return
}

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)) {
if darkbot_logus.Log.CheckWarn(err, "Atoi result with warning", utils_logus.OptError(err)) {
printer.Println(cmd, "failed to parse value to integer. Value="+rawInteger)
}

Expand All @@ -55,7 +56,7 @@ func (t *alertThresholdCommands[T]) CreateSetAlertCmd() {
printer.Println(cmd, "ERR msg="+err.Error())
return
}
logus.Debug("checking args again?", logus.Args(args))
darkbot_logus.Log.Debug("checking args again?", darkbot_logus.Args(args))

printer.Println(cmd, "OK alert threshold is set")
},
Expand All @@ -68,7 +69,7 @@ func (t *alertThresholdCommands[T]) CreateUnsetCmd() {
Use: "unset",
Short: "Unsert alert / Clear alert",
Run: func(cmd *cobra.Command, args []string) {
logus.Debug("CreateUnsetCmd.consoler running with args=", logus.Args(args))
darkbot_logus.Log.Debug("CreateUnsetCmd.consoler running with args=", darkbot_logus.Args(args))
if !CheckCommandAllowedToRun(cmd, t.channels, t.GetChannelID()) {
return
}
Expand All @@ -93,7 +94,7 @@ func (t *alertThresholdCommands[T]) CreateStatusCmd() {
Use: "status",
Short: "Status of alert",
Run: func(cmd *cobra.Command, args []string) {
logus.Debug("CreateStatusCmd.consoler running with args=", logus.Args(args))
darkbot_logus.Log.Debug("CreateStatusCmd.consoler running with args=", darkbot_logus.Args(args))
if !CheckCommandAllowedToRun(cmd, t.channels, t.GetChannelID()) {
return
}
Expand Down Expand Up @@ -140,7 +141,7 @@ func (t *AlertBoolCommands[T]) CreateEnableCmd() {
Use: "enable",
Short: "Enable alert",
Run: func(cmd *cobra.Command, args []string) {
logus.Debug("CreateEnableCmd.consoler running with args=", logus.Args(args))
darkbot_logus.Log.Debug("CreateEnableCmd.consoler running with args=", darkbot_logus.Args(args))
if !CheckCommandAllowedToRun(cmd, t.channels, t.GetChannelID()) {
return
}
Expand All @@ -154,7 +155,7 @@ func (t *AlertBoolCommands[T]) CreateEnableCmd() {
}
return
}
logus.Debug("Create Enable is finished", logus.Args(args))
darkbot_logus.Log.Debug("Create Enable is finished", darkbot_logus.Args(args))

printer.Println(cmd, "OK alert is enabled")
},
Expand All @@ -167,7 +168,7 @@ func (t *AlertBoolCommands[T]) CreateDisableCmd() {
Use: "disable",
Short: "Disable alert / Clear alert",
Run: func(cmd *cobra.Command, args []string) {
logus.Debug("CreateDisableCmd.consoler running with args=", logus.Args(args))
darkbot_logus.Log.Debug("CreateDisableCmd.consoler running with args=", darkbot_logus.Args(args))
if !CheckCommandAllowedToRun(cmd, t.channels, t.GetChannelID()) {
return
}
Expand All @@ -192,7 +193,7 @@ func (t *AlertBoolCommands[T]) CreateStatusCmd() {
Use: "status",
Short: "Status of alert",
Run: func(cmd *cobra.Command, args []string) {
logus.Debug("CreateStatusCmd.consoler running with args=", logus.Args(args))
darkbot_logus.Log.Debug("CreateStatusCmd.consoler running with args=", darkbot_logus.Args(args))
if !CheckCommandAllowedToRun(cmd, t.channels, t.GetChannelID()) {
return
}
Expand Down Expand Up @@ -243,7 +244,7 @@ func (t *AlertSetStringCommand[T]) CreateSetCmd(allowed_order_keys []types.Order
Short: "Set Value (provide 'set StringValue')",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
logus.Debug("CreateSetAlertCmd.consoler running with args=", logus.Args(args))
darkbot_logus.Log.Debug("CreateSetAlertCmd.consoler running with args=", darkbot_logus.Args(args))
if !CheckCommandAllowedToRun(cmd, t.channels, t.GetChannelID()) {
return
}
Expand All @@ -270,7 +271,7 @@ func (t *AlertSetStringCommand[T]) CreateSetCmd(allowed_order_keys []types.Order
printer.Println(cmd, "ERR msg="+err.Error())
return
}
logus.Debug("finished CreateSetCmd", logus.Args(args))
darkbot_logus.Log.Debug("finished CreateSetCmd", darkbot_logus.Args(args))

printer.Println(cmd, "OK value is set")
},
Expand All @@ -283,7 +284,7 @@ func (t *AlertSetStringCommand[T]) CreateUnsetCmd() {
Use: "unset",
Short: "Unsert / Clear ",
Run: func(cmd *cobra.Command, args []string) {
logus.Debug("CreateUnsetCmd.consoler running with args=", logus.Args(args))
darkbot_logus.Log.Debug("CreateUnsetCmd.consoler running with args=", darkbot_logus.Args(args))
if !CheckCommandAllowedToRun(cmd, t.channels, t.GetChannelID()) {
return
}
Expand All @@ -308,7 +309,7 @@ func (t *AlertSetStringCommand[T]) CreateStatusCmd() {
Use: "status",
Short: "Status",
Run: func(cmd *cobra.Command, args []string) {
logus.Debug("CreateStatusCmd.consoler running with args=", logus.Args(args))
darkbot_logus.Log.Debug("CreateStatusCmd.consoler running with args=", darkbot_logus.Args(args))
if !CheckCommandAllowedToRun(cmd, t.channels, t.GetChannelID()) {
return
}
Expand Down
Loading

0 comments on commit a98a331

Please sign in to comment.