From 00d1a337925e4b937ed230c4e6d12f6e0bed6ee1 Mon Sep 17 00:00:00 2001 From: b00f Date: Thu, 9 Jan 2025 15:19:04 +0800 Subject: [PATCH] fix: set proper AppIDs and Flags (#249) --- Makefile | 1 + .../engine/command/crowdfund/create_test.go | 10 +- .../engine/command/crowdfund/crowdfund.gen.go | 93 ++++++++++--------- .../engine/command/crowdfund/crowdfund.go | 44 +++++---- .../engine/command/crowdfund/info_test.go | 4 +- .../engine/command/crowdfund/purchase_test.go | 4 +- internal/generator/command.tmpl | 46 +++++---- 7 files changed, 114 insertions(+), 88 deletions(-) diff --git a/Makefile b/Makefile index cbf5fe10..69400e1c 100644 --- a/Makefile +++ b/Makefile @@ -52,6 +52,7 @@ build-http: gen: go run ./internal/generator/main.go \ "./internal/engine/command/crowdfund/crowdfund.yml" + find . -name "*.gen.go" -exec gofumpt -l -w {} + ### .PHONY: devtools mock proto fmt check test build build-cli build-discord build-grpc build-telegram build-http gen diff --git a/internal/engine/command/crowdfund/create_test.go b/internal/engine/command/crowdfund/create_test.go index 7b2ca0b9..17937aed 100644 --- a/internal/engine/command/crowdfund/create_test.go +++ b/internal/engine/command/crowdfund/create_test.go @@ -18,7 +18,7 @@ func TestCreate(t *testing.T) { "desc": "crowdfund-desc", "packages": "INVALID-JSON", } - result := td.crowdfundCmd.createHandler(caller, subCmdCreate, args) + result := td.crowdfundCmd.createHandler(caller, td.crowdfundCmd.subCmdCreate, args) assert.False(t, result.Successful) assert.Contains(t, result.Message, "looking for beginning of value") }) @@ -29,7 +29,7 @@ func TestCreate(t *testing.T) { "desc": "", "packages": "[]", } - result := td.crowdfundCmd.createHandler(caller, subCmdCreate, args) + result := td.crowdfundCmd.createHandler(caller, td.crowdfundCmd.subCmdCreate, args) assert.False(t, result.Successful) assert.Contains(t, result.Message, "The title of the crowdfunding campaign cannot be empty") }) @@ -40,7 +40,7 @@ func TestCreate(t *testing.T) { "desc": "crowdfund-desc", "packages": "[]", } - result := td.crowdfundCmd.createHandler(caller, subCmdCreate, args) + result := td.crowdfundCmd.createHandler(caller, td.crowdfundCmd.subCmdCreate, args) assert.False(t, result.Successful) assert.Contains(t, result.Message, "At least 3 packages are required for the crowdfunding campaign") }) @@ -56,7 +56,7 @@ func TestCreate(t *testing.T) { {"name": "package-3", "usd_amount": 300, "pac_amount": 300} ]`, } - result := td.crowdfundCmd.createHandler(caller, subCmdCreate, args) + result := td.crowdfundCmd.createHandler(caller, td.crowdfundCmd.subCmdCreate, args) assert.True(t, result.Successful) assert.Equal(t, result.Message, "Crowdfund campaign 'crowdfund-title' created successfully with 3 packages") }) @@ -72,7 +72,7 @@ func TestCreate(t *testing.T) { {"name": "package-3", "usd_amount": 300, "pac_amount": 300} ]`, } - result := td.crowdfundCmd.createHandler(caller, subCmdCreate, args) + result := td.crowdfundCmd.createHandler(caller, td.crowdfundCmd.subCmdCreate, args) assert.False(t, result.Successful) assert.Contains(t, result.Message, "There is an active campaign: crowdfund-title") }) diff --git a/internal/engine/command/crowdfund/crowdfund.gen.go b/internal/engine/command/crowdfund/crowdfund.gen.go index 89897a6f..255b230b 100644 --- a/internal/engine/command/crowdfund/crowdfund.gen.go +++ b/internal/engine/command/crowdfund/crowdfund.gen.go @@ -3,37 +3,30 @@ package crowdfund import ( "github.com/pagu-project/pagu/internal/engine/command" - "github.com/pagu-project/pagu/internal/entity" ) -var argNameCreateTitle = "title" -var argNameCreateDesc = "desc" -var argNameCreatePackages = "packages" -var argNamePurchasePackage = "package" - -var subCmdCreate *command.Command -var subCmdDisable *command.Command -var subCmdReport *command.Command -var subCmdInfo *command.Command -var subCmdPurchase *command.Command -var subCmdClaim *command.Command +const ( + argNameCreateTitle = "title" + argNameCreateDesc = "desc" + argNameCreatePackages = "packages" + argNamePurchasePackage = "package" +) -func (c *CrowdfundCmd) crowdfundCommand() *command.Command { - crowdfundCmd := &command.Command{ - Name: "crowdfund", - Help: "Commands for managing crowdfunding campaigns", - AppIDs: entity.AllAppIDs(), - TargetFlag: command.TargetMaskAll, - SubCommands: make([]*command.Command, 0), - } +type crowdfundSubCmds struct { + subCmdCreate *command.Command + subCmdDisable *command.Command + subCmdReport *command.Command + subCmdInfo *command.Command + subCmdPurchase *command.Command + subCmdClaim *command.Command +} - subCmdCreate = &command.Command{ +func (c *CrowdfundCmd) buildSubCmds() *crowdfundSubCmds { + subCmdCreate := &command.Command{ Name: "create", Help: "Create a new crowdfunding campaign", Handler: c.createHandler, ResultTemplate: `Crowdfund campaign '{{.campaign.Title}}' created successfully with {{ .campaign.Packages | len }} packages`, - AppIDs: entity.AllAppIDs(), - TargetFlag: command.TargetMaskAll, Args: []*command.Args{ { Name: "title", @@ -55,23 +48,19 @@ func (c *CrowdfundCmd) crowdfundCommand() *command.Command { }, }, } - subCmdDisable = &command.Command{ + subCmdDisable := &command.Command{ Name: "disable", Help: "Disable an existing crowdfunding campaign", Handler: c.disableHandler, ResultTemplate: ``, - AppIDs: entity.AllAppIDs(), - TargetFlag: command.TargetMaskAll, } - subCmdReport = &command.Command{ + subCmdReport := &command.Command{ Name: "report", Help: "View reports of a crowdfunding campaign", Handler: c.reportHandler, ResultTemplate: ``, - AppIDs: entity.AllAppIDs(), - TargetFlag: command.TargetMaskAll, } - subCmdInfo = &command.Command{ + subCmdInfo := &command.Command{ Name: "info", Help: "Get detailed information about a crowdfunding campaign", Handler: c.infoHandler, @@ -80,12 +69,10 @@ func (c *CrowdfundCmd) crowdfundCommand() *command.Command { Packages: {{range .campaign.Packages}} -- {{.Name}} +- {{.Name}}: {{.USDAmount}} USDT to {{.PACAmount}} PAC {{- end}}`, - AppIDs: entity.AllAppIDs(), - TargetFlag: command.TargetMaskAll, } - subCmdPurchase = &command.Command{ + subCmdPurchase := &command.Command{ Name: "purchase", Help: "Make a purchase in a crowdfunding campaign", Handler: c.purchaseHandler, @@ -95,8 +82,6 @@ Please visit {{ .paymentLink }} to make the payment. Once the payment is done, you can claim your PAC coins using "claim" command. Thanks`, - AppIDs: entity.AllAppIDs(), - TargetFlag: command.TargetMaskAll, Args: []*command.Args{ { Name: "package", @@ -106,20 +91,38 @@ Thanks`, }, }, } - subCmdClaim = &command.Command{ + subCmdClaim := &command.Command{ Name: "claim", Help: "Claim packages from a crowdfunding campaign", Handler: c.claimHandler, ResultTemplate: ``, - AppIDs: entity.AllAppIDs(), - TargetFlag: command.TargetMaskAll, } - crowdfundCmd.AddSubCommand(subCmdCreate) - crowdfundCmd.AddSubCommand(subCmdDisable) - crowdfundCmd.AddSubCommand(subCmdReport) - crowdfundCmd.AddSubCommand(subCmdInfo) - crowdfundCmd.AddSubCommand(subCmdPurchase) - crowdfundCmd.AddSubCommand(subCmdClaim) + + return &crowdfundSubCmds{ + subCmdCreate: subCmdCreate, + subCmdDisable: subCmdDisable, + subCmdReport: subCmdReport, + subCmdInfo: subCmdInfo, + subCmdPurchase: subCmdPurchase, + subCmdClaim: subCmdClaim, + } +} + +func (c *CrowdfundCmd) buildCrowdfundCommand() *command.Command { + crowdfundCmd := &command.Command{ + Name: "crowdfund", + Help: "Commands for managing crowdfunding campaigns", + SubCommands: make([]*command.Command, 0), + } + + c.crowdfundSubCmds = c.buildSubCmds() + + crowdfundCmd.AddSubCommand(c.subCmdCreate) + crowdfundCmd.AddSubCommand(c.subCmdDisable) + crowdfundCmd.AddSubCommand(c.subCmdReport) + crowdfundCmd.AddSubCommand(c.subCmdInfo) + crowdfundCmd.AddSubCommand(c.subCmdPurchase) + crowdfundCmd.AddSubCommand(c.subCmdClaim) return crowdfundCmd } diff --git a/internal/engine/command/crowdfund/crowdfund.go b/internal/engine/command/crowdfund/crowdfund.go index cabbeaad..d8e7427e 100644 --- a/internal/engine/command/crowdfund/crowdfund.go +++ b/internal/engine/command/crowdfund/crowdfund.go @@ -11,6 +11,8 @@ import ( ) type CrowdfundCmd struct { + *crowdfundSubCmds + ctx context.Context db *repository.Database wallet wallet.IWallet @@ -35,21 +37,31 @@ func (c *CrowdfundCmd) activeCampaign() *entity.CrowdfundCampaign { } func (c *CrowdfundCmd) GetCommand() *command.Command { - cmd := c.crowdfundCommand() - - subCmdCreate.AppIDs = []entity.PlatformID{entity.PlatformIDCLI, entity.PlatformIDDiscord} - subCmdDisable.AppIDs = []entity.PlatformID{entity.PlatformIDCLI, entity.PlatformIDDiscord} - subCmdReport.AppIDs = entity.AllAppIDs() - subCmdInfo.AppIDs = entity.AllAppIDs() - subCmdPurchase.AppIDs = entity.AllAppIDs() - subCmdClaim.AppIDs = entity.AllAppIDs() - - subCmdCreate.TargetFlag = command.TargetMaskModerator - subCmdDisable.TargetFlag = command.TargetMaskModerator - subCmdReport.TargetFlag = command.TargetMaskMainnet - subCmdInfo.TargetFlag = command.TargetMaskMainnet - subCmdPurchase.TargetFlag = command.TargetMaskMainnet - subCmdClaim.TargetFlag = command.TargetMaskMainnet + middlewareHandler := command.NewMiddlewareHandler(c.db, c.wallet) + cmd := c.buildCrowdfundCommand() + + cmd.AppIDs = entity.AllAppIDs() + cmd.TargetFlag = command.TargetMaskModerator | command.TargetMaskMainnet + + c.subCmdCreate.AppIDs = []entity.PlatformID{entity.PlatformIDCLI, entity.PlatformIDDiscord} + c.subCmdCreate.TargetFlag = command.TargetMaskModerator + c.subCmdCreate.Middlewares = []command.MiddlewareFunc{middlewareHandler.OnlyModerator} + + c.subCmdDisable.AppIDs = []entity.PlatformID{entity.PlatformIDCLI, entity.PlatformIDDiscord} + c.subCmdDisable.TargetFlag = command.TargetMaskModerator + c.subCmdDisable.Middlewares = []command.MiddlewareFunc{middlewareHandler.OnlyModerator} + + c.subCmdReport.AppIDs = entity.AllAppIDs() + c.subCmdReport.TargetFlag = command.TargetMaskModerator | command.TargetMaskMainnet + + c.subCmdInfo.AppIDs = entity.AllAppIDs() + c.subCmdInfo.TargetFlag = command.TargetMaskModerator | command.TargetMaskMainnet + + c.subCmdPurchase.AppIDs = entity.AllAppIDs() + c.subCmdPurchase.TargetFlag = command.TargetMaskModerator | command.TargetMaskMainnet + + c.subCmdClaim.AppIDs = entity.AllAppIDs() + c.subCmdClaim.TargetFlag = command.TargetMaskModerator | command.TargetMaskMainnet activeCampaign := c.activeCampaign() if activeCampaign != nil { @@ -62,7 +74,7 @@ func (c *CrowdfundCmd) GetCommand() *command.Command { purchaseChoices = append(purchaseChoices, choice) } - subCmdPurchase.Args[0].Choices = purchaseChoices + c.subCmdCreate.Args[0].Choices = purchaseChoices } return cmd diff --git a/internal/engine/command/crowdfund/info_test.go b/internal/engine/command/crowdfund/info_test.go index a6a6aa79..04d75cd6 100644 --- a/internal/engine/command/crowdfund/info_test.go +++ b/internal/engine/command/crowdfund/info_test.go @@ -13,7 +13,7 @@ func TestInfo(t *testing.T) { caller := &entity.User{DBModel: entity.DBModel{ID: 1}} t.Run("No active Campaign", func(t *testing.T) { - result := td.crowdfundCmd.infoHandler(caller, subCmdInfo, nil) + result := td.crowdfundCmd.infoHandler(caller, td.crowdfundCmd.subCmdInfo, nil) assert.False(t, result.Successful) assert.Contains(t, result.Message, "No active campaign") }) @@ -21,7 +21,7 @@ func TestInfo(t *testing.T) { t.Run("OK", func(t *testing.T) { testCampaign := td.createTestCampaign(t) - result := td.crowdfundCmd.infoHandler(caller, subCmdInfo, nil) + result := td.crowdfundCmd.infoHandler(caller, td.crowdfundCmd.subCmdInfo, nil) assert.True(t, result.Successful) assert.Contains(t, result.Message, testCampaign.Title) assert.Contains(t, result.Message, testCampaign.Desc) diff --git a/internal/engine/command/crowdfund/purchase_test.go b/internal/engine/command/crowdfund/purchase_test.go index 8d32b99d..c511b685 100644 --- a/internal/engine/command/crowdfund/purchase_test.go +++ b/internal/engine/command/crowdfund/purchase_test.go @@ -17,7 +17,7 @@ func TestPurchase(t *testing.T) { args := map[string]string{ "package": "1", } - result := td.crowdfundCmd.purchaseHandler(caller, subCmdPurchase, args) + result := td.crowdfundCmd.purchaseHandler(caller, td.crowdfundCmd.subCmdPurchase, args) assert.False(t, result.Successful) assert.Contains(t, result.Message, "No active campaign") }) @@ -35,7 +35,7 @@ func TestPurchase(t *testing.T) { args := map[string]string{ "package": "1", } - result := td.crowdfundCmd.purchaseHandler(caller, subCmdPurchase, args) + result := td.crowdfundCmd.purchaseHandler(caller, td.crowdfundCmd.subCmdPurchase, args) assert.True(t, result.Successful) assert.Contains(t, result.Message, "payment-link") diff --git a/internal/generator/command.tmpl b/internal/generator/command.tmpl index cae262ae..8a983255 100644 --- a/internal/generator/command.tmpl +++ b/internal/generator/command.tmpl @@ -3,36 +3,29 @@ package {{.Name}} import ( "github.com/pagu-project/pagu/internal/engine/command" - "github.com/pagu-project/pagu/internal/entity" ) {{ range .SubCommands}} {{- $subCmdName := .Name }} {{- range .Args}} -var argName{{$subCmdName | title}}{{.Name | title}} = "{{.Name}}" +const argName{{$subCmdName | title}}{{.Name | title}} = "{{.Name}}" {{- end}} {{- end}} -{{ range .SubCommands}} -var subCmd{{.Name | title}} *command.Command + +type {{.Name}}SubCmds struct { +{{- range .SubCommands}} + subCmd{{.Name | title}} *command.Command {{- end}} +} -func (c *{{.Name | title}}Cmd) {{.Name}}Command() *command.Command { - {{.Name}}Cmd := &command.Command{ - Name: "{{.Name}}", - Help: "{{.Help}}", - AppIDs: entity.AllAppIDs(), - TargetFlag: command.TargetMaskAll, - SubCommands: make([]*command.Command, 0), - } -{{ range .SubCommands }} - subCmd{{.Name | title}} = &command.Command{ +func (c *{{.Name | title}}Cmd) buildSubCmds() *crowdfundSubCmds { +{{- range .SubCommands }} + subCmd{{.Name | title}} := &command.Command{ Name: "{{.Name}}", Help: "{{.Help}}", Handler: c.{{.Name}}Handler, ResultTemplate: `{{.ResultTemplate | trim}}`, - AppIDs: entity.AllAppIDs(), - TargetFlag: command.TargetMaskAll, {{- if .Args }} - Args: []command.Args{ + Args: []*command.Args{ {{- range .Args}} { Name: "{{.Name}}", @@ -56,9 +49,26 @@ func (c *{{.Name | title}}Cmd) {{.Name}}Command() *command.Command { } {{- end }} + return &crowdfundSubCmds{ {{- $cmdName := .Name }} {{- range .SubCommands }} - {{$cmdName}}Cmd.AddSubCommand(subCmd{{.Name | title}}) + subCmd{{.Name | title}}: subCmd{{.Name | title}}, +{{- end}} + } +} + +func (c *{{.Name | title}}Cmd) build{{.Name | title}}Command() *command.Command { + {{.Name}}Cmd := &command.Command{ + Name: "{{.Name}}", + Help: "{{.Help}}", + SubCommands: make([]*command.Command, 0), + } + + c.{{.Name}}SubCmds = c.buildSubCmds() + +{{- $cmdName := .Name }} +{{ range .SubCommands }} + {{$cmdName}}Cmd.AddSubCommand(c.subCmd{{.Name | title}}) {{- end}} return {{.Name}}Cmd