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