@@ -3,6 +3,7 @@ package resource
3
3
import (
4
4
"fmt"
5
5
"os"
6
+ "strings"
6
7
7
8
"github.com/docker/infrakit/cmd/cli/base"
8
9
"github.com/docker/infrakit/pkg/cli"
@@ -31,6 +32,7 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
31
32
Use : "resource" ,
32
33
Short : "Access resource plugin" ,
33
34
}
35
+ globals := cmd .PersistentFlags ().StringArray ("global" , []string {}, "key=value pairs of 'global' values" )
34
36
name := cmd .PersistentFlags ().String ("name" , "resource" , "Name of plugin" )
35
37
cmd .PersistentPreRunE = func (c * cobra.Command , args []string ) error {
36
38
if err := cli .EnsurePersistentPreRunE (c ); err != nil {
@@ -63,7 +65,7 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
63
65
os .Exit (1 )
64
66
}
65
67
66
- spec , err := readSpecFromTemplateURL (args [0 ])
68
+ spec , err := readSpecFromTemplateURL (args [0 ], * globals )
67
69
if err != nil {
68
70
return err
69
71
}
@@ -92,7 +94,7 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
92
94
os .Exit (1 )
93
95
}
94
96
95
- spec , err := readSpecFromTemplateURL (args [0 ])
97
+ spec , err := readSpecFromTemplateURL (args [0 ], * globals )
96
98
if err != nil {
97
99
return err
98
100
}
@@ -120,7 +122,7 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
120
122
os .Exit (1 )
121
123
}
122
124
123
- spec , err := readSpecFromTemplateURL (args [0 ])
125
+ spec , err := readSpecFromTemplateURL (args [0 ], * globals )
124
126
if err != nil {
125
127
return err
126
128
}
@@ -138,13 +140,25 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
138
140
return cmd
139
141
}
140
142
141
- func readSpecFromTemplateURL (templateURL string ) (* resource.Spec , error ) {
143
+ func readSpecFromTemplateURL (templateURL string , globals [] string ) (* resource.Spec , error ) {
142
144
log .Info ("Reading template" , "url" , templateURL )
143
145
engine , err := template .NewTemplate (templateURL , template.Options {})
144
146
if err != nil {
145
147
return nil , err
146
148
}
147
149
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
+
148
162
engine .WithFunctions (func () []template.Function {
149
163
return []template.Function {
150
164
{Name : "resource" , Func : func (s string ) string { return fmt .Sprintf ("{{ resource `%s` }}" , s ) }},
0 commit comments