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

Feature request: Shell alias expansion #2

Closed
croissong opened this issue Aug 18, 2021 · 19 comments
Closed

Feature request: Shell alias expansion #2

croissong opened this issue Aug 18, 2021 · 19 comments
Labels
enhancement New feature or request

Comments

@croissong
Copy link

It would be nice to be able to use aliases in the watch commands :)

@davidandradeduarte
Copy link

I've closed #9, since it was a duplicate of this one.

@davidandradeduarte
Copy link

davidandradeduarte commented Aug 19, 2021

Apparently if we do this:

alias my_alias="viddy " (note the extra space after the command)

It starts working with aliases:
my_alias k get pods -n all (k for kubectl)

@croissong
Copy link
Author

True, but this has the same shortcoming as mentioned here: https://unix.stackexchange.com/a/25329

Unfortunately this method has a limitation that you cannot provide options to watch because the alias has to be the next word, whereas watch needs its arguments before the command to run. If you tried watch -n 10 , bash won't expand the alias

@sachaos
Copy link
Owner

sachaos commented Aug 19, 2021

To solve this, should I parse the file which is defining the aliases (e.g. bashrc or zshrc)?

I haven't come up with another good way to do it.

@croissong
Copy link
Author

The command could be called via the shell:

shell := os.Getenv("SHELL")
cmd := exec.Command(shell, "-ci", "k get pods")

This could be considered insecure though, so maybe it could be opt-in via flag?

@davidandradeduarte
Copy link

Yep, that would be my suggestion too!

Get the shell and execute the command within that shell.

Should work for the most used shells at least.
* with fish i think the arguments order need to be ic instead of ci.

@sachaos
Copy link
Owner

sachaos commented Aug 19, 2021

Thank you! I understood.

@sachaos
Copy link
Owner

sachaos commented Aug 23, 2021

@croissong @davidandradeduarte I've added new options --shell, --shell-options in v0.1.8 .
Now, you can run alias command like following examples.

Please specify your aliases in ~/.profile (the files which are loaded on starting as login shell).

Define you aliases in .profile

alias ll="ls -l"

Run viddy

viddy --shell-options "--login" ll

How do you think this solution?

To run on the shell interactive mode, we should run a command on PTY.
It is not difficult implementation, but it is too complex to just support alias.
And interactive mode will load rc file (e.g. bashrc, zshrc), it is too heavy to call every time. (I'm defining much of functions and using complex prompt).
So I recommend to run a command on login shell with specifying --login option.
This will load profile file only.

The --shell-options is long name option, but I'm planning to implement loading the configuration from a file.

@davidandradeduarte
Copy link

davidandradeduarte commented Aug 23, 2021

It's good to at least have the option, but I don't think most users will maintain a separate file for aliases just to use in viddy :/

We can get them easily with:

alias | xargs -I {} echo "alias {}" > ~/.profile

But it's hard to maintain.

This will not work for me right now tho, because you're only getting the first string from the alias.
e.g

alias ll=ls -la

only gets ls instead of ls -la.

If I change it to

alias ll="ls -la"

with quotes, it works.

On another note, I've tried the --shell options with:

viddy --shell "/bin/zsh" ll

and doesn't seem to work either. Maybe I'm doing something wrong, haven't looked at the code yet.

Edit:

alias | parallel -I {} echo "alias {}" > ~/.profile

using parallel instead of xargs, doesn't remove the single quotes from aliases.

@sachaos
Copy link
Owner

sachaos commented Aug 24, 2021

@davidandradeduarte

This will not work for me right now tho, because you're only getting the first string from the alias.
e.g

alias ll=ls -la

I think this is invalid alias definition. The quotation is necessary.

On another note, I've tried the --shell options with:

viddy --shell "/bin/zsh" ll
and doesn't seem to work either.

When you want to use alias, you should specify --shell-options "--login" to load the alias definition file.

I think maintaining alias file is easy.
I suggest to create file for the aliases, and load that from .profile and rc file (e.g. .bashrc) like following.

Define alias in .alias

alias ll="ls -l"

Load that from .profile and rc file (.bashrc) too

source ~/.alias

@croissong
Copy link
Author

croissong commented Aug 24, 2021

@sachaos Thanks for the update!
It works as you described, and I understand the problems with the interactive mode.
For me personally, sourcing my aliases in .profile is fine.

I wasn't aware of the implications of using interactive mode outside of a pty, and to be honest, i still don't fully understand it (and how it can be solved).

But as far as i can tell, running the shell in non-interactive mode still provides all the other advantages of spawning a shell (e.g. pipes work), so i am perfectly happy with this solution 👍

@sachaos
Copy link
Owner

sachaos commented Aug 24, 2021

Thank you for trying out.

I couldn't get the output from interactive mode shell.
But, it might be my mistake.
Il've used golang pty package, and that worked.
I'm not using that now, to keep it simple.

@sachaos
Copy link
Owner

sachaos commented Aug 24, 2021

I would like to close this issue.

If there are other problems, feel free to open or comment here. :)

@sachaos sachaos closed this as completed Aug 24, 2021
@lirlia
Copy link

lirlia commented Aug 28, 2021

My alias file is little complex, so I use this function.

vd() {
	viddy -d -n 1 --shell zsh  "$(which $1 | cut -d' ' -f 4-)"
}

# ex:
alias kgp="kubectl get pod"

After I can run like this

$ vd kgp

caution: but vd can't handle functoin.

@onedr0p
Copy link
Contributor

onedr0p commented Aug 28, 2021

In fish this function works for me

function vdy --description 'modern watch'
    viddy -d -n 2 --shell fish $argv[1..-1]
end

@totoroot
Copy link

Thanks to this suggestion by @lirlia, I found a solution that works for me in zsh.
However, I noticed that this suggestion has the limitation of only being able to handle single word aliases. So, if I wanted to watch a command like the following, it wouldn't work:

vd k get pods

Many people have aliased kubectl to k so the alias should get expanded, but the following words of the command shall be handled as well.

We can do this by setting an alias like the following:

alias vd=(){viddy -d -n 1 --shell zsh  "$(which $1 | cut -d' ' -f 4-)${@:2}";}

${@:2} means that all remaining words of the command still get passed to viddy.

@Shaked
Copy link

Shaked commented Feb 7, 2024

I am not sure if this tool is still being developed, but in case it is - is there a way to add . ~/.zshrc right after the zsh -c?

I assume it should be here: https://github.com/sachaos/viddy/blob/master/snapshot.go#L117-L118.

The idea behind it is to run viddy with zsh -ic ". ~/.zshrc; $ARGS, which will support both aliases, functions and won't fail when using the aforementioned alias/function when running things like watch time

@sachaos
Copy link
Owner

sachaos commented Feb 8, 2024

@Shaked This tool is alive.
The suggestion seems interesting.
I think we can not add any argument right after -c now.

In this case we should add that feature as experimental option.
We should add --pre-command option or something to execute arbitary command before the ARGS.

The PR is welcome.

@Shaked
Copy link

Shaked commented Feb 8, 2024

@sachaos thanks for the quick reply!

I have started playing with it but it seems like my idea doesn't work. Only -ci would and that seems like a problem with viddy.

I don't mind filing a PR but I would have to find a way to make this work first :)

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

No branches or pull requests

7 participants