delete in yml file

yml_delte cmd allow you to delete key from a yml file.

How to use

  • ymlfile: the yml filename
  • refdir: optional directory of the ymlfile located, default to the global refdir from command line, or global configured
  • path: path is the yq path
  • reg: the var name to register to
  • flags:
    • localOnly: to indicate that the registered var is in local func var scope
    • inpace: the delete operation applied directly to file, otherwise the change will be made to the registered var only

Note that inplace and reg/localOnly are mutual exclusive

Demo

source

Main task yaml file
    vars:
      mock_yml: |
        tom:
          sex: male
          age: 23
        jason:
          sex: male
          age: 35
        emily:
          sex: female
          age: 32
    tasks:
    - name: task
      task:
      - func: shell
        do:
        - 'echo "{{.mock_yml}}" > /tmp/mock_yml.yml'
      - func: cmd
        desc: inplace modification
        do:
        - name: ymlDelete
          cmd:
            ymlfile: mock_yml.yml
            refdir: /tmp
            path: jason.sex
            verbose: v
          flags:
          - inplace
        - name: readFile
          desc: check new file content
          cmd:
            filename: mock_yml.yml
            dir: /tmp
            reg: new_yml
        - name: print
          desc: show the modified yml content read from file
          cmd: '{{.new_yml}}'
      - func: cmd
        desc: modify in memory and register to cache
        do:
        - name: ymlDelete
          cmd:
            ymlfile: mock_yml.yml
            refdir: /tmp
            path: jason.sex
            verbose: vvvv
            reg: modified_yml
          flags: [localOnly]
        - name: print
          desc: show the modified yml content registered
          cmd: '{{.modified_yml}}'
    
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 -> c0102
                 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/c0102
    module: [self], instance id: [dev], exec profile: []
    profile -  envVars:
    
    (*core.Cache)({
    })
    
    Task1: [task ==> task:  ]
    -Step1:
    self: final context exec vars:
    
    (*core.Cache)({
      "mock_yml": "tom:\n  sex: male\n  age: 23\njason:\n  sex: male\n  age: 35\nemily:\n  sex: female\n  age: 32\n",
      "up_runtime_task_layer_number": 0
    })
    
    cmd( 1):
    echo "{{.mock_yml}}" > /tmp/mock_yml.yml
    
    -
    
    -
     .. ok
    . ok
    -Step2: [: inplace modification ]
    self: final context exec vars:
    
    (*core.Cache)({
      "mock_yml": "tom:\n  sex: male\n  age: 23\njason:\n  sex: male\n  age: 35\nemily:\n  sex: female\n  age: 32\n",
      "last_result": (*utils.ExecResult)({
        Cmd: "echo \"tom:\n  sex: male\n  age: 23\njason:\n  sex: male\n  age: 35\nemily:\n  sex: female\n  age: 32\n\" > /tmp/mock_yml.yml",
        Code: 0,
        Output: "",
        ErrMsg: ""
      }),
      "up_runtime_task_layer_number": 0
    })
    
    ~SubStep1: [ymlDelete:  ]
    ~SubStep2: [readFile: check new file content ]
    ~SubStep3: [print: show the modified yml content read from file ]
    tom:
      sex: male
      age: 23
    jason:
      age: 35
    emily:
      sex: female
      age: 32
    
    -Step3: [: modify in memory and register to cache ]
    self: final context exec vars:
    
    (*core.Cache)({
      "mock_yml": "tom:\n  sex: male\n  age: 23\njason:\n  sex: male\n  age: 35\nemily:\n  sex: female\n  age: 32\n",
      "last_result": (*utils.ExecResult)({
        Cmd: "echo \"tom:\n  sex: male\n  age: 23\njason:\n  sex: male\n  age: 35\nemily:\n  sex: female\n  age: 32\n\" > /tmp/mock_yml.yml",
        Code: 0,
        Output: "",
        ErrMsg: ""
      }),
      "new_yml": "tom:\n  sex: male\n  age: 23\njason:\n  age: 35\nemily:\n  sex: female\n  age: 32\n",
      "up_runtime_task_layer_number": 0
    })
    
    ~SubStep1: [ymlDelete:  ]
    ~SubStep2: [print: show the modified yml content registered ]
    tom:
      sex: male
      age: 23
    jason:
      age: 35
    emily:
      sex: female
      age: 32
    
    
Logs with different verbose level
Raw logs with different verbose level

Other references: