-
Notifications
You must be signed in to change notification settings - Fork 2
/
templatesource.go
72 lines (61 loc) · 1.74 KB
/
templatesource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"fmt"
"sort"
"text/tabwriter"
"golang.org/x/exp/maps"
)
type templateSourceCmd struct {
List templateSourceListCmd `kong:"cmd,help='list configured template sources'"`
Add templateSourceAddCmd `kong:"cmd,help='add a template source'"`
Remove templateSourceRemoveCmd `kong:"cmd,help='remove a template source'"`
}
type templateSourceListCmd struct{}
func (c *templateSourceListCmd) Run(ctx *runContext) error {
cfg, err := loadConfigFile(ctx, true)
if err != nil {
return err
}
w := tabwriter.NewWriter(ctx.stdout, 0, 0, 1, ' ', 0)
sourceNames := maps.Keys(cfg.TemplateSources)
sort.Strings(sourceNames)
for _, name := range sourceNames {
fmt.Fprintln(w, name+"\t"+cfg.TemplateSources[name])
}
return w.Flush()
}
type templateSourceAddCmd struct {
Name string `kong:"arg"`
Source string `kong:"arg"`
}
func (c *templateSourceAddCmd) Run(ctx *runContext) error {
cfg, err := loadConfigFile(ctx, true)
if err != nil {
return err
}
if cfg.TemplateSources == nil {
cfg.TemplateSources = map[string]string{}
}
if _, ok := cfg.TemplateSources[c.Name]; ok {
return fmt.Errorf("template source already exists")
}
cfg.TemplateSources[c.Name] = c.Source
return cfg.Write(ctx.rootCmd.JSONConfig)
}
type templateSourceRemoveCmd struct {
Name string `kong:"arg,predictor=templateSource"`
}
func (c *templateSourceRemoveCmd) Run(ctx *runContext) error {
cfg, err := loadConfigFile(ctx, true)
if err != nil {
return err
}
if cfg.TemplateSources == nil {
return fmt.Errorf("no template source named %q", c.Name)
}
if _, ok := cfg.TemplateSources[c.Name]; !ok {
return fmt.Errorf("no template source named %q", c.Name)
}
delete(cfg.TemplateSources, c.Name)
return cfg.Write(ctx.rootCmd.JSONConfig)
}