elves / elvish

Powerful scripting language & versatile interactive shell

Home Page:https://elv.sh/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pipe in elvish alias?

zQueal opened this issue Β· comments

I'm attempting to create an alias with a pipe. I have a 2fa cli application that I use frequently and would like to create an alias for it.

The application is 2fa and I'd like to create an alias for 2f. When called directly 2fa outputs a list of TOTP codes. I like to pipe them to fzf, choose one, and add it to the clipboard. Makes using MFA a little less painful.

#alias:new 2f 2fa
edit:add-var 2f~ { (2fa | fzf | awk '{ print $1 }' | copy) }

It seems to simply execute 2fa (lists all my MFA codes) and exits. Is there any reasonable way to get this to work?

Hmm there seems to be several things going on here.

  • The function definition seems wrong - it captures the output of copy (because the whole pipeline is wrapped in an output capture expression) and tries to execute it as a command - this should throw an exception

  • Most likely the behavior you're seeing is because somehow 2f wasn't actually defined according to what you posted and simply is an alias to 2fa itself. What does echo $2f~[body] output?

Have you tried testing the pipeline itself in the terminal (without the superfluous ()) - does it work as expected?

BTW, feel free to keep this conversation here, but if you have questions about using Elvish in the future, I'd recommend that you join the user group (linked from the README) and ask there. I have added issue forms that should hopefully make this clearer.

Have you tried testing the pipeline itself in the terminal (without the superfluous ()) - does it work as expected?

I have, and it still simply printed the output from the first command 2fa.

I ended up adding this in as a function via rc.elv

fn 2f { 2fa | fzf | awk '{print $1}' | copy }

And this works as expected--but I would prefer to add this as an alias so I can carry it with me through my backups. 🀷 Originally I had added the () to the alias because I wasn't sure if the pipes were interfering with something, so I wanted to try to group them.

The elvish really doesn't seem to like the pipes at all.

~\.elvish\aliases
➜ alias:new 2f 2fa | fzf | awk '{ print $1 }' | copy

And all this did was run a blank instance of fzf. I tried again with grouping;

~\.elvish\aliases 
➜ alias:new 2f { 2fa | fzf | awk '{ print $1 }' | copy }

~\.elvish\aliases
➜ alias:save 2f

The alias saved this time, but it's malformed;

#alias:new 2f <closure 0xc0002d4780>
edit:add-var 2f~ {|@_args|  <closure 0xc0002d4780> $@_args }

Just to see what would happen, I manually edited the 2f.elv file, refreshed my environment and tried it;

➜ 2f
Exception: wrong type: need !!eval.Callable, got string
Traceback:
  [eval 1]:2:29:
    edit:add-var 2f~ {|@_args|  edit:add-var 2f~ '{ 2fa | fzf | awk ''{ print $1 }'' | copy }' $@_args }
  [tty 1]:1:1:
    2f

Clearly there's something I'm not understanding here, but I'll take the function (which works as intended) as a win and call it good.

Appreciate the help.

OK, I see what's happening now, you're using zzamboni's alias module.

alias:new 2f 2fa | fzf | awk '{ print $1 }' | copy

Since alias:new is just a command, what's happening with this pipeline is that it's just invoking alias:new 2f 2fa and piping its output (which there probably isn't any) to the remaining commands.

I haven't used zzamboni's alias module and maybe you can try quoting the pipeline you want to define, like this:

alias:new 2f "2fa | fzf | awk '{ print $1 }' | copy"

If this doesn't work, then for simplicity I'd suggest that you just keep the 2f definition as part of your rc.elv. You can back it up too - if you can back up the alias definitions you should be able to back up rc.elv in the same way?

You can back it up too - if you can back up the alias definitions you should be able to back up rc.elv in the same way?

The backup solution that I was using was kind of a pain in the ass and didn't like to do single files and was generally picky. I spent the majority of the night swapping my backup solution to facilitate backing everything up, including rc.elv. lol

alias:new 2f "2fa | fzf | awk '{ print $1 }' | copy"

This also fails. It seems to expect the entire line to be an application and returns that it's not in $PATH. As I said, the definition seems to work just fine, and I'll be rolling with that from now on. I appreciate the time and attention.