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

Commit 8be63aa

Browse files
author
David Chung
authored
GCP example + support default values in prompts (#466)
Signed-off-by: David Chung <[email protected]>
1 parent e4e9d7c commit 8be63aa

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

examples/cli/gcp/provision-instance.ikc

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
# Input to create instance using the GCP instance plugin
22
{{/* =% sh %= */}}
33

4-
{{ $user := flag "user" "string" "owner" | prompt "Owner?" "string" }}
5-
{{ $prefix := flag "prefix" "string" "Prefix to use" | prompt "Prefix for hostname:" "string" }}
6-
{{ $diskSize := flag "disk-size" "int" "Disk size in mb" | prompt "Disk size in MB [60]?" "int"}}
7-
{{ $machineType := flag "machine-type" "string" "Machine type" | prompt "Machine type [n1-standard-1]?" "string"}}
4+
{{ $user := flag "user" "string" "owner" | prompt "Owner?" "string" (env "USER") nil }}
5+
{{ $prefix := flag "prefix" "string" "Prefix to use" | prompt "Prefix for hostname:" "string" (env "USER") }}
6+
{{ $diskSize := flag "disk-size" "int" "Disk size in mb" | prompt "Disk size in MB?" "int" 100 }}
7+
{{ $machineType := flag "machine-type" "string" "Machine type" | prompt "Machine type?" "string" "n1-standard-1"}}
8+
{{ $privateIP := flag "private-ip" "string" "Private IP" | prompt "Private IP address (IPv4)?" "string" "10.128.0.10" nil }}
9+
{{ $image := flag "image" "string" "Image" | prompt "Image to boot?" "string" "https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1404-trusty-v20161205" }}
10+
11+
echo "===> Running with {{$user}}, {{$image}}, {{$privateIP}}:"
812

913
infrakit --log 3 --log-stack --name instance-gcp instance provision -y - <<EOF
1014

15+
LogicalID : {{ $privateIP }}
1116
Tags:
1217
infrakit-user: {{ $user }}
1318
infrakit-created: {{ now | htmlDate }}
@@ -20,11 +25,12 @@ Init: |
2025

2126
Properties:
2227
NamePrefix: {{ $prefix }}
28+
PrivateIP: {{ $privateIP }}
2329
Description: Some description
2430
Network: default
2531
MachineType: {{ $machineType }}
2632
DiskSizeMb: {{ $diskSize }}
27-
DiskImage: https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1404-trusty-v20161205
33+
DiskImage: {{ $image }}
2834
Scopes:
2935
- https://www.googleapis.com/auth/cloudruntimeconfig
3036
- https://www.googleapis.com/auth/logging.write

pkg/cli/local/context.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,23 @@ func parseBool(text string) (bool, error) {
165165
return v > 0, err
166166
}
167167

168-
func (c *Context) prompt(prompt, ftype string) (interface{}, error) {
168+
func (c *Context) prompt(prompt, ftype string, optional ...interface{}) (interface{}, error) {
169+
def, label := "", ""
170+
if len(optional) > 0 {
171+
def = fmt.Sprintf("%v", optional[0])
172+
if def != "" {
173+
label = fmt.Sprintf("[%s]", def)
174+
}
175+
}
176+
169177
input := bufio.NewReader(c.input)
170-
fmt.Fprintf(os.Stderr, "%s ", prompt)
178+
fmt.Fprintf(os.Stderr, "%s %s: ", prompt, label)
171179
text, _ := input.ReadString('\n')
172180
text = strings.Trim(text, " \t\n")
181+
if len(text) == 0 {
182+
text = def
183+
}
184+
173185
switch ftype {
174186
case "string":
175187
return text, nil
@@ -229,10 +241,13 @@ func (c *Context) Funcs() []template.Function {
229241
if !c.exec {
230242
return "", nil
231243
}
232-
if len(optional) > 0 && !missing(ftype, optional[0]) {
233-
return optional[0], nil
244+
// The last value in the optional var args is the value from the previous
245+
// pipeline.
246+
if len(optional) > 0 && !missing(ftype, optional[len(optional)-1]) {
247+
return optional[len(optional)-1], nil
234248
}
235-
return c.prompt(prompt, ftype)
249+
250+
return c.prompt(prompt, ftype, optional...)
236251
},
237252
},
238253
}

0 commit comments

Comments
 (0)