Skip to content

Commit e7a2d19

Browse files
LeVraiBaptistelanathlor
authored andcommitted
fix(ctl): template keys list
Added a command `stamusctl template keys` to list the available keys in a template.
1 parent d308256 commit e7a2d19

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

cmd/ctl/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"stamus-ctl/cmd/ctl/compose"
88
"stamus-ctl/cmd/ctl/config"
9+
tmpl "stamus-ctl/cmd/ctl/template"
910
"stamus-ctl/internal/logging"
1011
"stamus-ctl/internal/models"
1112

@@ -47,5 +48,6 @@ func rootCmd() *cobra.Command {
4748
cmd.AddCommand(loginCmd())
4849
cmd.AddCommand(compose.ComposeCmd())
4950
cmd.AddCommand(config.ConfigCmd())
51+
cmd.AddCommand(tmpl.TemplateCmd())
5052
return cmd
5153
}

cmd/ctl/template/keys.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
flags "stamus-ctl/internal/handlers"
6+
tmpl "stamus-ctl/internal/handlers/template"
7+
8+
"github.com/spf13/cobra"
9+
)
10+
11+
// Command
12+
func keysCmd() *cobra.Command {
13+
cmd := &cobra.Command{
14+
Use: "keys",
15+
Short: "Get list of template keys",
16+
RunE: func(cmd *cobra.Command, args []string) error {
17+
return keysHandler()
18+
},
19+
}
20+
// Flags
21+
flags.Template.AddAsFlag(cmd, false)
22+
flags.Markdown.AddAsFlag(cmd, false)
23+
return cmd
24+
}
25+
26+
func keysHandler() error {
27+
// Validate flags
28+
value, err := flags.Template.GetValue()
29+
if err != nil {
30+
return err
31+
}
32+
if value == "" {
33+
return fmt.Errorf("template folder is required, use --template or -t to set and `config list` to list available templates")
34+
}
35+
isMd, err := flags.Markdown.GetValue()
36+
if err != nil {
37+
return err
38+
}
39+
// Call handler
40+
return tmpl.KeysHandler(value.(string), isMd.(bool))
41+
}

cmd/ctl/template/root.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package config
2+
3+
import (
4+
// Common
5+
6+
// External
7+
8+
"github.com/spf13/cobra"
9+
// Custom
10+
"stamus-ctl/internal/app"
11+
"stamus-ctl/internal/embeds"
12+
)
13+
14+
// Init
15+
func init() {
16+
// Setup
17+
embeds.InitClearNDRFolder(app.DefaultClearNDRPath)
18+
}
19+
20+
// Commands
21+
func TemplateCmd() *cobra.Command {
22+
// Create command
23+
cmd := &cobra.Command{
24+
Use: "template",
25+
Short: "Interact with compose config file",
26+
}
27+
// Add Commands
28+
cmd.AddCommand(keysCmd())
29+
return cmd
30+
}

internal/handlers/template/keys.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package config
2+
3+
import (
4+
"os"
5+
"stamus-ctl/internal/models"
6+
7+
"github.com/jedib0t/go-pretty/v6/table"
8+
)
9+
10+
func KeysHandler(templatePath string, isMd bool) error {
11+
// Get template keys
12+
confFile, err := models.CreateFile(templatePath, "config.yaml")
13+
if err != nil {
14+
return err
15+
}
16+
config, err := models.ConfigFromFile(confFile)
17+
if err != nil {
18+
return err
19+
}
20+
params, _, err := config.ExtractParams()
21+
if err != nil {
22+
return err
23+
}
24+
// Prepare data
25+
rows := []table.Row{}
26+
for _, name := range params.GetOrdered() {
27+
param := params.Get(name)
28+
choices := ""
29+
if param.Choices != nil {
30+
for nb, choice := range param.Choices {
31+
choices = choices + choice.AsString()
32+
if nb != len(param.Choices)-1 {
33+
choices = choices + ", "
34+
if nb%4 == 3 {
35+
choices = choices + "\n"
36+
}
37+
}
38+
}
39+
}
40+
rows = append(rows, table.Row{name, param.Type, param.Default.AsString(), choices})
41+
}
42+
// Print
43+
t := table.NewWriter()
44+
t.SetStyle(table.StyleRounded)
45+
header := table.Row{"Key", "Type", "Default", "Choices"}
46+
t.SetOutputMirror(os.Stdout)
47+
t.AppendHeader(header)
48+
t.AppendRows(rows)
49+
t.AppendFooter(header)
50+
if isMd {
51+
t.RenderMarkdown()
52+
} else {
53+
t.Render()
54+
}
55+
return nil
56+
}

0 commit comments

Comments
 (0)