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

Commit 12aaeda

Browse files
YujiOshimaDavid Chung
authored andcommitted
playbook supports string list (#539)
Signed-off-by: YujiOshima <[email protected]>
1 parent 9dfe658 commit 12aaeda

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

docs/playbooks/intro/aws/provision-group.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
{{ $image := flag "image-id" "string" "Image ID" | prompt "Image to boot?" "string" "ami-4926fd29" }}
88
{{ $instanceType := flag "instance-type" "string" "Instance type" | prompt "Instance type?" "string" "t2.micro"}}
99
{{ $sshKey := flag "ssh-key" "string" "SSH key name" | prompt "SSH key?" "string" "" }}
10-
{{ $SecurityGroupId := flag "security-group-id" "string" "Security group ID" | prompt "Names of security group ID?" "string" "" }}
10+
{{ $SecurityGroupId := listflag "security-group-id" "string" "Security group ID ( Comma-separated )" | listprompt "Names of security group ID ( Comma-separated )?" "string" "" }}
1111
{{ $project := var "/project" | flag "project" "string" "Project name" | prompt "Project?" "string" "myproject" }}
1212

1313
- Plugin: group
@@ -36,6 +36,8 @@
3636
InstanceType: {{ $instanceType }}
3737
KeyName: {{ $sshKey }}
3838
SecurityGroupIds:
39-
- {{ $SecurityGroupId }}
39+
{{range $SecurityGroupId}}
40+
- {{ . }}
41+
{{end}}
4042
Tags:
4143
Name: {{ $groupName }}

pkg/cli/context.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,30 @@ func (c *Context) Funcs() []template.Function {
251251
return c.defineFlag(n, ftype, desc, defaultValue)
252252
},
253253
},
254+
{
255+
Name: "listflag",
256+
Func: func(n, ftype, desc string, optional ...interface{}) ([]string, error) {
257+
if ftype == "" {
258+
return nil, fmt.Errorf("missing type for variable %v", n)
259+
}
260+
if ftype != "string" {
261+
return nil, fmt.Errorf("list flag only support string %v", n)
262+
}
263+
var defaultValue interface{}
264+
if len(optional) > 0 {
265+
defaultValue = optional[0]
266+
}
267+
if c.exec {
268+
d, err := c.cmd.Flags().GetString(n)
269+
return strings.Split(d, ","), err
270+
}
271+
272+
// Defining a flag never returns a printable value that can mess up the template rendering
273+
d, err := c.defineFlag(n, ftype, desc, defaultValue)
274+
dl := strings.Split(d.(string), ",")
275+
return dl, err
276+
},
277+
},
254278
{
255279
Name: "fetch",
256280
Func: func(p string, opt ...interface{}) (string, error) {
@@ -358,6 +382,47 @@ func (c *Context) Funcs() []template.Function {
358382
return Prompt(c.input, prompt, ftype, optional...)
359383
},
360384
},
385+
{
386+
Name: "listprompt",
387+
Func: func(prompt, ftype string, optional ...interface{}) ([]string, error) {
388+
389+
if ftype == "" {
390+
return nil, fmt.Errorf("missing type for variable prompted")
391+
}
392+
if ftype == "" {
393+
return nil, fmt.Errorf("listprompt only support string")
394+
}
395+
var pl []string
396+
if !c.exec {
397+
return pl, nil
398+
}
399+
400+
if len(optional) > 0 {
401+
end := optional[len(optional)-1]
402+
if cond, is := end.(func() (bool, interface{})); is {
403+
ok, last := cond()
404+
if !ok {
405+
return pl, nil
406+
}
407+
// if the condition evaluates to true, then we'd continue
408+
// so the trailing arg must look like the cond was not
409+
// inserted before this -- hence using the value from
410+
// stage before the cond as the end
411+
end = last
412+
}
413+
414+
// The last value in the optional var args is the value from the previous
415+
// pipeline.
416+
if len(end.([]string)) > 1 {
417+
return end.([]string), nil
418+
}
419+
420+
}
421+
p, err := Prompt(c.input, prompt, ftype, optional...)
422+
pl = strings.Split(p.(string), ",")
423+
return pl, err
424+
},
425+
},
361426
}
362427
}
363428

pkg/cli/context_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func TestContext(t *testing.T) {
3333
{{ $clusterName := flag "cluster-name" "string" "the name of the cluster" "swarm" }}
3434
{{ $clusterSize := flag "size" "int" "the size of the cluster" 20 }}
3535
{{ $floatValue := flag "param" "float" "some float param" 25.5 }}
36+
{{ $listValue := listflag "tags" "string" "some string tags (Comma-separated)" "test" }}
3637
3738
{{ $user := prompt "Please enter your user name" "string" }}
3839
@@ -46,7 +47,8 @@ func TestContext(t *testing.T) {
4647
"username" : "{{$user}}",
4748
"doCommit" : {{$doCommit}},
4849
"instanceType" : "{{$instanceType}}",
49-
"param" : {{$floatValue}}
50+
"param" : {{$floatValue}},
51+
"tags" : "{{$listValue}}"
5052
}
5153
`
5254

@@ -67,7 +69,7 @@ func TestContext(t *testing.T) {
6769
require.NotNil(t, c.cmd.Flag(n))
6870
}
6971

70-
err = c.cmd.Flags().Parse(strings.Split("--param 75.0 --cluster-name swarm1 --commit true --size 20 --instance-type large", " "))
72+
err = c.cmd.Flags().Parse(strings.Split("--param 75.0 --cluster-name swarm1 --tags dev,infrakit --commit true --size 20 --instance-type large", " "))
7173
require.NoError(t, err)
7274

7375
err = c.Execute()
@@ -85,6 +87,7 @@ func TestContext(t *testing.T) {
8587
"username": "username",
8688
"doCommit": true,
8789
"instanceType": "large",
90+
"tags": "[dev infrakit]",
8891
}).String(), types.AnyValueMust(m).String())
8992
}
9093

0 commit comments

Comments
 (0)