-
Notifications
You must be signed in to change notification settings - Fork 2
/
template.go
111 lines (95 loc) · 2.78 KB
/
template.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"fmt"
"strings"
"github.com/willabides/bindown/v3"
)
type templateCmd struct {
List templateListCmd `kong:"cmd,help='list templates'"`
Remove templateRemoveCmd `kong:"cmd,help='remove a template'"`
UpdateFromSource templateUpdateFromSourceCmd `kong:"cmd,help='update a template from source'"`
UpdateVars templateUpdateVarCmd `kong:"cmd,help='update template vars'"`
}
type templateUpdateVarCmd struct {
Template string `kong:"arg,predictor=localTemplate"`
Set map[string]string `kong:"help='add or update a var'"`
Unset []string `kong:"help='remove a var'"`
}
func (c *templateUpdateVarCmd) Run(ctx *runContext) error {
config, err := loadConfigFile(ctx, true)
if err != nil {
return err
}
if len(c.Set) > 0 {
err = config.SetTemplateVars(c.Template, c.Set)
if err != nil {
return err
}
}
if len(c.Unset) > 0 {
err = config.UnsetTemplateVars(c.Template, c.Unset)
if err != nil {
return err
}
}
return config.Write(ctx.rootCmd.JSONConfig)
}
type templateUpdateFromSourceCmd struct {
Source string `kong:"help='source of the update in the form of source_name#template_name',predictor=templateSource"`
Template string `kong:"arg,help='template to update',predictor=localTemplateFromSource"`
}
func (c *templateUpdateFromSourceCmd) Run(ctx *runContext) error {
if c.Source == "" {
c.Source = c.Template
}
srcParts := strings.SplitN(c.Source, "#", 2)
if len(srcParts) != 2 {
return fmt.Errorf("source must be formatted as source#name (with the #)")
}
src := srcParts[0]
srcTmpl := srcParts[1]
cfg, err := loadConfigFile(ctx, true)
if err != nil {
return err
}
if cfg.Templates == nil {
cfg.Templates = map[string]*bindown.Dependency{}
}
err = cfg.CopyTemplateFromSource(ctx, src, srcTmpl, c.Template)
if err != nil {
return err
}
return cfg.Write(ctx.rootCmd.JSONConfig)
}
type templateListCmd struct {
Source string `kong:"help='source of templates to list',predictor=templateSource"`
}
func (c *templateListCmd) Run(ctx *runContext) error {
cfg, err := loadConfigFile(ctx, true)
if err != nil {
return err
}
templates, err := cfg.ListTemplates(ctx, c.Source)
if err != nil {
return err
}
fmt.Fprintln(ctx.stdout, strings.Join(templates, "\n"))
return nil
}
type templateRemoveCmd struct {
Template string `kong:"arg,predictor=localTemplate"`
}
func (c *templateRemoveCmd) Run(ctx *runContext) error {
cfg, err := loadConfigFile(ctx, true)
if err != nil {
return err
}
if cfg.Templates == nil {
return fmt.Errorf("no template named %q", c.Template)
}
if _, ok := cfg.Templates[c.Template]; !ok {
return fmt.Errorf("no template named %q", c.Template)
}
delete(cfg.Templates, c.Template)
return cfg.Write(ctx.rootCmd.JSONConfig)
}