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

     1  doc_meta: |
     2    folder: loop
     3    title: loop usage guide
     4    weight: 1400
     5    head: |
     6      Showcase use loop in UP cmd cli
     7  
     8    sections:
     9      - title: How to use the loop? The simple way
    10        content: |
    11          To put it in simple way, you can loop a list of values simply attaching to loop in func call
    12  
    13          ```
    14          - func: shell
    15            do:
    16              - echo "hello {{.loopitem}}"
    17            loop:
    18              - tom
    19              - peter
    20              - james
    21  
    22          - func: shell
    23            do:
    24              - echo "name {{.loopitem.name}}"
    25              - echo "age {{.loopitem.age}}"
    26            loop:
    27              - name: tom
    28                age: 11
    29              - name: peter
    30                age: 45
    31              - name: james
    32                age: 23
    33          ```
    34  
    35      - title: Advanced usage
    36        content: |
    37          * You can use a name reference for the loop elememnt, the reference is a var/dvar, then it is up to you to load it dynamically, or externalizing the value from file
    38          * You can also use a dynamic value referencing to a var/dvar, this gives you the ability to controll the workflow dynamically
    39  
    40          Pleas refer to the usage of .countries
    41  
    42      - title: Demo
    43        log: yes
    44  
    45  goal:
    46    - add loop support
    47  
    48  design: |
    49      loop takes either a list directly
    50      or it takes a static var/dvar name to reference to
    51      or a templated string to be evaluated and eventually
    52      pointing to a var/dvar name
    53  
    54  consideration: |
    55    do we need the while condition to stop the loop
    56    maybe not
    57    the reason is that you can always use dvar + cmd func
    58     to prepare the list for iteration
    59  
    60  vars:
    61    countries:
    62      - name: Australia
    63        population: 20m
    64  
    65      - name: British
    66        population: 2000m
    67  
    68      - name: China
    69        population: 1.4b
    70  
    71      - name: Danmark
    72        population: 30m
    73  
    74  tasks:
    75    -
    76      name: task
    77      task:
    78  
    79        - func: shell
    80          do:
    81            - echo "hello {{.loopitem}}"
    82          loop:
    83            - tom
    84            - peter
    85            - james
    86  
    87        - func: shell
    88          do:
    89            - echo "name {{.loopitem.name}}"
    90            - echo "age {{.loopitem.age}}"
    91          loop:
    92            - name: tom
    93              age: 11
    94            - name: peter
    95              age: 45
    96            - name: james
    97              age: 23
    98  
    99        - func: shell
   100          do:
   101            - echo "hello {{.loopitem}}"
   102          loop: [Australia, British, China, Danmark]
   103  
   104        - func: shell
   105          desc: |
   106            the loop point to a iteratable var countries
   107          do:
   108            - echo "hello {{.loopitem.name}}"
   109            - echo "hello {{.loopitem.population}}"
   110          loop: "countries"
   111  
   112        - func: shell
   113          desc: |
   114            the templated value will be eventually a var/dvar name
   115          dvars:
   116            - name: listname
   117              value: "countries"
   118          do:
   119            - echo "hello {{.loopindex}}"
   120            - echo "hello {{.loopindex1}}"
   121            - echo "hello {{.loopitem.name}}"
   122            - echo "hello {{.loopitem.population}}"
   123          loop: "{{.listname}}"