pyinfra-dev / pyinfra

pyinfra turns Python code into shell commands and runs them on your servers. Execute ad-hoc commands and write declarative operations. Target SSH servers, local machine and Docker containers. Fast and scales from one server to thousands.
https://pyinfra.com
MIT License
3.91k stars 382 forks source link

`files.block` duplicates end marker when overwriting existing block #1031

Closed stone-w4tch3r closed 10 months ago

stone-w4tch3r commented 12 months ago

Describe the bug

If file with marked block already exists, but content is not what expected, files.block will append redundant marker line after block

To Reproduce

  1. On remote:
    touch file && echo "hello
    #### BEGIN BLOCK ####
    ---
    #### END BLOCK ####
    bye" > file
  2. Run main.py:
    
    from pyinfra.operations import files

files.block( path="/home/ubuntu/file", content="blah blah", marker="#### {mark} BLOCK ####", )

3. See result on remote:
NOTE:  due to #1028 `file.block` adds a `$`, but even after fixing this behavior issue with redundant marker persists
```bash
cat file
hello
#### BEGIN BLOCK ####
$blah blah
#### END BLOCK ####
#### END BLOCK ####
bye

Expected behavior

No duplication after running operation

Meta

stone-w4tch3r commented 12 months ago

Issue is in final awk code: awk 'BEGIN {{f=1; x=ARGV[2]; ARGV[2]=""}}/#### BEGIN BLOCK ####/ {print; print x; f=0} /#### END BLOCK ####/ {print; f=1} f' ...