David
David

Reputation: 1770

Maintain YAML indentation while printing the contents of a file into that YAML (using bash scripting)

I need to print the contents of a python file into a YAML file, without the indentation of the YAML file getting messed up. The part of the YAML file that's doing this is a description of the commands that will get passed into a Bash script, so we need to accomplish this with Bash scripting syntax. For example, the following hard-coded code WORKS. Notice how the "cat" line is outputting the conents of script.py with the appropriate indentation:

enter image description here

The trick is getting it into that format when trying to read the Python from another file. In fact, in order to create that screenshot, I had to actually go into the file after it was generated and add a bunch of spaces so it looks correct. After I added the spaces, the file worked as intended. Here's what the incorrect output looks like BEFORE I manually edit it:

enter image description here

Interestingly, the first line of the python (green arrow) starts on the correct line. But the remaining lines are all along the left margin and is incorrect (red arrow).

Here's the code I'm using to get my code (in script.py) into the buildspec:

Note: ${Script} is a reference to my script.py file. This variable is getting set elsewhere in the file.

version: 0.2
phases:
  install:
    commands:
      - apt-get update -y
  build:
    commands:
      - git config --global credential.helper '!aws codecommit credential-helper $@'
      - git config --global credential.UseHttpPath true
      - |
        cat >> scrypt.py <<EOL
        ${Script}
        EOL

Upvotes: 2

Views: 1719

Answers (1)

David
David

Reputation: 1770

So the problem was that the !Include function that was putting the contents of script.py in place of ${Script} didn't care that we were in an YAML file, so whenever there was a new line, it just throw them all the way to the left.

There were two ways to solve this. First, we could simply add spaces to script.py itself, so when its value gets substituted into ${Script}, they land in the right place.

What we ended up doing through (since the first way was pretty hacky and hard to maintain) was to simply give up on trying to !Include the file, and intead just literally paste the entire script.py contents into the YAML file, exactly where ${Script} originally was. So the result looked like this:

version: 0.2
phases:
  install:
    commands:
      - apt-get update -y
  build:
    commands:
      - git config --global credential.helper '!aws codecommit credential-helper $@'
      - git config --global credential.UseHttpPath true
      - pip3 install --quiet boto3
      - pip3 install --quiet GitPython
      - |
        cat > script.py <<EOL
        import boto3
        import git
        etc. etc. etc.
        EOL
      - python script.py

Upvotes: 1

Related Questions