-
Notifications
You must be signed in to change notification settings - Fork 4
/
c0079.yml
142 lines (125 loc) · 4.09 KB
/
c0079.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
doc_meta: |
folder: design-patterns
title: data structure
head: |
show how to use the design pattern of composibility to form a data structure
Firstly, the proper way using UP cmd is to use defined template to gradually stubstitue all the required variables, then register it with a formal name, remember the yaml doc is a text, also it's convertable to become a object, so that you can use it in your template
The reason why the school_* vars are defined in local func scope is to show that this var could be obtained in runtime in complex scenarios
Please note the expand level does make it more dynamic and you should make wise use of this feature
sections:
- title: Demo
log: yes
vars:
complex_data_structure_template: |
school:
name: '{{.school_name}}'
address: '{{.school_address}}'
principals:
{{- range $_, $p :=.schoo_principals }}
- {{$p}}
{{- end}}
ranking: '{{.schoo_ranking}}'
dvars:
- name: intest
value: '{{ env "GOTEST" |default "false" }}'
tasks:
-
name: task
task:
-
func: cmd
vars:
school_name: sydney grammar
school_address: 1 fox road, sydney, nsw 2000
schoo_principals:
- peter
- tom
- jane
schoo_ranking: No 5
dvars:
- name: school_string
desc: use default expand == 1
value: '{{.complex_data_structure_template}}'
do:
- name: print
cmd: '{{.school_string}}'
-
func: cmd
vars:
school_name: sydney grammar
school_address: 1 fox road, sydney, nsw 2000
schoo_principals:
- peter
- tom
- jane
schoo_ranking: No 5
dvars:
- name: school_yml
desc: use dynamic expand == 2, so that the template will be rendered
value: '{{.complex_data_structure_template}}'
expand: 2
flags: [reg]
do:
- name: print
cmd: '{{.school_yml}}'
-
func: cmd
desc: |
show global school_yml var
convert the yml to object so that it could be used in template later
dvars:
- name: school_details
desc: use dynamic expand == 2, so that the template will be rendered
value: '{{.school_yml}}'
flags: [vvv, toObj, reg]
do:
- name: print
cmd: '{{.school_yml}}'
- name: printObj
cmd: 'school_details'
- name: print
cmd: '{{.school_details}}'
- name: printObj
cmd: 'school_details_object'
- func: shell
do:
- echo "?intest -> {{.intest}}"
- pwd
reg: register_task_root
- func: cmd
do:
- name: print
cmd: 'intest is: [{{.intest}}]'
- name: reg
cmd:
name: correct_working_dir
desc: the value of .intest is string but not bool so you can not simple use if .intest for condition
value: >-
{{if eq .intest "true" }}{{.register_task_root.Output}}{{else}}{{.register_task_root.Output}}{{end}}
- func: cmd
do:
- name: print
cmd: 'root dir is: [{{.register_task_root.Output}}]'
- name: print
cmd: 'correct working dir is: [{{.correct_working_dir}}]'
-
func: cmd
desc: render final result using template
do:
- name: template
desc: render the template file using above dynamic variable from defined var
cmd:
src: '{{.correct_working_dir}}/tests/functests/d0079.template'
dest: /tmp/myschool.txt
datakey: "school_details_object"
- func: cmd
do:
- name: readFile
desc: read content of a file and register it to a var
cmd:
filename: myschool.txt
dir: /tmp
reg: my_school
-
name: print
cmd: "{{.my_school}}"