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

Give script templates access to their own name and other metadata #137

Closed
anthonyrisinger opened this issue Jan 6, 2018 · 5 comments
Closed

Comments

@anthonyrisinger
Copy link
Contributor

This is a duplicate of coryb/kingpeon#2, but it's replicated here in the event it can be solved faster/better within go-jira.

You can access options and args via function (kingpeon.go#L68) but there is no way for a script template to discover the command that triggered it, or other useful information.

I'm trying to make a common entry point like so:

custom-commands:
  - name: sync
    options: ...
    args: ...
    script: |
      JIRA={{jira}} exec {{jira}}-lib sync 10<<'EOF'
      {{toJson options}}
      {{toJson args}}
      EOF

The script is the same for every custom command except for the word sync (or whatever command is in play) because I don't see a way to get that info, eg. {{.name}}.

Could we assign . to the config so things like .name or .help work? Or something else?

If there was a toJson that returned compact JSON, it would help avoid the quoted heredoc nonsense above, which I'm using as a safe way to pass the prettified, unquoted JSON. Lastly, a shellquote function would be massively appreciated! Combined with compact JSON, one could then safely:

/path/to/command {{options | toJson | shellquote}} {{args | toJson | shellquote}}

(It might also be useful to expand options to --opt=abc -ddd ... or whatever, but I don't need that myself, and prefer to work with the JSON blob version of options and args)

Shell quoting is pretty straightforward; surround text with ' and replace single-quotes with '\''.

@coryb
Copy link
Contributor

coryb commented Jan 7, 2018

I think adding a {{name}} param makes sense. For the shellquote, I think you can use something like:

{{options | toJson | printf "%q"}}

That will correctly double-quote the input string, for example:

custom-commands:
 - name: test-quote
   options:
     - name: foo
       default: deffoo
     - name: bar
       default: defbar
   script: |-
     cat <<'EOF'
     {{options | toJson | printf "%q"}}
     EOF
$ jira test-quote
"{\n    \"bar\": \"defbar\",\n    \"foo\": \"deffoo\"\n}"

@coryb
Copy link
Contributor

coryb commented Jan 7, 2018

A trivial change in kingpeon will allow for you to access the calling command properties, like this:

custom-commands:
  - name: test-command-properties
    help: "i'm helpful"
    options:
      - name: foo
        default: deffoo
      - name: bar
        default: defbar
    script: |-
      cat <<'EOF'
      {{. | toJson}}
      EOF
      echo "Name: {{.Name}}"
      echo "Help: {{.Help}}"
$ ./jira test-command-properties
{
    "name": "test-command-properties",
    "options": [
        {
            "name": "foo",
            "default": "deffoo"
        },
        {
            "name": "bar",
            "default": "defbar"
        }
    ],
    "script": "cat \u003c\u003c'EOF'\n{{. | toJson}}\nEOF\necho \"Name: {{.Name}}\"\necho \"Help: {{.Help}}\"",
    "help": "i'm helpful"
}
Name: test-command-properties
Help: i'm helpful

So I think I will get that change in.

@anthonyrisinger
Copy link
Contributor Author

This looks great, another job well done.

@anthonyrisinger
Copy link
Contributor Author

Also I didn't know about the {{... | printf "%q"}}, thanks!

@anthonyrisinger
Copy link
Contributor Author

@coryb I tried using printf "%q" but sadly it doesn't quite close the gap I don't think, see #139.

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