@@ -3,6 +3,7 @@ package resource
33import (
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