Skip to content

Commit

Permalink
feat: info command added
Browse files Browse the repository at this point in the history
  • Loading branch information
dd84ai committed Feb 3, 2025
1 parent eca8759 commit fcd71df
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 29 deletions.
61 changes: 61 additions & 0 deletions app/consoler/commands/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package commands

import (
"fmt"
"strings"

"github.com/darklab8/fl-darkbot/app/configurator"
"github.com/darklab8/fl-darkbot/app/consoler/commands/cmdgroup"
"github.com/darklab8/fl-darkbot/app/consoler/printer"
"github.com/darklab8/fl-darkbot/app/settings/logus"
"github.com/darklab8/fl-darkstat/darkrpc"
"github.com/spf13/cobra"
)

type InfoCommands struct {
*cmdgroup.CmdGroup
channels configurator.ConfiguratorChannel
}

func NewInfoCommands(
cmdGroup *cmdgroup.CmdGroup,
channels configurator.ConfiguratorChannel,
) *InfoCommands {
t := &InfoCommands{CmdGroup: cmdGroup, channels: channels}
t.CreateGetInfoCmd()
return t
}

func (t *InfoCommands) CreateGetInfoCmd() {
command := &cobra.Command{
Use: "info",
Short: "Get info about some Freelancer entity",
// Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
logus.Log.Debug("Get info about some Freelancer entity", logus.Args(args))

// printer.Println(cmd, "Attempting to parse input into integer number")
query_args := args
query := strings.Join(query_args, " ")

answer := "OK giving info about(" + fmt.Sprintf("%d", len(query)) + "): " + query
printer.Println(cmd, answer)

client := darkrpc.NewClient(darkrpc.WithSockCli(darkrpc.DarkstatRpcSock))
reply := new(darkrpc.GetInfoReply)
err := client.GetInfo(darkrpc.GetInfoArgs{Query: query}, reply)

if err != nil {
printer.Println(cmd, err.Error())
} else {
printer.Println(cmd, "Got info!")
for _, line := range reply.Content {
printer.Println(cmd, line)
}

}

},
}
t.CurrentCmd.AddCommand(command)
}
5 changes: 5 additions & 0 deletions app/consoler/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ func newRootCommands(
r.CreateConnect()
r.CreateDisconnect()
r.CreateConfig()

NewInfoCommands(
r.CmdGroup,
r.channels,
)
return r
}

Expand Down
39 changes: 37 additions & 2 deletions app/listener/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func allowedMessage(s *discordgo.Session, m *discordgo.MessageCreate) bool {
return false
}

if strings.HasPrefix(m.Content, fmt.Sprintf("%s info", settings.Env.ConsolerPrefix)) {
return true
}

// Ignore all messages created by the bot itself
// This isn't required in this specific example but it's a good practice.
if messageAuthorID == botID {
Expand Down Expand Up @@ -100,16 +104,47 @@ func init() {
console = consoler.NewConsoler(settings.Dbpath)
}

func consolerHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
func Chunks(s string, chunkSize int) []string {
if len(s) == 0 {
return nil
}
if chunkSize >= len(s) {
return []string{s}
}
var chunks []string = make([]string, 0, (len(s)-1)/chunkSize+1)
currentLen := 0
currentStart := 0
for i := range s {
if currentLen == chunkSize {
chunks = append(chunks, s[currentStart:i])
currentLen = 0
currentStart = i
}
currentLen++
}
chunks = append(chunks, s[currentStart:])
return chunks
}

func consolerHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
if !allowedMessage(s, m) {
return
}
channelID := types.DiscordChannelID(m.ChannelID)
rendered := console.Execute(m.Content, channelID)

if rendered != "" {
s.ChannelMessageSend(m.ChannelID, rendered)
var err error
chunked_strings := Chunks(rendered, 1950)
for _, chunk_str := range chunked_strings {
_, err := s.ChannelMessageSend(m.ChannelID, chunk_str)
if err != nil {
break
}
}
if logus.Log.CheckWarn(err, "failed to send message of consoler") {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintln("failed to send message of consoler with reason: ", err.Error()))
}
}
logus.Log.Debug("consolerHandler finished", logus.ChannelID(channelID))
}
1 change: 0 additions & 1 deletion app/viewer/viewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func NewViewer(dbpath types.Dbpath, scrappy_storage *scrappy.ScrappyStorage) *Vi
}

v.workers = worker.NewTaskPoolPersistent(
worker.WithAllowFailedTasks(),
worker.WithDisableParallelism(false),
worker.WithWorkersAmount(10),
)
Expand Down
19 changes: 11 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
module github.com/darklab8/fl-darkbot

go 1.22.5
go 1.23.0

