Skip to content

Commit

Permalink
Add '-settingsfile' to 'pwsh' to support loading a custom powershell …
Browse files Browse the repository at this point in the history
…config file. (PowerShell#5920)

Support loading a custom `powershell.config.json` file via the command-line for use in testing.
This change supports replacing the default `powershell.config.json` file that's usually loaded from the `PSHome` directory with a custom version file.

The primary use-cases for this command-line option are as follows:
1. Allow the CI system to disable settings that impact test run times; such as disabling syslog usage on Linux and MacOS
2. Support testing of syslog and os_log without interfering with normal PowerShell operations during test runs via launching an instance with custom log settings.
  • Loading branch information
dantraMSFT authored and daxian-dbw committed Jan 25, 2018
1 parent a36d58c commit 4366280
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ internal class CommandLineParameterParser
"file",
"executionpolicy",
"command",
"settingsfile",
"help"
};

Expand Down Expand Up @@ -709,6 +710,36 @@ private void ParseHelper(string[] args)
break;
}
}

else if (MatchSwitch(switchKey, "settingsfile", "settings") )
{
++i;
if (i >= args.Length)
{
WriteCommandLineError(
CommandLineParameterParserStrings.MissingSettingsFileArgument);
break;
}
string configFile = null;
try
{
configFile = NormalizeFilePath(args[i]);
}
catch (Exception ex)
{
string error = string.Format(CultureInfo.CurrentCulture, CommandLineParameterParserStrings.InvalidSettingsFileArgument, args[i], ex.Message);
WriteCommandLineError(error);
break;
}

if (!System.IO.File.Exists(configFile))
{
string error = string.Format(CultureInfo.CurrentCulture, CommandLineParameterParserStrings.SettingsFileNotExists, configFile);
WriteCommandLineError(error);
break;
}
PowerShellConfig.Instance.SetSystemConfigFilePath(configFile);
}
#if STAMODE
// explicit setting of the ApartmentState Not supported on NanoServer
else if (MatchSwitch(switchKey, "sta", "s"))
Expand Down Expand Up @@ -849,6 +880,15 @@ private void ParseExecutionPolicy(string[] args, ref int i, ref string execution
executionPolicy = args[i];
}

private static string NormalizeFilePath(string path)
{
// Normalize slashes
path = path.Replace(StringLiterals.AlternatePathSeparator,
StringLiterals.DefaultPathSeparator);

return Path.GetFullPath(path);
}

private bool ParseFile(string[] args, ref int i, bool noexitSeen)
{
// Process file execution. We don't need to worry about checking -command
Expand Down Expand Up @@ -906,10 +946,7 @@ bool TryGetBoolValue(string arg, out bool boolValue)
string exceptionMessage = null;
try
{
// Normalize slashes
_file = args[i].Replace(StringLiterals.AlternatePathSeparator,
StringLiterals.DefaultPathSeparator);
_file = Path.GetFullPath(_file);
_file = NormalizeFilePath(args[i]);
}
catch (Exception e)
{
Expand Down Expand Up @@ -986,11 +1023,11 @@ bool TryGetBoolValue(string arg, out bool boolValue)
string argName = arg.Substring(0, offset);
if (TryGetBoolValue(argValue, out bool boolValue))
{
_collectedArgs.Add(new CommandParameter(argName, boolValue));
_collectedArgs.Add(new CommandParameter(argName, boolValue));
}
else
{
_collectedArgs.Add(new CommandParameter(argName, argValue));
_collectedArgs.Add(new CommandParameter(argName, argValue));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,15 @@
<data name="MissingConfigurationNameArgument" xml:space="preserve">
<value>Cannot process the command because -Configuration requires an argument that is a remote endpoint configuration name. Specify this argument and try again.</value>
</data>
<data name="MissingSettingsFileArgument" xml:space="preserve">
<value>Cannot process the command because -SettingsFile requires a file path. Supply a path for the SettingsFile parameter and then try the command again.</value>
</data>
<data name="InvalidSettingsFileArgument" xml:space="preserve">
<value>Processing -SettingsFile '{0}' failed: {1}. Specify a valid path for the -SettingsFile parameter.</value>
</data>
<data name="SettingsFileNotExists" xml:space="preserve">
<value>The argument '{0}' passed to the -SettingsFile does not exist. Provide the path to an existing json file as an argument to the -SettingsFile parameter.</value>
</data>
<data name="InvalidArgument" xml:space="preserve">
<value>Invalid argument '{0}', did you mean:</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,17 @@ All parameters are case-insensitive.</value>

-WindowStyle | -w
Sets the window style to Normal, Minimized, Maximized or Hidden.

-SettingsFile | -settings
Overrides the system-wide powershell.config.json settings file for the session.
By default, system-wide settings are read from the powershell.config.json
in the $PSHOME directory.

Note that these settings are not used by the endpoint specified
by the -ConfigurationName argument.

Example: pwsh -SettingsFile c:\myproject\powershell.config.json

</value>
</data>
</root>
Loading

0 comments on commit 4366280

Please sign in to comment.