kcmerrill / alfred

(v0.2) Even Batman needs a little help. Task runner. Automator. Build system.
MIT License
63 stars 14 forks source link

Integrating environment variables #38

Closed moritzheiber closed 6 years ago

moritzheiber commented 7 years ago

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:

export.var:
  summary: Export var into Alfred context
  register: my.var
  command: |
      echo "${VAR}"

test.var:
  summary: Tests whether VAR is empty
  test: test -n "{{ index .Vars "my.var" }}"
  fail: set.default.var

set.default.var:
  register: my.var
  command: |
    echo "My default"

It's more than a little cumbersome, as you might realize.

Something along these lines would help:

my.task:
  env:
    VAR: "My default"
  command: |
    echo "${VAR}"

or

my.task:
  env:
    - VAR
  command: |
    echo "${VAR}"

and fail if ${VAR} isn't set.

I could even think you could run other Alfred tasks to fill empty variables:

my.var:
  register: my.variable
  command: |
    echo "derp!"

my.task:
  env:
    VAR: {{ index .Vars "my.variable" }}
  command: |
    echo "${VAR}"

What do you think?

kcmerrill commented 7 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

kcmerrill commented 6 years ago

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" }}"
moritzheiber commented 6 years ago

@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?

kcmerrill commented 6 years ago

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 ]