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

     1  doc_meta: |
     2    folder: templating
     3    title: datakey and datapath
     4    head: |
     5      By default you just implicitly use the global runtime vars as datasource to render a template file.
     6  
     7      This shows that you can use datakey and datapath to reference to a varname and a sub element of a var in global runtime scope
     8  
     9    sections:
    10      - title: How to use
    11        content: |
    12          * the datakey would be a plain text of a var name, or a dynamic templating value to pointing to a var name
    13  
    14          * datapath:
    15            format is like a.b.c(name=fr*).value, following the yq path format:
    16  
    17            1. 'a.b.c'
    18            2. 'a.*.c'
    19            3. 'a.**.c'
    20            4. 'a.(child.subchild==co*).c'
    21            5. 'a.array[0].blah'
    22            6. 'a.array[*].blah'
    23  
    24      - title: Relavant
    25        content: render using dvar and datakey
    26        refs:
    27          - title: use dvar to render
    28            link: ../../dvars/c0082
    29  
    30      - title: query and yq datapath
    31        content: query details
    32        refs:
    33          - title: query object
    34            link: ../../object-oriented/c0100
    35  
    36      - title: Demo
    37        log: yes
    38  
    39    related:
    40      refs:
    41        - title: default golang template reference
    42          link: https://github.com/mikefarah/yq
    43  
    44  notes:
    45    goal:
    46      - add featuer to template cmd to support sub element
    47      - path format is like a.b.c(name=fr*).value
    48      - prefix will be used to get the obj from cache, the rest will be used as yq path
    49  
    50  vars:
    51    school_details_object:
    52      school:
    53        name: "sydney grammar"
    54        address: "1 fox road, sydney, nsw 2000"
    55      principals:
    56        - "peter"
    57        - "tom"
    58        - "jane"
    59      ranking: "No 5"
    60    root:
    61      parent:
    62        school_details_object:
    63          school:
    64            name: "sydney grammar"
    65            address: "1 fox road, sydney, nsw 2000"
    66          principals:
    67            - "peter"
    68            - "tom"
    69            - "jane"
    70          ranking: "No 5"
    71  
    72  tasks:
    73    -
    74      name: task
    75      task:
    76        -
    77          func: cmd
    78          do:
    79            - name: template
    80              desc: render the template file using above dynamic variable from defined var
    81              cmd:
    82                src: './tests/functests/d0079.template'
    83                dest: /tmp/myschool.txt
    84                datakey: "school_details_object"
    85  
    86        - func: cmd
    87          do:
    88            - name: readFile
    89              desc: read content of a file and register it to a var
    90              cmd:
    91                filename: myschool.txt
    92                dir: /tmp
    93                reg: my_school
    94  
    95            -
    96              name: print
    97              cmd: "{{.my_school}}"
    98  
    99        -
   100          func: cmd
   101          do:
   102            - name: template
   103              desc: render the template file using above dynamic variable from defined var
   104              cmd:
   105                src: './tests/functests/d0079.template'
   106                dest: /tmp/myschool.txt
   107                datapath: "root.parent.school_details_object"
   108  
   109        - func: cmd
   110          do:
   111            - name: readFile
   112              desc: read content of a file and register it to a var
   113              cmd:
   114                filename: myschool.txt
   115                dir: /tmp
   116                reg: my_school
   117  
   118            -
   119              name: print
   120              cmd: "{{.my_school}}"