diff --git a/README.md b/README.md index d1379bc7..03e9072e 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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. diff --git a/docs/docs/usage/usage.md b/docs/docs/usage/usage.md index 69e67daf..4200d738 100644 --- a/docs/docs/usage/usage.md +++ b/docs/docs/usage/usage.md @@ -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. ``` diff --git a/src/gsudo/Commands/HelpCommand.cs b/src/gsudo/Commands/HelpCommand.cs index 490ac771..6cf723fc 100644 --- a/src/gsudo/Commands/HelpCommand.cs +++ b/src/gsudo/Commands/HelpCommand.cs @@ -63,6 +63,7 @@ gsudo status {key} [--no-output]\tShows status filtered by json {key}. Boolean k --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. diff --git a/src/gsudo/Helpers/CommandLineParser.cs b/src/gsudo/Helpers/CommandLineParser.cs index 73c46858..588e0936 100644 --- a/src/gsudo/Helpers/CommandLineParser.cs +++ b/src/gsudo/Helpers/CommandLineParser.cs @@ -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 diff --git a/src/gsudo/Helpers/CommandToRunAdapter.cs b/src/gsudo/Helpers/CommandToRunAdapter.cs index 99968b03..209b756b 100644 --- a/src/gsudo/Helpers/CommandToRunAdapter.cs +++ b/src/gsudo/Helpers/CommandToRunAdapter.cs @@ -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");