From d382b460921ad87e607a67d3fa5f933f603d2d32 Mon Sep 17 00:00:00 2001 From: Mostafa Date: Wed, 8 Jan 2025 18:31:55 +0800 Subject: [PATCH] faet: add result template to command struct --- .gitignore | 5 +- internal/engine/command/command.go | 23 ++++--- internal/engine/command/crowdfund/create.go | 12 ++-- .../engine/command/crowdfund/crowdfund.gen.go | 67 ++++++++++--------- .../engine/command/crowdfund/crowdfund.go | 22 +++--- .../engine/command/crowdfund/crowdfund.yml | 3 +- internal/engine/command/crowdfund/info.go | 2 +- internal/generator/command.tmpl | 12 ++-- internal/generator/main.go | 4 +- 9 files changed, 78 insertions(+), 72 deletions(-) diff --git a/.gitignore b/.gitignore index 8299c8b..72aff7a 100644 --- a/.gitignore +++ b/.gitignore @@ -7,10 +7,7 @@ todo .env *.log /local -config/config.yml -config/config.*.yml -config/wallets -config/templates +config/config.local.yml __debug_* *.sqlite* deployment/.env \ No newline at end of file diff --git a/internal/engine/command/command.go b/internal/engine/command/command.go index 10dc4da..2c43a50 100644 --- a/internal/engine/command/command.go +++ b/internal/engine/command/command.go @@ -93,15 +93,16 @@ type Args struct { type HandlerFunc func(caller *entity.User, cmd *Command, args map[string]string) CommandResult type Command struct { - Emoji string `yaml:"emoji"` - Name string `yaml:"name"` - Help string `yaml:"help"` - Args []Args `yaml:"args"` - AppIDs []entity.PlatformID `yaml:"-"` - SubCommands []*Command `yaml:"sub_commands"` - Middlewares []MiddlewareFunc `yaml:"-"` - Handler HandlerFunc `yaml:"-"` - TargetFlag int `yaml:"-"` + Emoji string `yaml:"emoji"` + Name string `yaml:"name"` + Help string `yaml:"help"` + Args []Args `yaml:"args"` + SubCommands []*Command `yaml:"sub_commands"` + ResultTemplate string `yaml:"result_template"` + Middlewares []MiddlewareFunc `yaml:"-"` + Handler HandlerFunc `yaml:"-"` + AppIDs []entity.PlatformID `yaml:"-"` + TargetFlag int `yaml:"-"` } type CommandResult struct { @@ -130,7 +131,7 @@ func (cmd *Command) RenderErrorTemplate(err error) CommandResult { } } -func (cmd *Command) RenderResultTemplate(templateContent string, keyvals ...any) CommandResult { +func (cmd *Command) RenderResultTemplate(keyvals ...any) CommandResult { if len(keyvals)%2 != 0 { keyvals = append(keyvals, "!MISSING-VALUE!") } @@ -143,7 +144,7 @@ func (cmd *Command) RenderResultTemplate(templateContent string, keyvals ...any) data[key] = val } - msg, err := cmd.executeTemplate(templateContent, data) + msg, err := cmd.executeTemplate(cmd.ResultTemplate, data) if err != nil { return cmd.RenderErrorTemplate(err) } diff --git a/internal/engine/command/crowdfund/create.go b/internal/engine/command/crowdfund/create.go index f614bcc..b72f84b 100644 --- a/internal/engine/command/crowdfund/create.go +++ b/internal/engine/command/crowdfund/create.go @@ -19,15 +19,15 @@ func (c *CrowdfundCmd) createHandler( packages := []entity.Package{} err := json.Unmarshal([]byte(packagesJSON), &packages) if err != nil { - return cmd.FailedResult(err.Error()) + return cmd.RenderErrorTemplate(err) } if title == "" { - return cmd.FailedResult("The title of the crowdfunding campaign cannot be empty") + return cmd.RenderFailedTemplate("The title of the crowdfunding campaign cannot be empty") } if len(packages) < 2 { - return cmd.FailedResult("At least 3 packages are required for the crowdfunding campaign") + return cmd.RenderFailedTemplate("At least 3 packages are required for the crowdfunding campaign") } campaign := &entity.CrowdfundCampaign{ @@ -38,10 +38,8 @@ func (c *CrowdfundCmd) createHandler( } err = c.db.AddCrowdfundCampaign(campaign) if err != nil { - return cmd.FailedResult(err.Error()) + return cmd.RenderErrorTemplate(err) } - return cmd.SuccessfulResultF( - "Crowdfund campaign '%s' created successfully with %d packages", - title, len(packages)) + return cmd.RenderResultTemplate(".", campaign) } diff --git a/internal/engine/command/crowdfund/crowdfund.gen.go b/internal/engine/command/crowdfund/crowdfund.gen.go index d47ae6c..fd23312 100644 --- a/internal/engine/command/crowdfund/crowdfund.gen.go +++ b/internal/engine/command/crowdfund/crowdfund.gen.go @@ -17,16 +17,18 @@ 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), } subCmdCreate = &command.Command{ - Name: "create", - Help: "Create a new crowdfunding campaign", - Handler: c.createHandler, - AppIDs: entity.AllAppIDs(), - TargetFlag: command.TargetMaskAll, + Name: "create", + Help: "Create a new crowdfunding campaign", + Handler: c.createHandler, + ResultTemplate: `Crowdfund campaign '{{.Name}}' created successfully with {{ len(.Packages) }} packages`, + AppIDs: entity.AllAppIDs(), + TargetFlag: command.TargetMaskAll, Args: []command.Args{ { Name: "title", @@ -49,32 +51,36 @@ func (c *CrowdfundCmd) crowdfundCommand() *command.Command { }, } subCmdDisable = &command.Command{ - Name: "disable", - Help: "Disable an existing crowdfunding campaign", - Handler: c.disableHandler, - AppIDs: entity.AllAppIDs(), - TargetFlag: command.TargetMaskAll, + Name: "disable", + Help: "Disable an existing crowdfunding campaign", + Handler: c.disableHandler, + ResultTemplate: ``, + AppIDs: entity.AllAppIDs(), + TargetFlag: command.TargetMaskAll, } subCmdReport = &command.Command{ - Name: "report", - Help: "View reports of a crowdfunding campaign", - Handler: c.reportHandler, - AppIDs: entity.AllAppIDs(), - TargetFlag: command.TargetMaskAll, + Name: "report", + Help: "View reports of a crowdfunding campaign", + Handler: c.reportHandler, + ResultTemplate: ``, + AppIDs: entity.AllAppIDs(), + TargetFlag: command.TargetMaskAll, } subCmdInfo = &command.Command{ - Name: "info", - Help: "Get detailed information about a crowdfunding campaign", - Handler: c.infoHandler, - AppIDs: entity.AllAppIDs(), - TargetFlag: command.TargetMaskAll, + Name: "info", + Help: "Get detailed information about a crowdfunding campaign", + Handler: c.infoHandler, + ResultTemplate: ``, + AppIDs: entity.AllAppIDs(), + TargetFlag: command.TargetMaskAll, } subCmdPurchase = &command.Command{ - Name: "purchase", - Help: "Make a purchase in a crowdfunding campaign", - Handler: c.purchaseHandler, - AppIDs: entity.AllAppIDs(), - TargetFlag: command.TargetMaskAll, + Name: "purchase", + Help: "Make a purchase in a crowdfunding campaign", + Handler: c.purchaseHandler, + ResultTemplate: ``, + AppIDs: entity.AllAppIDs(), + TargetFlag: command.TargetMaskAll, Args: []command.Args{ { Name: "package", @@ -85,11 +91,12 @@ func (c *CrowdfundCmd) crowdfundCommand() *command.Command { }, } subCmdClaim = &command.Command{ - Name: "claim", - Help: "Claim packages from a crowdfunding campaign", - Handler: c.claimHandler, - AppIDs: entity.AllAppIDs(), - TargetFlag: command.TargetMaskAll, + 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) diff --git a/internal/engine/command/crowdfund/crowdfund.go b/internal/engine/command/crowdfund/crowdfund.go index 927cd38..fd5e8e1 100644 --- a/internal/engine/command/crowdfund/crowdfund.go +++ b/internal/engine/command/crowdfund/crowdfund.go @@ -33,20 +33,18 @@ func NewCrowdfundCmd(ctx context.Context, } func (c *CrowdfundCmd) GetCommand() *command.Command { - if c.activeCampaign == nil { - return nil - } - - purchaseChoices := []command.Choice{} - for index, pkg := range c.activeCampaign.Packages { - choice := command.Choice{ - Name: pkg.Name, - Value: index, + if c.activeCampaign != nil { + purchaseChoices := []command.Choice{} + for index, pkg := range c.activeCampaign.Packages { + choice := command.Choice{ + Name: pkg.Name, + Value: index, + } + + purchaseChoices = append(purchaseChoices, choice) } - - purchaseChoices = append(purchaseChoices, choice) + subCmdPurchase.Args[0].Choices = purchaseChoices } - subCmdPurchase.Args[0].Choices = purchaseChoices return c.crowdfundCommand() } diff --git a/internal/engine/command/crowdfund/crowdfund.yml b/internal/engine/command/crowdfund/crowdfund.yml index cce71ef..1bab916 100644 --- a/internal/engine/command/crowdfund/crowdfund.yml +++ b/internal/engine/command/crowdfund/crowdfund.yml @@ -4,7 +4,8 @@ help: Commands for managing crowdfunding campaigns sub_commands: - name: create help: Create a new crowdfunding campaign - result_template: + result_template: | + Crowdfund campaign '{{.Name}}' created successfully with {{ len(.Packages) }} packages args: - name: title desc: The title of this crowdfunding campaign diff --git a/internal/engine/command/crowdfund/info.go b/internal/engine/command/crowdfund/info.go index be46397..739dca4 100644 --- a/internal/engine/command/crowdfund/info.go +++ b/internal/engine/command/crowdfund/info.go @@ -25,5 +25,5 @@ func (c *CrowdfundCmd) infoHandler( return cmd.RenderFailedTemplate("No active campaign") } - return cmd.RenderResultTemplate(infoResponseTemplate, "campaign", c.activeCampaign) + return cmd.RenderResultTemplate("campaign", c.activeCampaign) } diff --git a/internal/generator/command.tmpl b/internal/generator/command.tmpl index 12555fe..b6f7700 100644 --- a/internal/generator/command.tmpl +++ b/internal/generator/command.tmpl @@ -13,16 +13,18 @@ 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{ - Name: "{{.Name}}", - Help: "{{.Help}}", - Handler: c.{{.Name}}Handler, - AppIDs: entity.AllAppIDs(), - TargetFlag: command.TargetMaskAll, + Name: "{{.Name}}", + Help: "{{.Help}}", + Handler: c.{{.Name}}Handler, + ResultTemplate: `{{.ResultTemplate | trim}}`, + AppIDs: entity.AllAppIDs(), + TargetFlag: command.TargetMaskAll, {{- if .Args }} Args: []command.Args{ {{- range .Args}} diff --git a/internal/generator/main.go b/internal/generator/main.go index c3bc238..00ca350 100644 --- a/internal/generator/main.go +++ b/internal/generator/main.go @@ -61,10 +61,12 @@ func generateCode(cmd *command.Command) (string, error) { "title": func(str string) string { return cases.Title(language.English).String(str) }, - "string": func(s fmt.Stringer) string { return s.String() }, + "trim": func(s string) string { + return strings.TrimSpace(s) + }, } tml, err := template.New("code").Funcs(funcMap).Parse(commandTemplate)