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: Nushell support #152

Closed
charlesroper opened this issue Aug 23, 2022 · 12 comments
Closed

Feature: Nushell support #152

charlesroper opened this issue Aug 23, 2022 · 12 comments

Comments

@charlesroper
Copy link

It would be great if gsudo could support Nushell.

It currently works fine if running a command like gsudo notepad.

However, if I run just gsudo on its own to elevate the session, it starts an elevated cmd session rather than natively elevating Nushell.

If I run gsudo nu that works, and Starship picks up that I am running elevated. So hopefully a fix for this issue could be fairly easy?

2022-08-23_03

@jmoore34
Copy link

A simple solution would be to put alias su = sudo nu in your config.nu

@gerardog
Copy link
Owner

I have a question... Actually two.

1. Shell elevation

When someone calls sudo or gsudo, it elevates the current shell.... From Nu that should elevate nu.exe. That is straightforward, except for the banner. There is no nu -k as in cmd /k, nor 'nu -NoLogo | -nol' as in pwsh, to skip the logo.
(Or is one? --interactive?)

image

2. command elevation

How gsudo should elevate commands when invoked from nu? Like in gsudo {command}.

This is different for every shell.

For example In cmd.exe, shell elevation is done by elevating cmd /k, while command elevation is cmd /c {command}

  • Should elevate as cmd commands? (as of right now) like gsudo -d would force from (for example) powershell or bash ?
  • Should elevate as nu commands? as in: gsudo ls /somedir" | Take 1. (it would become <elevated> nu -c "ls" | <non elevated>Take 1 ... and, what happens with the pipe, there?
    • If we elevate the command between the pipes | , then we have two processes that need to communicate via Stdio/Stdout. And in nu we get different results when doing ls | Take 1 and nu -c ls | Take 1.
    • Could be a shell plugin or a profile function.: sudo a|b|c could elevate a|b|c if nu syntax allows it.

Then, how to properly escape quoting...

@gerardog gerardog changed the title Nushell support Feature: Nushell support Aug 31, 2022
@fdncred
Copy link

fdncred commented Aug 31, 2022

right now, there is no way to disable the banner from the command line. I thought this may work but it appears that the check for the banner config may happen in the wrong order to make this command useful. nu -e 'let-env config = ($env.config | upsert show_banner false)'. I'm guessing that we'd probably accept a pr for nologo, /k type of functionality.

not sure about your second question

@charlesroper
Copy link
Author

Thanks for looking at this @gerardog

I've set show_banner: false in my config, so that's not a problem for me. See here.

I'm not quite sure about question 2 - I hadn't thought about it very deeply :)

If I run a command like gsudo ls C:\Windows\ | Take 1 then I would expect the whole thing would run elevated as nu commands. I can put a ^ in front of a command to escape to an external command.

So gsudo ls c:\Windows\System32 | take 1 would be like running gsudo nu -c "ls c:\Windows\System32 | take 1".

Or gsudo touch c:\Windows\System32\drivers\etc\hosts would be like running gsudo nu -c "touch c:\Windows\System32\drivers\etc\hosts"

It seems like an alias like @jmoore34 suggests could do it, but I'm not sure how to create an alias like this:

alias sudo = (gsudo nu -c "$args")

I've asked on Discord. :)

@fdncred
Copy link

fdncred commented Sep 2, 2022

I posted this to discord.

def sudo [...rest] { gsudo $"nu -c '($rest | str collect ' ')'" }

but you still have to wrap the command in quotes like sudo "ls | take 1". the things in quotes here are what shows up in the $rest variable.

The problem is that the nushell see's the pipe and stops collecting the rest because it thinks that's a new command which I think is why this doesn't work sudo ls | take 1

@charlesroper
Copy link
Author

Thanks. That didn't work, but this slight modification does:

def sudo [...rest] { gsudo nu -c $"'($rest | str collect ' ')'" }

Shame it doesn't work without the quotes, but can live with this solution.

@fdncred
Copy link

fdncred commented Sep 2, 2022

Ya, that's what I meant. 🤣

@jmoore34
Copy link

jmoore34 commented Sep 3, 2022

Thanks. That didn't work, but this slight modification does:

def sudo [...rest] { gsudo nu -c $"'($rest | str collect ' ')'" }

Shame it doesn't work without the quotes, but can live with this solution.

From what I can tell, $rest doesn't support passing over flags. E.g. I think you'll get an error from sudo ls -l

@charlesroper
Copy link
Author

@jmoore34 Ah yes, good spot, you're right.

Your suggestion of a su alias works pretty well for my purposes actually. And just doing a sudo nu -c "[nu commands]" works fine too.

@gerardog
Copy link
Owner

gerardog commented Sep 3, 2022

Can a Nu function receive an anonymous method/block as a parameter? Like sudo { ls -l } ?
... if so, it could then escape the block content and call gsudo nu -c "ls -l"...

gerardog added a commit that referenced this issue Oct 24, 2022
@gerardog
Copy link
Owner

Finally!

  • Calling gsudo from Nu, will elevate nu.exe

  • Calling gsudo command will elevate nu.exe -c "escaped_command", where escaped_command has the following rules
    - " => ""
    - \ => \\

  • Calling gsudo --noexit command will elevate nu.exe -e "escaped_command"

  • No planned support for multi-command pipelines (| operator).

You can test it in the v2.0.0-preview release.

For example (Running with loglevel=Debug)
image

image

@gerardog
Copy link
Owner

Closing as implemented in v2.0.0

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

4 participants