diff --git a/config/global.go b/config/global.go index 1c1097e1..4268ec3e 100644 --- a/config/global.go +++ b/config/global.go @@ -3,3 +3,5 @@ package config const ( PriceCacheKey = "PriceCacheKey" ) + +var HelpCommandTemplate string = `{{range .}}{{end}}
{{ .Name }}{{ .Desc }}
` diff --git a/internal/engine/command/command.go b/internal/engine/command/command.go index 6622c769..86b65897 100644 --- a/internal/engine/command/command.go +++ b/internal/engine/command/command.go @@ -4,6 +4,9 @@ import ( "fmt" "slices" + "github.com/pagu-project/Pagu/config" + "github.com/pagu-project/Pagu/pkg/template" + "github.com/pagu-project/Pagu/internal/entity" ) @@ -96,10 +99,12 @@ func (cmd *Command) HasSubCommand() bool { func (cmd *Command) HelpMessage() string { help := cmd.Help help += "\n\nAvailable commands:\n" - for _, sc := range cmd.SubCommands { - help += fmt.Sprintf(" %-12s %s\n", sc.Name, sc.Desc) + message, err := template.ExecuteHtml(config.HelpCommandTemplate, cmd.SubCommands) + if err != nil { + return "" } + help += message return help } diff --git a/internal/engine/command/command_test.go b/internal/engine/command/command_test.go new file mode 100644 index 00000000..65939a1b --- /dev/null +++ b/internal/engine/command/command_test.go @@ -0,0 +1,44 @@ +package command + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/pagu-project/Pagu/internal/entity" +) + +func commandTestSetup() Command { + return Command{ + Name: "Help", + Desc: "", + Help: "", + Args: []Args{}, + SubCommands: []Command{ + { + Name: "A", + Desc: "some description for command A", + Help: "", + }, + { + Name: "B", + Desc: "some description for command B", + Help: "", + }, + { + Name: "C", + Desc: "some description for command C", + Help: "", + }, + }, + Middlewares: nil, + AppIDs: entity.AllAppIDs(), + User: &entity.User{ID: 1}, + } +} + +func TestCommand_HelpMessage(t *testing.T) { + command := commandTestSetup() + message := command.HelpMessage() + assert.Equal(t, "\n\nAvailable commands:\n
Asome description for command A
Bsome description for command B
Csome description for command C
", message) +} diff --git a/pkg/template/html.go b/pkg/template/html.go new file mode 100644 index 00000000..f75fdcf5 --- /dev/null +++ b/pkg/template/html.go @@ -0,0 +1,20 @@ +package template + +import ( + "bytes" + "html/template" +) + +func ExecuteHtml(tmpl string, keyValue any) (string, error) { + b := bytes.Buffer{} + tp, err := template.New("").Parse(tmpl) + if err != nil { + return "", err + } + + if err = tp.Execute(&b, keyValue); err != nil { + return "", err + } + + return b.String(), nil +}