Skip to content

Commit

Permalink
Fixes for --chdir {dir}, like path validation.
Browse files Browse the repository at this point in the history
Use Pushd when {dir} is a network path.
Updated help, README.md and docs.
  • Loading branch information
gerardog committed May 26, 2024
1 parent 687c099 commit 00f4fd6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Note: `gsudo.exe` is portable. No windows service is required or system change i
## Usage

``` powershell
gsudo [options] # Elevates your current shell
gsudo [options] # Starts your current shell elevated
gsudo [options] {command} [args] # Runs {command} with elevated permissions
gsudo cache [on | off | help] # Starts/Stops a credentials cache session. (less UAC popups)
gsudo status [--json | filter ] # Shows current user, cache and console status.
Expand Down Expand Up @@ -106,6 +106,7 @@ Other options:
--debug # Enable debug mode.
--copyns # Connect network drives to the elevated user. Warning: Verbose, interactive asks for credentials
--copyev # (deprecated) Copy environment variables to the elevated process. (not needed on default console mode)
--chdir {dir} # Change the current directory to {dir} before running the command.
```

**Note:** You can use anywhere **the `sudo` alias** created by the installers.
Expand Down
1 change: 1 addition & 0 deletions docs/docs/usage/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Other options:
--debug # Enable debug mode.
--copyns # Connect network drives to the elevated user. Warning: Verbose, interactive asks for credentials
--copyev # (deprecated) Copy environment variables to the elevated process. (not needed on default console mode)
--chdir {dir} # Change the current directory to {dir} before running the command.
```

Expand Down
1 change: 1 addition & 0 deletions src/gsudo/Commands/HelpCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ internal static void ShowHelp()
--debug Enable debug mode.
--copyns Connect network drives to the elevated user. Warning: Interactive asks for credentials
--copyev (deprecated) Copy all environment variables to the elevated process.
--chdir {dir} Change the current directory to {dir} before running the command.
Configuration:
gsudo config\t\t\t\tShow current config settings & values.
Expand Down
9 changes: 8 additions & 1 deletion src/gsudo/Helpers/CommandLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,14 @@ ICommand ParseOption(string argChar, string argWord, out bool skipRemainingChars
else if (match(null, "--preserve-env")) { Settings.CopyEnvironmentVariables.Value = true; }
else if (match(null, "--new-window")) { InputArguments.NewWindow = true; }
else if (argChar == "D" && argWord == "-D" && FileApi.PathExists(args.FirstOrDefault())) { InputArguments.StartingDirectory = DeQueueArg(); }
else if (match(null, "--chdir")) { InputArguments.StartingDirectory = DeQueueArg(); }
else if (match(null, "--chdir"))
{
InputArguments.StartingDirectory = DeQueueArg();
if (!FileApi.PathExists(InputArguments.StartingDirectory))
{
throw new ApplicationException($"Invalid directory: {InputArguments.StartingDirectory}");
}
}
else if (match(null, "--inline")) { InputArguments.NewWindow = false; }

// rest
Expand Down
7 changes: 4 additions & 3 deletions src/gsudo/Helpers/CommandToRunAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,15 @@ internal void Build()
postCommands.Add("exit /b !errl!");
}

bool bNetworkfolder = Environment.CurrentDirectory.StartsWith(@"\\", StringComparison.Ordinal);
string startupFolder = InputArguments.StartingDirectory ?? Environment.CurrentDirectory;
bool bNetworkfolder = startupFolder.StartsWith(@"\\", StringComparison.Ordinal);
bool bIsCmdExe = ArgumentsHelper.UnQuote(command.First()).EndsWith("cmd.exe", StringComparison.OrdinalIgnoreCase);

if (bNetworkfolder && (bIsCmdExe || mustWrap))
{
Logger.Instance.Log($"The current directory '{Environment.CurrentDirectory}' is a network folder. Mapping as a network drive.", LogLevel.Debug);
Logger.Instance.Log($"The path '{startupFolder}' is a network folder. Mapping as a network drive.", LogLevel.Debug);
// Prepending PUSHD command. It maps network folders magically!
preCommands.Insert(0, $"pushd \"{Environment.CurrentDirectory}\"");
preCommands.Insert(0, $"pushd \"{startupFolder}\"");
postCommands.Add("popd");
// And set current directory to local folder to avoid CMD warning message
Environment.CurrentDirectory = Environment.GetEnvironmentVariable("SystemRoot");
Expand Down

0 comments on commit 00f4fd6

Please sign in to comment.