advanced usage

Demo usecases of builtin with, block, definition

Also demo the dynamic templating using expand so that you can compose multiple templates

Demo

source

Main task yaml file
    vars:
      ns: prod
      pod_name: web_app
      ha: true
      age: 34
      old: 54
      nobody:
      admins: [tom, jason, alice]
      managers:
      - tom
      - jason
      - alice
      student:
        name: Tom
        gender: Male
        teachers:
        - tom
        - jason
        - alice
        address:
          suburb:
            name: sydney
            postcode: 2000
            CBD: yes
          school: Sydney Grammar
          #due to the viper's lower case all var keys, please use lower case as reference
      template_def_consume_dynamic_T1: |
        {{define "T1"}}ONE: {{.}}{{end}}
      template_def_consume_dynamic_T2: '{{define "T2"}}TWO: {{.}}{{end}}'
      template_def_consume_dynamic_T3: |
        {{define "T3"}}student:
          name: {{template "T1" .name}}
          gender: {{template "T2" .gender}}{{end}}
        {{template "T3" .student}}
      template_def_consume_dynamic_main: |
        {{.template_def_consume_dynamic_t1}}
        {{.template_def_consume_dynamic_t2}}
        {{.template_def_consume_dynamic_t3}}
        {{template "T3" .student}}
    dvars:
    - name: usage_of_with
      value: ' {{ with .student }} student name: {{ println .name}} {{ with .address}}
        school name: {{ println .school}} {{ end}} {{ println .gender}} {{ end}} '
      flags: [vvvv]
    - name: usage_of_with_else
      value: ' {{ with .nobody }} i am nobody {{else}} i am somebody {{ end}} '
      flags: [vvvv]
    - name: template_def_plain
      value: |
        {{define "T1"}}ONE{{end}}
        {{define "T2"}}TWO{{end}}
        {{define "T3"}}{{template "T1"}} {{template "T2"}}{{end}}
        {{template "T3"}}"
      flags: [vvvv]
    - name: template_def_consume
      value: |2
    
        show example of how you can define reusable template
        and how you can pass in the parameters
    
        {{define "T1"}}ONE: {{.}}{{end}}
        {{define "T2"}}TWO: {{.}}{{end}}
        {{define "T3"}}student:
          name: {{template "T1" .name}}
          gender: {{template "T2" .gender}}{{end}}
        {{template "T3" .student}}
      flags: [vvvv]
      #  NOTE: this will not work, as the value itself serve as plain text as template rather than
      #  dynamic rendering, so this reusable definition of template should go to vars section
      #-------------------------------------------------------
      #  - name: template_def_consume_dynamic_T1
      #    value: |
      #      {{define "T1"}}ONE: {{.}}{{end}}
      #    flags: [vvvv,]
      #
      #  - name: template_def_consume_dynamic_T2
      #    value: |
      #      {{define "T1"}}ONE: {{.}}{{end}}
      #    flags: [vvvv,]
      #
      #  - name: template_def_consume_dynamic_T3
      #    value: |
      #      {{.template_def_consume_dynamic_T1}}
      #      {{.template_def_consume_dynamic_T2}}
      #      {{define "T3"}}student:
      #        name: {{template "T1" .name}}
      #        gender: {{template "T2" .gender}}{{end}}
      #      {{template "T3" .student}}
      #    flags: [vvvv,]
      #-------------------------------------------------------
    - name: template_def_consume_dynamic
      value: |
        {{.template_def_consume_dynamic_main}}
      flags: [vvvv]
      expand: 3
    - name: template_use_block
      desc: |
        A block is shorthand for defining a template
          {{define "name"}} T1 {{end}}
        and then executing it in place
          {{template "name" pipeline}}
        The typical use is to define a set of root templates that are
        then customized by redefining the block templates within.
    
        In this case the define of T3 and execution using template
        are merged into one block statement
      value: |
        {{define "T1"}}ONE: {{.}}{{end}}
        {{define "T2"}}TWO: {{.}}{{end}}
        {{block "T3" .student}}student:
          name: {{template "T1" .name}}
        gender: {{template "T2" .gender}}
        {{end}}
      flags: [vvvv]
    tasks:
    - name: task
      task:
      - func: shell
        do:
        - echo "value\n {{.usage_of_with}}"
        - echo "{{.usage_of_template}}"
        - echo "{{.template_def_consume}}"
        - echo "{{.template_def_consume_dynamic_t1}}"
        - echo "{{.template_def_consume_dynamic_t2}}"
        - echo "{{.template_def_consume_dynamic_t3}}"
        - echo "{{.template_def_consume_dynamic}}"
        - echo "{{.template_use_block}}"
    
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 -> c0039
                 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/c0039
    module: [self], instance id: [dev], exec profile: []
    profile -  envVars:
    
    (*core.Cache)({
    })
    
    dvar> usage_of_with:
    "  student name: Tom\n  school name: Sydney Grammar\n  Male\n  "
    
    -
      student name: Tom
      school name: Sydney Grammar
      Male
      
    dvar> usage_of_with_else:
    "  i am somebody  "
    
    -
      i am somebody  
    dvar> template_def_plain:
    "\n\n\nONE TWO\"\n"
    
    -
    
    
    
    ONE TWO"
    
    dvar> template_def_consume:
    "\nshow example of how you can define reusable template\nand how you can pass in the parameters\n\n\n\n\nstudent:\n  name: ONE: Tom\n  gender: TWO: Male\n"
    
    -
    
    show example of how you can define reusable template
    and how you can pass in the parameters
    
    
    
    
    student:
      name: ONE: Tom
      gender: TWO: Male
    
          template rendering -> template: .:4:11: executing "." at <{{template "T3" .student}}>: template "T3" not defined
    WARN:
        1:{{.template_def_consume_dynamic_t1}}
        2:{{.template_def_consume_dynamic_t2}}
        3:{{.template_def_consume_dynamic_t3}}
        4:{{template "T3" .student}}
        5:
        6:
    
    trouble shooting tips:
    <incompatible types for comparison>: the variable might not be registered, use -v vvv to see the cache, or use inspect cmd to debug
    
    dvar> template_def_consume_dynamic:
    "\n\n\n\nstudent:\n  name: ONE: Tom\n  gender: TWO: Male\n\n"
    
    -
    
    
    
    
    student:
      name: ONE: Tom
      gender: TWO: Male
    
    
    dvar> template_use_block:
    "\n\nstudent:\n  name: ONE: Tom\ngender: TWO: Male\n\n"
    
    -
    
    
    student:
      name: ONE: Tom
    gender: TWO: Male
    
    
    Task1: [task ==> task:  ]
    -Step1:
    self: final context exec vars:
    
    (*core.Cache)({
      "up_runtime_task_layer_number": 0,
      "age": 34,
      "template_def_consume": "\nshow example of how you can define reusable template\nand how you can pass in the parameters\n\n\n\n\nstudent:\n  name: ONE: Tom\n  gender: TWO: Male\n",
      "ns": "prod",
      "usage_of_with": "  student name: Tom\n  school name: Sydney Grammar\n  Male\n  ",
      "template_use_block": "\n\nstudent:\n  name: ONE: Tom\ngender: TWO: Male\n\n",
      "usage_of_with_else": "  i am somebody  ",
      "template_def_consume_dynamic_main": "{{.template_def_consume_dynamic_t1}}\n{{.template_def_consume_dynamic_t2}}\n{{.template_def_consume_dynamic_t3}}\n{{template \"T3\" .student}}\n",
      "template_def_consume_dynamic_t3": "{{define \"T3\"}}student:\n  name: {{template \"T1\" .name}}\n  gender: {{template \"T2\" .gender}}{{end}}\n{{template \"T3\" .student}}\n",
      "ha": true,
      "nobody": <nil>,
      "student": {
        "address": {
          "suburb": {
            "cbd": true,
            "name": "sydney",
            "postcode": 2000
          },
          "school": "Sydney Grammar"
        },
        "name": "Tom",
        "gender": "Male",
        "teachers": {
          "tom",
          "jason",
          "alice"
        }
      },
      "pod_name": "web_app",
      "managers": {
        "tom",
        "jason",
        "alice"
      },
      "template_def_consume_dynamic_t2": "{{define \"T2\"}}TWO: {{.}}{{end}}",
      "template_def_plain": "\n\n\nONE TWO\"\n",
      "template_def_consume_dynamic": "\n\n\n\nstudent:\n  name: ONE: Tom\n  gender: TWO: Male\n\n",
      "admins": {
        "tom",
        "jason",
        "alice"
      },
      "old": 54,
      "template_def_consume_dynamic_t1": "{{define \"T1\"}}ONE: {{.}}{{end}}\n"
    })
    
    cmd( 1):
    echo "value\n {{.usage_of_with}}"
    
    -
    value\n   student name: Tom
      school name: Sydney Grammar
      Male
      
    
    -
     .. ok
    cmd( 2):
    echo "{{.usage_of_template}}"
    
    -
    <no value>
    
    -
     .. ok
    cmd( 3):
    echo "{{.template_def_consume}}"
    
    -
    
    show example of how you can define reusable template
    and how you can pass in the parameters
    
    
    
    
    student:
      name: ONE: Tom
      gender: TWO: Male
    
    
    -
     .. ok
    cmd( 4):
    echo "{{.template_def_consume_dynamic_t1}}"
    
    -
    {{define T1}}ONE: {{.}}{{end}}
    
    
    -
     .. ok
    cmd( 5):
    echo "{{.template_def_consume_dynamic_t2}}"
    
    -
    {{define T2}}TWO: {{.}}{{end}}
    
    -
     .. ok
    cmd( 6):
    echo "{{.template_def_consume_dynamic_t3}}"
    
    -
    {{define T3}}student:
      name: {{template T1 .name}}
      gender: {{template T2 .gender}}{{end}}
    {{template T3 .student}}
    
    
    -
     .. ok
    cmd( 7):
    echo "{{.template_def_consume_dynamic}}"
    
    -
    
    
    
    
    student:
      name: ONE: Tom
      gender: TWO: Male
    
    
    
    -
     .. ok
    cmd( 8):
    echo "{{.template_use_block}}"
    
    -
    
    
    student:
      name: ONE: Tom
    gender: TWO: Male
    
    
    
    -
     .. ok
    . ok
    
Logs with different verbose level
Raw logs with different verbose level