Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 022ed70

Browse files
nwtDavid Chung
authored andcommitted
Add global flag to manager and resource CLI subcommands (#451)
Signed-off-by: Noah Treuhaft <[email protected]>
1 parent ecf667f commit 022ed70

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

cmd/cli/manager/manager.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package manager
33
import (
44
"fmt"
55
"os"
6+
"strings"
67

78
"github.com/docker/infrakit/cmd/cli/base"
89
"github.com/docker/infrakit/pkg/cli"
@@ -78,6 +79,7 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
7879
Use: "commit <template_URL>",
7980
Short: "commit a multi-group configuration, as specified by the URL",
8081
}
82+
globals := commit.Flags().StringArray("global", []string{}, "key=value pairs of 'global' values")
8183
pretend := commit.Flags().Bool("pretend", false, "Don't actually commit, only explain the commit")
8284
commit.RunE = func(cmd *cobra.Command, args []string) error {
8385

@@ -94,6 +96,18 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
9496
return err
9597
}
9698

99+
for _, global := range *globals {
100+
kv := strings.SplitN(global, "=", 2)
101+
if len(kv) != 2 {
102+
continue
103+
}
104+
key := strings.TrimSpace(kv[0])
105+
val := strings.TrimSpace(kv[1])
106+
if key != "" && val != "" {
107+
engine.Global(key, val)
108+
}
109+
}
110+
97111
engine.WithFunctions(func() []template.Function {
98112
return []template.Function{
99113
{

cmd/cli/resource/resource.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package resource
33
import (
44
"fmt"
55
"os"
6+
"strings"
67

78
"github.com/docker/infrakit/cmd/cli/base"
89
"github.com/docker/infrakit/pkg/cli"
@@ -31,6 +32,7 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
3132
Use: "resource",
3233
Short: "Access resource plugin",
3334
}
35+
globals := cmd.PersistentFlags().StringArray("global", []string{}, "key=value pairs of 'global' values")
3436
name := cmd.PersistentFlags().String("name", "resource", "Name of plugin")
3537
cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {
3638
if err := cli.EnsurePersistentPreRunE(c); err != nil {
@@ -63,7 +65,7 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
6365
os.Exit(1)
6466
}
6567

66-
spec, err := readSpecFromTemplateURL(args[0])
68+
spec, err := readSpecFromTemplateURL(args[0], *globals)
6769
if err != nil {
6870
return err
6971
}
@@ -92,7 +94,7 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
9294
os.Exit(1)
9395
}
9496

95-
spec, err := readSpecFromTemplateURL(args[0])
97+
spec, err := readSpecFromTemplateURL(args[0], *globals)
9698
if err != nil {
9799
return err
98100
}
@@ -120,7 +122,7 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
120122
os.Exit(1)
121123
}
122124

123-
spec, err := readSpecFromTemplateURL(args[0])
125+
spec, err := readSpecFromTemplateURL(args[0], *globals)
124126
if err != nil {
125127
return err
126128
}
@@ -138,13 +140,25 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
138140
return cmd
139141
}
140142

141-
func readSpecFromTemplateURL(templateURL string) (*resource.Spec, error) {
143+
func readSpecFromTemplateURL(templateURL string, globals []string) (*resource.Spec, error) {
142144
log.Info("Reading template", "url", templateURL)
143145
engine, err := template.NewTemplate(templateURL, template.Options{})
144146
if err != nil {
145147
return nil, err
146148
}
147149

150+
for _, global := range globals {
151+
kv := strings.SplitN(global, "=", 2)
152+
if len(kv) != 2 {
153+
continue
154+
}
155+
key := strings.TrimSpace(kv[0])
156+
val := strings.TrimSpace(kv[1])
157+
if key != "" && val != "" {
158+
engine.Global(key, val)
159+
}
160+
}
161+
148162
engine.WithFunctions(func() []template.Function {
149163
return []template.Function{
150164
{Name: "resource", Func: func(s string) string { return fmt.Sprintf("{{ resource `%s` }}", s) }},

0 commit comments

Comments
 (0)