if condition
Showcase using if condition
Supported true/false values
- true: [“1”, “t”, “T”, “TRUE”, “true”, “True”]
- false: [“0”, “f”, “F”, “FALSE”, “false”, “False”]
Demo
source
Main task yaml file
goal:
- add condition support
supported:
true: ["1", "t", "T", "TRUE", "true", "True"]
false: ["0", "f", "F", "FALSE", "false", "False"]
tasks:
- name: task
task:
- func: shell
do:
- echo "hello, world"
if: "true"
- func: shell
do:
- echo "hello, world"
if: "false"
- func: shell
do:
- echo "hello, world"
if: "FALSE"
- func: shell
desc: |
note that for one step, there will be only one
return, which will be the last do cmd
in this case, since the step3 exit code is 0
the whole step will have the return code of 0
if you need to use the exit code of the step
you need to consider to reduce the number of do cmds
or put it to last step, this is not a bug
this is a feature
flags:
- ignoreError
do:
- echo "step1"
- echo "step2" |grep notexist
- echo "step3"
- func: shell
do:
- echo "hello, world"
- func: shell
dvars:
- name: last_task_succeeded
value: "{{eq .last_result.Code 0}}"
flags: [vvvv]
do:
- echo "check last step"
- echo "{{.last_result|toJson}}"
- echo "{{.last_result|toPrettyJson}}"
- echo "{{eq .last_result.Code 0}}"
if: "{{eq .last_result.Code 0}}"
- func: shell
flags:
- ignoreError
do:
- echo "step1"
- echo "step2" |grep notexist
- func: shell
do:
- echo "check last step"
- echo "{{.last_result|toJson}}"
- echo "{{.last_result|toPrettyJson}}"
if: "{{eq .last_result.Code 0}}"
- func: shell
vars:
student:
name: peter
sex: male
age: 23
dvars:
- name: condition
value: '{{eq .student.sex "male"}}'
flags: [vvvv]
do:
- echo "simple dvar as condition"
if: "{{.condition}}"
- func: shell
vars:
student:
name: peter
sex: male
age: 23
dvars:
- name: condition
value: '{{and (ge .student.age 18) (eq .student.sex "male") }}'
flags: [vvvv]
do:
- echo "complicated dvar evaluation as condition"
if: "{{.condition}}"
- func: shell
vars:
student:
name: peter
sex: male
age: 23
dvars:
- name: condition
value: |
{{- with .student -}}
{{and (ge .age 18) (eq .sex "male") }}
{{- end -}}
flags: [vvvv]
do:
- echo "a even more complicated condition but more readable"
if: "{{.condition}}"
- func: shell
vars:
student:
name: peter
sex: male
age: 23
do:
- echo "a complicated condition without dvar"
if: |
{{- with .student -}}
{{and (ge .age 18) (eq .sex "male") }}
{{- end -}}
- func: cmd
desc: |
show that complicated arg needs to be quoted using ()
otherwise it will cause gt to be confused
vars:
doc: hello
do:
- name: print
cmd: '{{ gt (.doc|len) 1 }}'
- func: cmd
vars:
doc: hello
do:
- name: print
cmd: '{{ gt (.doc|len) 1 }}'
if: '{{gt (.doc|len) 0}}'
Main log file
loading [Config]: ./tests/functests/upconfig.yml
Main config:
Version -> 1.0.0
RefDir -> ./tests/functests
WorkDir -> cwd
AbsWorkDir -> /up_project/up
TaskFile -> c0054
Verbose -> vvv
ModuleName -> self
ShellType -> /bin/sh
MaxCallLayers -> 8
Timeout -> 3600000
MaxModuelCallLayers -> 256
EntryTask -> task
ModRepoUsernameRef ->
ModRepoPasswordRef ->
work dir: /up_project/up
-exec task: task
loading [Task]: ./tests/functests/c0054
module: [self], instance id: [dev], exec profile: []
profile - envVars:
(*core.Cache)({
})
Task1: [task ==> task: ]
-Step1:
self: final context exec vars:
(*core.Cache)({
"up_runtime_task_layer_number": 0
})
cmd( 1):
echo "hello, world"
-
hello, world
-
.. ok
. ok
-Step2:
self: final context exec vars:
(*core.Cache)({
"last_result": (*utils.ExecResult)({
Cmd: "echo \"hello, world\"",
Code: 0,
Output: "hello, world",
ErrMsg: ""
}),
"up_runtime_task_layer_number": 0
})
condition failed, skip executing step
-Step3:
self: final context exec vars:
(*core.Cache)({
"last_result": (*utils.ExecResult)(<nil>),
"up_runtime_task_layer_number": 0
})
condition failed, skip executing step
-Step4: [
note that for one step, there will be only one
return, which will be the last do cmd
in this case, since the step3 exit code is 0
the whole step will have the return code of 0
if you need to use the exit code of the step
you need to consider to reduce the number of do cmds
or put it to last step, this is not a bug
this is a feature
]
self: final context exec vars:
(*core.Cache)({
"last_result": (*utils.ExecResult)(<nil>),
"up_runtime_task_layer_number": 0
})
cmd( 1):
echo "step1"
-
step1
-
.. ok
cmd( 2):
echo "step2" |grep notexist
-
-
.. failed(suppressed if it is not the last step)
cmd( 3):
echo "step3"
-
step3
-
.. ok
. ok
-Step5:
self: final context exec vars:
(*core.Cache)({
"last_result": (*utils.ExecResult)({
Cmd: "echo \"step3\"",
Code: 0,
Output: "step3",
ErrMsg: ""
}),
"up_runtime_task_layer_number": 0
})
cmd( 1):
echo "hello, world"
-
hello, world
-
.. ok
. ok
-Step6:
dvar> last_task_succeeded:
"true"
-
true
self: final context exec vars:
(*core.Cache)({
"last_result": (*utils.ExecResult)({
Cmd: "echo \"hello, world\"",
Code: 0,
Output: "hello, world",
ErrMsg: ""
}),
"up_runtime_task_layer_number": 0,
"last_task_succeeded": "true"
})
cmd( 1):
echo "check last step"
-
check last step
-
.. ok
cmd( 2):
echo "{{.last_result|toJson}}"
-
{Cmd:echo "hello, world",Code:0,Output:hello, world,ErrMsg:}
-
.. ok
cmd( 3):
echo "{{.last_result|toPrettyJson}}"
-
{
Cmd: echo "hello, world",
Code: 0,
Output: hello, world,
ErrMsg:
}
-
.. ok
cmd( 4):
echo "{{eq .last_result.Code 0}}"
-
true
-
.. ok
. ok
-Step7:
self: final context exec vars:
(*core.Cache)({
"up_runtime_task_layer_number": 0,
"last_result": (*utils.ExecResult)({
Cmd: "echo \"true\"",
Code: 0,
Output: "true",
ErrMsg: ""
})
})
cmd( 1):
echo "step1"
-
step1
-
.. ok
cmd( 2):
echo "step2" |grep notexist
-
-
.. failed(suppressed if it is not the last step)
WARN: [ignoreError:] - [Error ignored!!!]
-Step8:
self: final context exec vars:
(*core.Cache)({
"up_runtime_task_layer_number": 0,
"last_result": (*utils.ExecResult)({
Cmd: "echo \"step2\" |grep notexist",
Code: 1,
Output: "",
ErrMsg: "exit status 1"
})
})
condition failed, skip executing step
-Step9:
dvar> condition:
"true"
-
true
self: final context exec vars:
(*core.Cache)({
"last_result": (*utils.ExecResult)(<nil>),
"up_runtime_task_layer_number": 0,
"student": {
"sex": "male",
"age": 23,
"name": "peter"
},
"condition": "true"
})
cmd( 1):
echo "simple dvar as condition"
-
simple dvar as condition
-
.. ok
. ok
-Step10:
dvar> condition:
"true"
-
true
self: final context exec vars:
(*core.Cache)({
"condition": "true",
"last_result": (*utils.ExecResult)({
Cmd: "echo \"simple dvar as condition\"",
Code: 0,
Output: "simple dvar as condition",
ErrMsg: ""
}),
"up_runtime_task_layer_number": 0,
"student": {
"sex": "male",
"age": 23,
"name": "peter"
}
})
cmd( 1):
echo "complicated dvar evaluation as condition"
-
complicated dvar evaluation as condition
-
.. ok
. ok
-Step11:
dvar> condition:
"true"
-
true
self: final context exec vars:
(*core.Cache)({
"student": {
"name": "peter",
"sex": "male",
"age": 23
},
"condition": "true",
"last_result": (*utils.ExecResult)({
Cmd: "echo \"complicated dvar evaluation as condition\"",
Code: 0,
Output: "complicated dvar evaluation as condition",
ErrMsg: ""
}),
"up_runtime_task_layer_number": 0
})
cmd( 1):
echo "a even more complicated condition but more readable"
-
a even more complicated condition but more readable
-
.. ok
. ok
-Step12:
self: final context exec vars:
(*core.Cache)({
"up_runtime_task_layer_number": 0,
"student": {
"name": "peter",
"sex": "male",
"age": 23
},
"last_result": (*utils.ExecResult)({
Cmd: "echo \"a even more complicated condition but more readable\"",
Code: 0,
Output: "a even more complicated condition but more readable",
ErrMsg: ""
})
})
cmd( 1):
echo "a complicated condition without dvar"
-
a complicated condition without dvar
-
.. ok
. ok
-Step13: [
show that complicated arg needs to be quoted using ()
otherwise it will cause gt to be confused
]
self: final context exec vars:
(*core.Cache)({
"last_result": (*utils.ExecResult)({
Cmd: "echo \"a complicated condition without dvar\"",
Code: 0,
Output: "a complicated condition without dvar",
ErrMsg: ""
}),
"up_runtime_task_layer_number": 0,
"doc": "hello"
})
~SubStep1: [print: ]
true
-Step14:
self: final context exec vars:
(*core.Cache)({
"last_result": (*utils.ExecResult)({
Cmd: "echo \"a complicated condition without dvar\"",
Code: 0,
Output: "a complicated condition without dvar",
ErrMsg: ""
}),
"up_runtime_task_layer_number": 0,
"doc": "hello"
})
~SubStep1: [print: ]
true
Logs with different verbose level
Raw logs with different verbose level