Skip to content

Commit

Permalink
fix: set proper AppIDs and Flags (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f authored Jan 9, 2025
1 parent 744f989 commit 00d1a33
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 88 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 5 additions & 5 deletions internal/engine/command/crowdfund/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
Expand All @@ -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")
})
Expand All @@ -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")
})
Expand All @@ -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")
})
Expand All @@ -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")
})
Expand Down
93 changes: 48 additions & 45 deletions internal/engine/command/crowdfund/crowdfund.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 28 additions & 16 deletions internal/engine/command/crowdfund/crowdfund.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
)

type CrowdfundCmd struct {
*crowdfundSubCmds

ctx context.Context
db *repository.Database
wallet wallet.IWallet
Expand All @@ -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 {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/engine/command/crowdfund/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ 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")
})

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)
Expand Down
4 changes: 2 additions & 2 deletions internal/engine/command/crowdfund/purchase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
Expand All @@ -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")
Expand Down
46 changes: 28 additions & 18 deletions internal/generator/command.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}",
Expand All @@ -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
Expand Down

0 comments on commit 00d1a33

Please sign in to comment.