use template cmd

Showcase use template cmd to template

my story text file reference

      My name is {{.name}}, my school is {{.school}}
      Once when I was {{.age}} years old
      In the book it said: {{.said}}
      I pondered deeply, then, over the adventures of the jungle.
    

How to use?

By default, all the vars from within the template will be substituted from the vars

It also demos that the rendered result could be a dynamic variable which pointing to a filename

Demo a use env var as input to determine working dir

This demos that if GOTEST env variable is set, then load the file from testing directory or root dir

Demo

source

Main task yaml file
    vars:
      name: little prince
      school: sydney grammar
      age: 12
      said: Boa constrictors swallow their prey whole
      save_to: /tmp/mystory2.txt
      read_dir: /tmp
      read_file: mystory2.txt
    dvars:
    - name: intest
      value: '{{ env "GOTEST" |default "false" }}'
    tasks:
    - name: task
      task:
      - func: shell
        do:
        - echo "?intest ->  {{.intest}}"
        - pwd
        reg: register_task_root
      - func: cmd
        do:
        - name: print
          cmd: 'intest is: [{{.intest}}]'
        - name: reg
          cmd:
            name: correct_working_dir
            desc: the value of .intest is string but not bool so you can not simply use
              if .intest for condition
            value: >-
              {{if eq .intest "true" }}{{.register_task_root.Output}}{{else}}{{.register_task_root.Output}}{{end}}
      - func: cmd
        do:
        - name: print
          cmd: 'root dir is: [{{.register_task_root.Output}}]'
        - name: print
          cmd: 'correct working dir is: [{{.correct_working_dir}}]'
        - name: template
          desc: render a template file to a file 1
          cmd:
            src: '{{.correct_working_dir}}/tests/functests/d0072.template'
            dest: /tmp/mystory.txt
        - name: readFile
          desc: read content of a file and register it to a var
          cmd:
            filename: mystory.txt
            dir: /tmp
            reg: my_interesting_story
        - name: print
          cmd: "{{.my_interesting_story}}"
      - func: cmd
        do:
        - name: template
          desc: render a template file to a file 2
          cmd:
            src: '{{.correct_working_dir}}/tests/functests/d0072.template'
            dest: '{{.save_to}}'
        - name: readFile
          desc: read content of a file and register it to a var
          cmd:
            filename: '{{.read_file}}'
            dir: '{{.read_dir}}'
            reg: my_interesting_story2
        - name: print
          cmd: "{{.my_interesting_story2}}"
    
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 -> c0072
                 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/c0072
    module: [self], instance id: [dev], exec profile: []
    profile -  envVars:
    
    (*core.Cache)({
    })
    
    Task1: [task ==> task:  ]
    -Step1:
    self: final context exec vars:
    
    (*core.Cache)({
      "read_file": "mystory2.txt",
      "school": "sydney grammar",
      "said": "Boa constrictors swallow their prey whole",
      "up_runtime_task_layer_number": 0,
      "name": "little prince",
      "age": 12,
      "read_dir": "/tmp",
      "save_to": "/tmp/mystory2.txt",
      "intest": "false"
    })
    
    cmd( 1):
    echo "?intest ->  {{.intest}}"
    
    -
    ?intest ->  false
    
    -
     .. ok
    cmd( 2):
    pwd
    
    -
    /up_project/up
    
    -
     .. ok
    . ok
    -Step2:
    self: final context exec vars:
    
    (*core.Cache)({
      "register_task_root": (*utils.ExecResult)({
        Cmd: "pwd",
        Code: 0,
        Output: "/up_project/up",
        ErrMsg: ""
      }),
      "read_file": "mystory2.txt",
      "school": "sydney grammar",
      "name": "little prince",
      "intest": "false",
      "read_dir": "/tmp",
      "last_result": (*utils.ExecResult)({
        Cmd: "pwd",
        Code: 0,
        Output: "/up_project/up",
        ErrMsg: ""
      }),
      "up_runtime_task_layer_number": 0,
      "age": 12,
      "said": "Boa constrictors swallow their prey whole",
      "save_to": "/tmp/mystory2.txt"
    })
    
    ~SubStep1: [print:  ]
    intest is: [false]
    ~SubStep2: [reg:  ]
    -Step3:
    self: final context exec vars:
    
    (*core.Cache)({
      "last_result": (*utils.ExecResult)({
        Cmd: "pwd",
        Code: 0,
        Output: "/up_project/up",
        ErrMsg: ""
      }),
      "correct_working_dir": "/up_project/up",
      "save_to": "/tmp/mystory2.txt",
      "read_dir": "/tmp",
      "read_file": "mystory2.txt",
      "said": "Boa constrictors swallow their prey whole",
      "intest": "false",
      "name": "little prince",
      "register_task_root": (*utils.ExecResult)({
        Cmd: "pwd",
        Code: 0,
        Output: "/up_project/up",
        ErrMsg: ""
      }),
      "school": "sydney grammar",
      "age": 12,
      "up_runtime_task_layer_number": 0
    })
    
    ~SubStep1: [print:  ]
    root dir is: [/up_project/up]
    ~SubStep2: [print:  ]
    correct working dir is: [/up_project/up]
    ~SubStep3: [template: render a template file to a file 1 ]
    ~SubStep4: [readFile: read content of a file and register it to a var ]
    ~SubStep5: [print:  ]
      My name is little prince, my school is sydney grammar
      Once when I was 12 years old
      In the book it said: Boa constrictors swallow their prey whole
      I pondered deeply, then, over the adventures of the jungle.
    
    -Step4:
    self: final context exec vars:
    
    (*core.Cache)({
      "read_dir": "/tmp",
      "intest": "false",
      "my_interesting_story": "  My name is little prince, my school is sydney grammar\n  Once when I was 12 years old\n  In the book it said: Boa constrictors swallow their prey whole\n  I pondered deeply, then, over the adventures of the jungle.\n",
      "last_result": (*utils.ExecResult)({
        Cmd: "pwd",
        Code: 0,
        Output: "/up_project/up",
        ErrMsg: ""
      }),
      "correct_working_dir": "/up_project/up",
      "name": "little prince",
      "save_to": "/tmp/mystory2.txt",
      "read_file": "mystory2.txt",
      "register_task_root": (*utils.ExecResult)({
        Cmd: "pwd",
        Code: 0,
        Output: "/up_project/up",
        ErrMsg: ""
      }),
      "said": "Boa constrictors swallow their prey whole",
      "school": "sydney grammar",
      "up_runtime_task_layer_number": 0,
      "age": 12
    })
    
    ~SubStep1: [template: render a template file to a file 2 ]
    ~SubStep2: [readFile: read content of a file and register it to a var ]
    ~SubStep3: [print:  ]
      My name is little prince, my school is sydney grammar
      Once when I was 12 years old
      In the book it said: Boa constrictors swallow their prey whole
      I pondered deeply, then, over the adventures of the jungle.
    
    
Logs with different verbose level
Raw logs with different verbose level