Skip to content

Commit

Permalink
faet: add result template to command struct
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f committed Jan 8, 2025
1 parent e98e2cf commit d382b46
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 72 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 12 additions & 11 deletions internal/engine/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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!")
}
Expand All @@ -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)
}
Expand Down
12 changes: 5 additions & 7 deletions internal/engine/command/crowdfund/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
}
67 changes: 37 additions & 30 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.

22 changes: 10 additions & 12 deletions internal/engine/command/crowdfund/crowdfund.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
3 changes: 2 additions & 1 deletion internal/engine/command/crowdfund/crowdfund.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/command/crowdfund/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
12 changes: 7 additions & 5 deletions internal/generator/command.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
4 changes: 3 additions & 1 deletion internal/generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d382b46

Please sign in to comment.