Closed moritzheiber closed 6 years ago
I think Alfred could use a way to interact with env variables for sure.
Let me chew on this. I'm on holiday but can probably come up with something on the 5th
Sorry for the delay.
So a while back I added sprig
and it allows the default values for map[string]interface{}
which means, using your example, you can do this:
my.task:
command: |
echo "{{ default "derp!" | index .Vars "my.variable" }}"
@kcmerrill Sorry for bothering you about this again .. but would you have a working example for reading environment variables?
I tried:
alfred.vars:
tag: {{ env "SOMETHING" | trim }}
or
alfred.vars:
tag: {{ default "bla!" | env "SOMETHING" | trim }}
both of which just give me
$ alfred
[ERROR] yaml: unmarshal errors:
line 4: cannot unmarshal !!map into string
$
The sprig documentation isn't all that verbose, and now I'm short of digging into the code. Any clue?
A couple quick comments.
The error you're getting is because that is not valid yaml. Basically, you cannot start strings with "{{" without enclosing them in double quotes.
So this is how you could get rid of the error
alfred.vars:
tag: "{{ default "blah!" | env "SOMETHING" | trim }}"
or better yet, how I personally do complex strings in YAML
alfred.vars:
tag: |
{{ default "blah!" | env "SOMETHING" | trim }}
So that will get rid of your YAML compilation error.
Having said that though, I don't think that will quite do what you think that it should do.
Admittedly alfred doesn't handle registration and the templating system as well as I'd like. I'm fixing this in the latest version of alfred, among many other shortcomings it currently has.
What I'd recommend, and it's not the sexiest solution, but do something like this instead:
04:42 PM ✔ kcmerrill tmp ] alfred env
[env] Register an env (Args: [])
default here
---
✔ env DONE
04:42 PM ✔ kcmerrill tmp ] cat alfred.yml
env:
summary: Env defaults
command: |
echo {{ if env "doesnotexist" }} $doesnotexist {{ else }} "default here" {{ end }}
04:43 PM ✔ kcmerrill tmp ]
It would be splendid for Alfred to have a notion of environment variables, and whether they are set or not.
Our code deals with environment variables a lot, and currently it would mean running the following:
It's more than a little cumbersome, as you might realize.
Something along these lines would help:
or
and fail if
${VAR}
isn't set.I could even think you could run other Alfred tasks to fill empty variables:
What do you think?