require (
github.com/anaskhan96/soup v1.2.5
github.com/bwmarrin/discordgo v0.28.1
github.com/darklab8/go-typelog v0.6.1
github.com/darklab8/go-utils v0.21.0
github.com/darklab8/fl-darkstat v1.64.2
github.com/darklab8/go-typelog v0.6.2
github.com/darklab8/go-utils v0.21.1
github.com/pkg/profile v1.7.0
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
golang.org/x/net v0.27.0
github.com/stretchr/testify v1.10.0
golang.org/x/net v0.34.0
gorm.io/driver/sqlite v1.5.6
gorm.io/gorm v1.25.11
)

require (
github.com/a-h/templ v0.2.747 // indirect
github.com/darklab8/fl-data-discovery v0.4.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/fgprof v0.9.4 // indirect
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 // indirect
Expand All @@ -27,9 +30,9 @@ require (
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/text v0.21.0 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
36 changes: 22 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/a-h/templ v0.2.747 h1:D0dQ2lxC3W7Dxl6fxQ/1zZHBQslSkTSvl5FxP/CfdKg=
github.com/a-h/templ v0.2.747/go.mod h1:69ObQIbrcuwPCU32ohNaWce3Cb7qM5GMiqN1K+2yop4=
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.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4=
Expand All @@ -13,10 +15,14 @@ github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMn
github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38GC8=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/darklab8/go-typelog v0.6.1 h1:G9b+roOt5bP9M+Nn1Ao3fxjhNoUU/+NujcfNay8SgcE=
github.com/darklab8/go-typelog v0.6.1/go.mod h1:AwwOf3dkp/tpevHFNbkB+PbwlDrUUgO1CVFkEnj+q5w=
github.com/darklab8/go-utils v0.21.0 h1:SsHSYj7DFmyUV2itLSG2LB+mRanwmywzpSyKxrStCJ4=
github.com/darklab8/go-utils v0.21.0/go.mod h1:3PLk/FOPr+zgkw0ZOZbsT4C+nP/TE8ImfSosca7maLA=
github.com/darklab8/fl-darkstat v1.64.2 h1:Wh6hsi0glY8bzaf1xnUe5IbfpbGZx5B2mfdtNo7ec1w=
github.com/darklab8/fl-darkstat v1.64.2/go.mod h1:5QdTzZ16wOAmppHn/Y+4pR+U0pCnKoA9JohuPLRowy8=
github.com/darklab8/fl-data-discovery v0.4.0 h1:cUacP+N04/PrYl4cmw687BAaQWVJI32scePjLfmqxEI=
github.com/darklab8/fl-data-discovery v0.4.0/go.mod h1:SgRh0N3cWrhslXVkc5LJXK52Mw4+Fdsbmv+0qkNJ6QI=
github.com/darklab8/go-typelog v0.6.2 h1:R6urrs4zgtjWfAngB7UclxpkVMA1uW3hEw7PEgSU7pU=
github.com/darklab8/go-typelog v0.6.2/go.mod h1:RPymaCKD04g9lpGNYTQ2RarSYD8XaAgTZKN5J9fGRGA=
github.com/darklab8/go-utils v0.21.1 h1:IpO1xRyauW6XnEOzLYww2cjd/Gx41Jtgi3PTxw21lqo=
github.com/darklab8/go-utils v0.21.1/go.mod h1:3PLk/FOPr+zgkw0ZOZbsT4C+nP/TE8ImfSosca7maLA=
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 @@ -26,6 +32,8 @@ github.com/felixge/fgprof v0.9.4/go.mod h1:yKl+ERSa++RYOs32d8K6WEXCB4uXdLls4ZaZP
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
github.com/gobwas/ws v1.2.1/go.mod h1:hRKAFb8wOxFROYNsT1bqfWnhX+b5MFeJM9r2ZSwg/KY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd/go.mod h1:KgnwoLYCZ8IQu3XUZ8Nc/bM9CCZFOyjUNOSygVozoDg=
github.com/google/pprof v0.0.0-20240227163752-401108e1b7e7/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik=
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 h1:FKHo8hFI3A+7w0aUQuYXQ+6EN5stWmeY/AZqtM8xk9k=
Expand Down Expand Up @@ -70,28 +78,28 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
Expand Down
9 changes: 9 additions & 0 deletions tf/modules/darkbot/docker.tf
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ resource "docker_service" "darkbot" {
propagation = "rprivate"
}
}
mounts { // darkstat socks
target = "/tmp/darkstat"
source = "/tmp/darkstat-${var.environment}"
type = "bind"
read_only = false
bind_options {
propagation = "rprivate"
}
}
}
restart_policy {
condition = "any"
Expand Down
8 changes: 4 additions & 4 deletions tf/production/providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ provider "cloudflare" {
api_token = data.external.secrets_cloudflare.result["token"]
}

provider "kubernetes" {
config_path = "~/.kube/config"
config_context = "darkbot-context"
}
# provider "kubernetes" {
# config_path = "~/.kube/config"
# config_context = "darkbot-context"
# }

0 comments on commit fcd71df

Please sign in to comment.