Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shellquote function for escaping text in custom scripts #139

Closed
anthonyrisinger opened this issue Jan 7, 2018 · 2 comments
Closed

shellquote function for escaping text in custom scripts #139

anthonyrisinger opened this issue Jan 7, 2018 · 2 comments

Comments

@anthonyrisinger
Copy link
Contributor

{{ ... | printf "%q"}} does not properly quote output for shell, so it's not very useful in the script: of custom commands. regReplace seems to work, but you have to remember to also add extra single quotes yourself else you get strange shell errors.

Maybe it's worth exposing a dedicated shellquote (or similar) function?

    script: |
      cat <<'EOF'
      #### EXPECTED
      {{toJson args}}
      EOF
      echo '#### REGREPLACE
      ''{{toJson args | regReplace "'" "'\\''"}}'
      echo '#### PRINTF (unquoted)
      '{{toJson args | printf "%q"}}
      echo '#### PRINTF (quoted)
      '"{{toJson args | printf "%q"}}"

Ouput:

$ bin/jira sync '$HOME.$$.sprint'\''t.yml{,2}'
#### EXPECTED
{
    "DOC": [
        "$HOME.$$.sprint't.yml",
        "$HOME.$$.sprint't.yml2"
    ]
}
#### REGREPLACE
{
    "DOC": [
        "$HOME.$$.sprint't.yml",
        "$HOME.$$.sprint't.yml2"
    ]
}
#### PRINTF (unquoted)
{
    "DOC": [
        "/Users/anthony.74569.sprint't.yml",
        "/Users/anthony.74569.sprint't.yml2"
    ]
}
#### PRINTF (quoted)
{n "DOC": [n "/Users/anthony.74569.sprintt.yml\",
        \"$HOME.$$.sprintt.yml2"n ]n}

It's hard to notice, but the printf variant actually only has one item in the list :-)

@coryb
Copy link
Contributor

coryb commented Jan 7, 2018

Okay, just added a toMinJson function to remove the pretty-print, also added shellquote function that takes a string and quotes it.

So given this example:

custom-commands:
  - name: test-quote
    args:
      - name: DOC
        repeat: true
    script: |
      cat <<'EOF'
      #### EXPECTED
      {{args | toMinJson}}
      EOF

      cat <<'EOF'
      #### SHELLQUOTE
      {{args | toMinJson | shellquote}}
      EOF

      echo "#### ECHO RAW"
      echo {{args | toMinJson}}

      echo "#### ECHO SHELLQUOTE"
      echo {{args | toMinJson | shellquote}}

This is the output I get:

$ ./jira test-quote '$HOME.$$.sprint'\''t.yml' '$HOME.$$.sprint'\''t.yml2'
#### EXPECTED
{"DOC":["$HOME.$$.sprint't.yml","$HOME.$$.sprint't.yml2"]}
#### SHELLQUOTE
\{\"DOC\":\[\"\$HOME.\$\$.sprint\'t.yml\",\"\$HOME.\$\$.sprint\'t.yml2\"]}
#### ECHO RAW
DOC:[/Users/cbennett.32161.sprint't.yml /Users/cbennett.32161.sprint't.yml2]
#### ECHO SHELLQUOTE
{"DOC":["$HOME.$$.sprint't.yml","$HOME.$$.sprint't.yml2"]}

@anthonyrisinger
Copy link
Contributor Author

You sir, are a saint and a scholar. Many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants