github.com/upcmd/up@v0.8.1-0.20230108151705-ad8b797bf04f/tests/functests/c0094.yml (about)

     1  doc_meta: |
     2    folder: design-patterns
     3    title: private var scope
     4    head: |
     5      A important feature of call func is to segregate a group of vars, including local vars and all inheriting vars in callee from other func's local vars
     6  
     7      The key to use this patter is to use multiple layers of call hierarchical structure, for example, main task, the routing layer func, the implmentation layer tasks. You will put all local vars to a call func, so that the global vars scope will not be polluted, changes will be made and applied within the layer of called implementation only
     8  
     9    sections:
    10      - title: Demo
    11        log: yes
    12  
    13  
    14  notes:
    15    goal:
    16      - show call func can be used to segregate a var scope due to its nature
    17      - |
    18        because all called tasks inerit the vars/dvars from calling func, so
    19        if you want to have a bound scope of vars/dvars, you can define the
    20        vars/dvars in calling func and they will be availabe in all funcs in
    21        called funcs within the task
    22      - |
    23        this example shows that defined vars layer1_aaa, layer1_bbb are available
    24        in both funcss of task layer2
    25      - |
    26        it is powerful to wrap up reusable code into a task which sharing the same
    27        set of vars/dvars
    28  
    29  tasks:
    30  
    31    - name: task
    32      task:
    33  
    34        - func: call
    35          vars:
    36            layer1_aaa: layer1_aaa
    37            layer1_bbb: layer1_bbb
    38  
    39          do:
    40            - layer2
    41          loop:
    42            - layer1-tom
    43            - layer1-peter
    44            - layer1-james
    45  
    46    - name: layer2
    47      task:
    48        - func: cmd
    49          do:
    50            -
    51              name: print
    52              cmd: 'hello {{.loopitem}}'
    53  
    54            -
    55              name: print
    56              cmd: 'hello layer1_aaa: {{.layer1_aaa}}'
    57  
    58        - func: cmd
    59          do:
    60            -
    61              name: print
    62              cmd: 'hello {{.loopitem}}'
    63  
    64            -
    65              name: print
    66              cmd: 'hello layer1_bbb: {{.layer1_bbb}}'