Skip to content

Commit

Permalink
Merge pull request #12 from akbariandev/feat/html-help-command
Browse files Browse the repository at this point in the history
feat(command): return html base help command message
  • Loading branch information
akbariandev authored Jun 23, 2024
2 parents ec909f8 + fd63574 commit a80b12f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
2 changes: 2 additions & 0 deletions config/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ package config
const (
PriceCacheKey = "PriceCacheKey"
)

var HelpCommandTemplate string = `<table>{{range .}}<tr><td>{{ .Name }}</td><td>{{ .Desc }}</td></tr>{{end}}</table>`
9 changes: 7 additions & 2 deletions internal/engine/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}

Expand Down
44 changes: 44 additions & 0 deletions internal/engine/command/command_test.go
Original file line number Diff line number Diff line change
@@ -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<table><tr><td>A</td><td>some description for command A</td></tr><tr><td>B</td><td>some description for command B</td></tr><tr><td>C</td><td>some description for command C</td></tr></table>", message)
}
20 changes: 20 additions & 0 deletions pkg/template/html.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit a80b12f

Please sign in to comment.