From e933222ac9d79576bbd73992e431cf53ab151ed2 Mon Sep 17 00:00:00 2001 From: mj Date: Fri, 28 Jun 2024 16:55:54 +0330 Subject: [PATCH] feat(targetflag): target flags added to support different application and platforms --- Makefile | 19 ++++++---- cmd/cli/main.go | 2 +- cmd/{ => mainnet}/discord/main.go | 0 cmd/mainnet/discord/run.go | 54 +++++++++++++++++++++++++++ cmd/{ => mainnet}/grpc/main.go | 0 cmd/{ => mainnet}/grpc/run.go | 2 +- cmd/{ => mainnet}/http/main.go | 0 cmd/{ => mainnet}/http/run.go | 2 +- cmd/{ => mainnet}/telegram/main.go | 0 cmd/{ => mainnet}/telegram/run.go | 2 +- cmd/moderation/discord/main.go | 21 +++++++++++ cmd/{ => moderation}/discord/run.go | 10 ++--- cmd/testnet/discord/main.go | 21 +++++++++++ cmd/testnet/discord/run.go | 54 +++++++++++++++++++++++++++ config/config.go | 28 +++++++------- config/config_test.go | 12 +++++- config/global.go | 10 ++++- deployment/discord/Dockerfile | 21 ----------- deployment/docker-compose.yml | 20 ++++++++-- deployment/mainnet/discord/Dockerfile | 21 +++++++++++ deployment/testnet/discord/Dockerfile | 21 +++++++++++ internal/engine/engine.go | 28 +++++++++++++- internal/platforms/discord/discord.go | 7 +++- 23 files changed, 291 insertions(+), 64 deletions(-) rename cmd/{ => mainnet}/discord/main.go (100%) create mode 100644 cmd/mainnet/discord/run.go rename cmd/{ => mainnet}/grpc/main.go (100%) rename cmd/{ => mainnet}/grpc/run.go (97%) rename cmd/{ => mainnet}/http/main.go (100%) rename cmd/{ => mainnet}/http/run.go (97%) rename cmd/{ => mainnet}/telegram/main.go (100%) rename cmd/{ => mainnet}/telegram/run.go (97%) create mode 100644 cmd/moderation/discord/main.go rename cmd/{ => moderation}/discord/run.go (82%) create mode 100644 cmd/testnet/discord/main.go create mode 100644 cmd/testnet/discord/run.go delete mode 100644 deployment/discord/Dockerfile create mode 100644 deployment/mainnet/discord/Dockerfile create mode 100644 deployment/testnet/discord/Dockerfile diff --git a/Makefile b/Makefile index 2cb6976c..f45aa420 100644 --- a/Makefile +++ b/Makefile @@ -38,22 +38,25 @@ check: golangci-lint run --timeout=20m0s ### building -build: build-cli build-discord build-grpc build-telegram build-http +build: build-cli build-discord-mainnet build-discord-testnet build-grpc build-telegram build-http build-cli: go build -o build/pagu-cli ./cmd/cli -build-discord: - go build -o build/pagu-discord ./cmd/discord - build-grpc: - go build -o build/pagu-grpc ./cmd/grpc - + go build -o build/pagu-grpc ./cmd/mainnet/grpc + +build-discord-mainnet: + go build -o build/pagu-discord-mainnet ./cmd/mainnet/discord + +build-discord-testnet: + go build -o build/pagu-discord-testnet ./cmd/testnet/discord + build-telegram: - go build -o build/pagu-telegram ./cmd/telegram + go build -o build/pagu-telegram ./cmd/mainnet/telegram build-http: - go build -o build/pagu-http ./cmd/http + go build -o build/pagu-http ./cmd/mainnet/http ### pre commit pre-commit: mock proto fmt check unit_test diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 70af969e..bc2500e3 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -34,7 +34,7 @@ func run(cmd *cobra.Command, args []string) { botEngine, err := engine.NewBotEngine(configs) pCmd.ExitOnError(cmd, err) - botEngine.RegisterAllCommands() + botEngine.RegisterCommands() botEngine.Start() reader := bufio.NewReader(os.Stdin) diff --git a/cmd/discord/main.go b/cmd/mainnet/discord/main.go similarity index 100% rename from cmd/discord/main.go rename to cmd/mainnet/discord/main.go diff --git a/cmd/mainnet/discord/run.go b/cmd/mainnet/discord/run.go new file mode 100644 index 00000000..da15e310 --- /dev/null +++ b/cmd/mainnet/discord/run.go @@ -0,0 +1,54 @@ +package main + +import ( + "os" + "os/signal" + "syscall" + + "github.com/pagu-project/Pagu/internal/engine" + + "github.com/pagu-project/Pagu/internal/platforms/discord" + "github.com/pagu-project/Pagu/pkg/log" + + pCmd "github.com/pagu-project/Pagu/cmd" + "github.com/pagu-project/Pagu/config" + "github.com/spf13/cobra" +) + +func runCommand(parentCmd *cobra.Command) { + run := &cobra.Command{ + Use: "run", + Short: "Runs a mainnet instance of Pagu for Discord", + } + + parentCmd.AddCommand(run) + + run.Run = func(cmd *cobra.Command, _ []string) { + // load configuration. + configs, err := config.Load(configPath) + pCmd.ExitOnError(cmd, err) + + // Initialize global logger. + log.InitGlobalLogger(configs.Logger) + + // starting botEngine. + botEngine, err := engine.NewBotEngine(configs) + pCmd.ExitOnError(cmd, err) + + discordMainBot, err := discord.NewDiscordBot(botEngine, configs.DiscordMainBot, config.TargetMaskMain) + pCmd.ExitOnError(cmd, err) + + err = discordMainBot.Start() + pCmd.ExitOnError(cmd, err) + + sigChan := make(chan os.Signal, 1) + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) + <-sigChan + + if err := discordMainBot.Stop(); err != nil { + pCmd.ExitOnError(cmd, err) + } + + botEngine.Stop() + } +} diff --git a/cmd/grpc/main.go b/cmd/mainnet/grpc/main.go similarity index 100% rename from cmd/grpc/main.go rename to cmd/mainnet/grpc/main.go diff --git a/cmd/grpc/run.go b/cmd/mainnet/grpc/run.go similarity index 97% rename from cmd/grpc/run.go rename to cmd/mainnet/grpc/run.go index dbc2c688..7b517dcf 100644 --- a/cmd/grpc/run.go +++ b/cmd/mainnet/grpc/run.go @@ -34,7 +34,7 @@ func runCommand(parentCmd *cobra.Command) { botEngine, err := engine.NewBotEngine(config) pCmd.ExitOnError(cmd, err) - botEngine.RegisterAllCommands() + botEngine.RegisterCommands() botEngine.Start() grpcServer := grpc.NewServer(botEngine, config.GRPC) diff --git a/cmd/http/main.go b/cmd/mainnet/http/main.go similarity index 100% rename from cmd/http/main.go rename to cmd/mainnet/http/main.go diff --git a/cmd/http/run.go b/cmd/mainnet/http/run.go similarity index 97% rename from cmd/http/run.go rename to cmd/mainnet/http/run.go index 77bc08ff..581b40a9 100644 --- a/cmd/http/run.go +++ b/cmd/mainnet/http/run.go @@ -34,7 +34,7 @@ func runCommand(parentCmd *cobra.Command) { botEngine, err := engine.NewBotEngine(configs) pCmd.ExitOnError(cmd, err) - botEngine.RegisterAllCommands() + botEngine.RegisterCommands() botEngine.Start() httpServer := http.NewHTTPServer(botEngine, configs.HTTP) diff --git a/cmd/telegram/main.go b/cmd/mainnet/telegram/main.go similarity index 100% rename from cmd/telegram/main.go rename to cmd/mainnet/telegram/main.go diff --git a/cmd/telegram/run.go b/cmd/mainnet/telegram/run.go similarity index 97% rename from cmd/telegram/run.go rename to cmd/mainnet/telegram/run.go index 132929c8..9e7b0384 100644 --- a/cmd/telegram/run.go +++ b/cmd/mainnet/telegram/run.go @@ -33,7 +33,7 @@ func runCommand(parentCmd *cobra.Command) { log.InitGlobalLogger(configs.Logger) - botEngine.RegisterAllCommands() + botEngine.RegisterCommands() botEngine.Start() chatID := configs.Telegram.ChatID diff --git a/cmd/moderation/discord/main.go b/cmd/moderation/discord/main.go new file mode 100644 index 00000000..ecf265bc --- /dev/null +++ b/cmd/moderation/discord/main.go @@ -0,0 +1,21 @@ +package main + +import ( + pagu "github.com/pagu-project/Pagu" + "github.com/pagu-project/Pagu/cmd" + "github.com/spf13/cobra" +) + +var configPath string + +func main() { + rootCmd := &cobra.Command{ + Use: "pagu-discord", + Version: pagu.StringVersion(), + } + + rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "./config.yml", "config path ./config.yml") + runCommand(rootCmd) + err := rootCmd.Execute() + cmd.ExitOnError(rootCmd, err) +} diff --git a/cmd/discord/run.go b/cmd/moderation/discord/run.go similarity index 82% rename from cmd/discord/run.go rename to cmd/moderation/discord/run.go index b1276d29..9bf593f7 100644 --- a/cmd/discord/run.go +++ b/cmd/moderation/discord/run.go @@ -6,6 +6,7 @@ import ( "syscall" "github.com/pagu-project/Pagu/internal/engine" + "github.com/pagu-project/Pagu/internal/platforms/discord" "github.com/pagu-project/Pagu/pkg/log" @@ -34,20 +35,17 @@ func runCommand(parentCmd *cobra.Command) { botEngine, err := engine.NewBotEngine(configs) pCmd.ExitOnError(cmd, err) - botEngine.RegisterAllCommands() - botEngine.Start() - - discordBot, err := discord.NewDiscordBot(botEngine, configs.DiscordBot.Token, configs.DiscordBot) + discordModerationBot, err := discord.NewDiscordBot(botEngine, configs.DiscordModerationBot, config.TargetMaskModerator) pCmd.ExitOnError(cmd, err) - err = discordBot.Start() + err = discordModerationBot.Start() pCmd.ExitOnError(cmd, err) sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) <-sigChan - if err := discordBot.Stop(); err != nil { + if err := discordModerationBot.Stop(); err != nil { pCmd.ExitOnError(cmd, err) } diff --git a/cmd/testnet/discord/main.go b/cmd/testnet/discord/main.go new file mode 100644 index 00000000..ecf265bc --- /dev/null +++ b/cmd/testnet/discord/main.go @@ -0,0 +1,21 @@ +package main + +import ( + pagu "github.com/pagu-project/Pagu" + "github.com/pagu-project/Pagu/cmd" + "github.com/spf13/cobra" +) + +var configPath string + +func main() { + rootCmd := &cobra.Command{ + Use: "pagu-discord", + Version: pagu.StringVersion(), + } + + rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "./config.yml", "config path ./config.yml") + runCommand(rootCmd) + err := rootCmd.Execute() + cmd.ExitOnError(rootCmd, err) +} diff --git a/cmd/testnet/discord/run.go b/cmd/testnet/discord/run.go new file mode 100644 index 00000000..9a0fd451 --- /dev/null +++ b/cmd/testnet/discord/run.go @@ -0,0 +1,54 @@ +package main + +import ( + "os" + "os/signal" + "syscall" + + "github.com/pagu-project/Pagu/internal/engine" + + "github.com/pagu-project/Pagu/internal/platforms/discord" + "github.com/pagu-project/Pagu/pkg/log" + + pCmd "github.com/pagu-project/Pagu/cmd" + "github.com/pagu-project/Pagu/config" + "github.com/spf13/cobra" +) + +func runCommand(parentCmd *cobra.Command) { + run := &cobra.Command{ + Use: "run", + Short: "Runs a testnet instance of Pagu for Discord", + } + + parentCmd.AddCommand(run) + + run.Run = func(cmd *cobra.Command, _ []string) { + // load configuration. + configs, err := config.Load(configPath) + pCmd.ExitOnError(cmd, err) + + // Initialize global logger. + log.InitGlobalLogger(configs.Logger) + + // starting botEngine. + botEngine, err := engine.NewBotEngine(configs) + pCmd.ExitOnError(cmd, err) + + discordTestBot, err := discord.NewDiscordBot(botEngine, configs.DiscordTestBot, config.TargetMaskTest) + pCmd.ExitOnError(cmd, err) + + err = discordTestBot.Start() + pCmd.ExitOnError(cmd, err) + + sigChan := make(chan os.Signal, 1) + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) + <-sigChan + + if err := discordTestBot.Stop(); err != nil { + pCmd.ExitOnError(cmd, err) + } + + botEngine.Stop() + } +} diff --git a/config/config.go b/config/config.go index ab3e9d14..991132fe 100644 --- a/config/config.go +++ b/config/config.go @@ -9,19 +9,21 @@ import ( ) type Config struct { - Network string `yaml:"network"` - NetworkNodes []string `yaml:"network_nodes"` - LocalNode string `yaml:"local_node"` - Database Database `yaml:"database"` - AuthIDs []string `yaml:"auth_ids"` - DiscordBot DiscordBot `yaml:"discord"` - GRPC GRPC `yaml:"grpc"` - Wallet Wallet `yaml:"main_net_wallet"` - TestNetWallet Wallet `yaml:"test_net_wallet"` - Logger Logger `yaml:"logger"` - HTTP HTTP `yaml:"http"` - Phoenix PhoenixNetwork `yaml:"phoenix"` - Telegram Telegram `yaml:"telegram"` + Network string `yaml:"network"` + NetworkNodes []string `yaml:"network_nodes"` + LocalNode string `yaml:"local_node"` + Database Database `yaml:"database"` + AuthIDs []string `yaml:"auth_ids"` + DiscordMainBot DiscordBot `yaml:"discord_main"` + DiscordTestBot DiscordBot `yaml:"discord_test"` + DiscordModerationBot DiscordBot `yaml:"discord_mod"` + GRPC GRPC `yaml:"grpc"` + Wallet Wallet `yaml:"main_net_wallet"` + TestNetWallet Wallet `yaml:"test_net_wallet"` + Logger Logger `yaml:"logger"` + HTTP HTTP `yaml:"http"` + Phoenix PhoenixNetwork `yaml:"phoenix"` + Telegram Telegram `yaml:"telegram"` } type Database struct { diff --git a/config/config_test.go b/config/config_test.go index 162650bb..c61a00e7 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -26,7 +26,11 @@ func TestBasicCheck(t *testing.T) { Password: "test_password", }, NetworkNodes: []string{"http://127.0.0.1:8545"}, - DiscordBot: DiscordBot{ + DiscordMainBot: DiscordBot{ + Token: "MTEabc123", + GuildID: "123456789", + }, + DiscordTestBot: DiscordBot{ Token: "MTEabc123", GuildID: "123456789", }, @@ -45,7 +49,11 @@ func TestBasicCheck(t *testing.T) { Password: "test_password", }, NetworkNodes: []string{}, - DiscordBot: DiscordBot{ + DiscordMainBot: DiscordBot{ + Token: "MTEabc123", + GuildID: "123456789", + }, + DiscordTestBot: DiscordBot{ Token: "MTEabc123", GuildID: "123456789", }, diff --git a/config/global.go b/config/global.go index 4268ec3e..24158d33 100644 --- a/config/global.go +++ b/config/global.go @@ -1,7 +1,13 @@ package config +var ( + TargetMaskMain = 1 + TargetMaskTest = 2 + TargetMaskModerator = 4 + + TargetMaskAll = TargetMaskMain | TargetMaskTest | TargetMaskModerator +) + const ( PriceCacheKey = "PriceCacheKey" ) - -var HelpCommandTemplate string = `{{range .}}{{end}}
{{ .Name }}{{ .Desc }}
` diff --git a/deployment/discord/Dockerfile b/deployment/discord/Dockerfile deleted file mode 100644 index 5963d1c2..00000000 --- a/deployment/discord/Dockerfile +++ /dev/null @@ -1,21 +0,0 @@ -FROM golang:1.22.2-alpine as builder - -RUN apk add --no-cache bash make git - -RUN mkdir /pagu -WORKDIR /pagu -COPY ../.. . - -RUN go mod tidy -RUN go mod vendor -RUN make build-discord - -FROM alpine:3.14 -RUN apk --no-cache add ca-certificates tzdata git - -RUN mkdir /pagu-discord -COPY --from=builder /pagu/build /pagu-discord - -RUN chmod +x /pagu-discord/pagu-discord - -CMD ["./pagu-discord/pagu-discord", "-c", "./config/config.yml", "run"] \ No newline at end of file diff --git a/deployment/docker-compose.yml b/deployment/docker-compose.yml index b2ea28ec..f8fdd2d6 100644 --- a/deployment/docker-compose.yml +++ b/deployment/docker-compose.yml @@ -59,12 +59,12 @@ services: - ../config/config.yml:/config/config.yml - ../config/wallets/main_wallet:/config/wallets/main_wallet - ../config/wallets/test_wallet:/config/wallets/test_wallet - pagu-discord: + pagu-discord-mainnet: build: context: ../. - dockerfile: deployment/discord/Dockerfile - hostname: pagu-discord - container_name: pagu-discord + dockerfile: deployment/mainnet/discord/Dockerfile + hostname: pagu-discord-mainnet + container_name: pagu-discord-mainnet networks: pagu-network: depends_on: @@ -72,6 +72,18 @@ services: volumes: - ../config/config.yml:/config/config.yml - ../config/wallets/main_wallet:/config/wallets/main_wallet + pagu-discord-testnet: + build: + context: ../. + dockerfile: deployment/testnet/discord/Dockerfile + hostname: pagu-discord-testnet + container_name: pagu-discord-testnet + networks: + pagu-network: + depends_on: + - pagu-db + volumes: + - ../config/config.yml:/config/config.yml - ../config/wallets/test_wallet:/config/wallets/test_wallet pagu-node: hostname: pagu-node diff --git a/deployment/mainnet/discord/Dockerfile b/deployment/mainnet/discord/Dockerfile new file mode 100644 index 00000000..d1d8ad56 --- /dev/null +++ b/deployment/mainnet/discord/Dockerfile @@ -0,0 +1,21 @@ +FROM golang:1.22.2-alpine as builder + +RUN apk add --no-cache bash make git + +RUN mkdir /pagu +WORKDIR /pagu +COPY ../../.. . + +RUN go mod tidy +RUN go mod vendor +RUN make build-discord-mainnet + +FROM alpine:3.14 +RUN apk --no-cache add ca-certificates tzdata git + +RUN mkdir /pagu-discord-mainnet +COPY --from=builder /pagu/build /pagu-discord-mainnet + +RUN chmod +x /pagu-discord/pagu-discord-mainnet + +CMD ["./pagu-discord/pagu-discord-mainnet", "-c", "./config/config.yml", "run"] \ No newline at end of file diff --git a/deployment/testnet/discord/Dockerfile b/deployment/testnet/discord/Dockerfile new file mode 100644 index 00000000..21f605ec --- /dev/null +++ b/deployment/testnet/discord/Dockerfile @@ -0,0 +1,21 @@ +FROM golang:1.22.2-alpine as builder + +RUN apk add --no-cache bash make git + +RUN mkdir /pagu +WORKDIR /pagu +COPY ../../.. . + +RUN go mod tidy +RUN go mod vendor +RUN make build-discord-testnet + +FROM alpine:3.14 +RUN apk --no-cache add ca-certificates tzdata git + +RUN mkdir /pagu-discord-testnet +COPY --from=builder /pagu/build /pagu-discord-testnet + +RUN chmod +x /pagu-discord/pagu-discord-testnet + +CMD ["./pagu-discord/pagu-discord-testnet", "-c", "./config/config.yml", "run"] \ No newline at end of file diff --git a/internal/engine/engine.go b/internal/engine/engine.go index f70a4e08..bda85ba1 100644 --- a/internal/engine/engine.go +++ b/internal/engine/engine.go @@ -166,17 +166,41 @@ func (be *BotEngine) Commands() []command.Command { return be.rootCmd.SubCommands } -func (be *BotEngine) RegisterAllCommands() { +func (be *BotEngine) RegisterCommands(targets ...int) { + for _, target := range targets { + switch target { + case config.TargetMaskMain: + be.registerMainnetCommands() + case config.TargetMaskTest: + be.registerTestnetCommands() + case config.TargetMaskModerator: + be.registerModerationCommands() + case config.TargetMaskAll: + be.registerMainnetCommands() + be.registerTestnetCommands() + be.registerModerationCommands() + } + } +} + +func (be *BotEngine) registerMainnetCommands() { be.rootCmd.AddSubCommand(be.blockchainCmd.GetCommand()) be.rootCmd.AddSubCommand(be.networkCmd.GetCommand()) be.rootCmd.AddSubCommand(be.zealyCmd.GetCommand()) be.rootCmd.AddSubCommand(be.voucherCmd.GetCommand()) be.rootCmd.AddSubCommand(be.marketCmd.GetCommand()) - be.rootCmd.AddSubCommand(be.phoenixCmd.GetCommand()) + be.rootCmd.AddHelpSubCommand() +} +func (be *BotEngine) registerTestnetCommands() { + be.rootCmd.AddSubCommand(be.phoenixCmd.GetCommand()) be.rootCmd.AddHelpSubCommand() } +func (be *BotEngine) registerModerationCommands() { + // TODO: register moderation commands +} + func (be *BotEngine) Run(appID entity.AppID, callerID string, tokens []string) command.CommandResult { log.Debug("run command", "callerID", callerID, "inputs", tokens) diff --git a/internal/platforms/discord/discord.go b/internal/platforms/discord/discord.go index c9848a6e..6b44c7eb 100644 --- a/internal/platforms/discord/discord.go +++ b/internal/platforms/discord/discord.go @@ -21,12 +21,15 @@ type DiscordBot struct { cfg config.DiscordBot } -func NewDiscordBot(botEngine *engine.BotEngine, token string, cfg config.DiscordBot) (*DiscordBot, error) { - s, err := discordgo.New("Bot " + token) +func NewDiscordBot(botEngine *engine.BotEngine, cfg config.DiscordBot, targets int) (*DiscordBot, error) { + s, err := discordgo.New("Bot " + cfg.Token) if err != nil { return nil, err } + botEngine.RegisterCommands(targets) + botEngine.Start() + return &DiscordBot{ Session: s, engine: botEngine,