github.com/upcmd/up@v0.8.1-0.20230108151705-ad8b797bf04f/tests/functests/c0054.yml (about) 1 doc_meta: | 2 folder: flow-controll 3 title: if condition 4 head: | 5 Showcase using if condition 6 7 sections: 8 - title: Supported true/false values 9 content: | 10 * true: ["1", "t", "T", "TRUE", "true", "True"] 11 * false: ["0", "f", "F", "FALSE", "false", "False"] 12 13 - title: Demo 14 log: yes 15 16 goal: 17 - add condition support 18 19 supported: 20 true: ["1", "t", "T", "TRUE", "true", "True"] 21 false: ["0", "f", "F", "FALSE", "false", "False"] 22 23 tasks: 24 - 25 name: task 26 task: 27 28 - func: shell 29 do: 30 - echo "hello, world" 31 if: "true" 32 33 - func: shell 34 do: 35 - echo "hello, world" 36 if: "false" 37 38 - func: shell 39 do: 40 - echo "hello, world" 41 if: "FALSE" 42 43 - func: shell 44 desc: | 45 note that for one step, there will be only one 46 return, which will be the last do cmd 47 in this case, since the step3 exit code is 0 48 the whole step will have the return code of 0 49 if you need to use the exit code of the step 50 you need to consider to reduce the number of do cmds 51 or put it to last step, this is not a bug 52 this is a feature 53 flags: 54 - ignoreError 55 do: 56 - echo "step1" 57 - echo "step2" |grep notexist 58 - echo "step3" 59 60 - func: shell 61 do: 62 - echo "hello, world" 63 64 - func: shell 65 dvars: 66 - name: last_task_succeeded 67 value: "{{eq .last_result.Code 0}}" 68 flags: [vvvv] 69 do: 70 - echo "check last step" 71 - echo "{{.last_result|toJson}}" 72 - echo "{{.last_result|toPrettyJson}}" 73 - echo "{{eq .last_result.Code 0}}" 74 if: "{{eq .last_result.Code 0}}" 75 76 - func: shell 77 flags: 78 - ignoreError 79 do: 80 - echo "step1" 81 - echo "step2" |grep notexist 82 83 - func: shell 84 do: 85 - echo "check last step" 86 - echo "{{.last_result|toJson}}" 87 - echo "{{.last_result|toPrettyJson}}" 88 if: "{{eq .last_result.Code 0}}" 89 90 - func: shell 91 vars: 92 student: 93 name: peter 94 sex: male 95 age: 23 96 dvars: 97 - name: condition 98 value: '{{eq .student.sex "male"}}' 99 flags: [vvvv] 100 do: 101 - echo "simple dvar as condition" 102 if: "{{.condition}}" 103 104 - func: shell 105 vars: 106 student: 107 name: peter 108 sex: male 109 age: 23 110 dvars: 111 - name: condition 112 value: '{{and (ge .student.age 18) (eq .student.sex "male") }}' 113 flags: [vvvv] 114 do: 115 - echo "complicated dvar evaluation as condition" 116 if: "{{.condition}}" 117 118 - func: shell 119 vars: 120 student: 121 name: peter 122 sex: male 123 age: 23 124 dvars: 125 - name: condition 126 value: | 127 {{- with .student -}} 128 {{and (ge .age 18) (eq .sex "male") }} 129 {{- end -}} 130 flags: [vvvv] 131 do: 132 - echo "a even more complicated condition but more readable" 133 if: "{{.condition}}" 134 135 - func: shell 136 vars: 137 student: 138 name: peter 139 sex: male 140 age: 23 141 do: 142 - echo "a complicated condition without dvar" 143 if: | 144 {{- with .student -}} 145 {{and (ge .age 18) (eq .sex "male") }} 146 {{- end -}} 147 148 - 149 func: cmd 150 desc: | 151 show that complicated arg needs to be quoted using () 152 otherwise it will cause gt to be confused 153 vars: 154 doc: hello 155 do: 156 - name: print 157 cmd: '{{ gt (.doc|len) 1 }}' 158 - 159 func: cmd 160 vars: 161 doc: hello 162 do: 163 - name: print 164 cmd: '{{ gt (.doc|len) 1 }}' 165 if: '{{gt (.doc|len) 0}}'