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/config-descriptions] Added descriptions to the config settings #354

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/gsudo/AppSettings/PathPrecedenceSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ namespace gsudo.AppSettings
internal class PathPrecedenceSetting : RegistrySetting<bool>
{
public PathPrecedenceSetting():
base("PathPrecedence", false, bool.Parse, RegistrySettingScope.GlobalOnly)
base("PathPrecedence", false, bool.Parse, RegistrySettingScope.GlobalOnly,
description: "Prioritize gsudo over Microsoft Sudo in the PATH environment variable.")
{

}
Expand Down
12 changes: 7 additions & 5 deletions src/gsudo/AppSettings/RegistrySetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ abstract class RegistrySetting
public RegistrySettingScope Scope { get; protected set; }

public string Name { get; set; }
public string Description { get; set; }
public abstract void Save(string newValue, bool global);
public abstract void Reset(bool global);
public abstract object GetStringValue();
Expand All @@ -37,19 +38,20 @@ class RegistrySetting<T> : RegistrySetting
private T runningValue;
private bool hasValue = false;
private readonly Func<string, T> deserializer;
private readonly Func<T, string> serializer;

public RegistrySetting(string name, T defaultValue, Func<string, T> deserializer, RegistrySettingScope scope = RegistrySettingScope.Any, Func<T, string> serializer = null)
: this(name, () => defaultValue, deserializer, scope, serializer)
private readonly Func<T, string> serializer;
public RegistrySetting(string name, T defaultValue, Func<string, T> deserializer, RegistrySettingScope scope = RegistrySettingScope.Any, Func<T, string> serializer = null, string description = null)
: this(name, () => defaultValue, deserializer, scope, serializer, description)
{ }

public RegistrySetting(string name, Func<T> defaultValue, Func<string, T> deserializer, RegistrySettingScope scope = RegistrySettingScope.Any, Func<T,string> serializer = null)
public RegistrySetting(string name, Func<T> defaultValue, Func<string, T> deserializer, RegistrySettingScope scope = RegistrySettingScope.Any, Func<T,string> serializer = null, string description = null)
{
Name = name.Replace('_', '.');
this.defaultValue = defaultValue;
this.deserializer = deserializer;
this.Scope = scope;
this.serializer = serializer;
this.Description = description;
}

public T Value
Expand Down
57 changes: 41 additions & 16 deletions src/gsudo/AppSettings/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,85 +18,110 @@ class Settings
public static RegistrySetting<CacheMode> CacheMode { get; set; }
= new RegistrySetting<CacheMode>(nameof(CacheMode), CredentialsCache.CacheMode.Explicit,
deserializer: ExtensionMethods.ParseEnum< CacheMode>,
scope: RegistrySettingScope.GlobalOnly);
scope: RegistrySettingScope.GlobalOnly,
description: "Defines how gsudo credentials cache works: Auto, Explicit (Manual), Disabled" );

public static RegistrySetting<TimeSpan> CacheDuration { get; }
= new RegistrySetting<TimeSpan>(nameof(CacheDuration),
defaultValue: TimeSpan.FromSeconds(300),
scope: RegistrySettingScope.GlobalOnly,
deserializer: TimeSpanParseWithInfinite,
serializer: TimeSpanWithInfiniteToString
);
serializer: TimeSpanWithInfiniteToString,
description: "Defines how long (HH:MM:SS) the credentials cache will be valid if idle. Use 'Infinite' for no expiration");

public static RegistrySetting<string> PipedPrompt { get; }
= new RegistrySetting<string>(nameof(PipedPrompt),
defaultValue: DefaultAsciiPrompt,
deserializer: (s) => s
deserializer: (s) => s,
description: "Prompt to be used when gsudo uses piped mode."
);

public static RegistrySetting<string> Prompt { get; }
= new RegistrySetting<string>(nameof(Prompt),
defaultValue: GetPromptDefaultValue,
deserializer: (s) => s);
deserializer: (s) => s,
description: "Prompt to be used when gsudo uses standard mode."
);

public static RegistrySetting<LogLevel> LogLevel { get; }
= new RegistrySetting<LogLevel>(nameof(LogLevel),
defaultValue: gsudo.LogLevel.Info,
deserializer: ExtensionMethods.ParseEnum<LogLevel>);
deserializer: ExtensionMethods.ParseEnum<LogLevel>,
description: "Defines the verbosity of the log. (Valid values: All, Debug, Info, Warning, Error, None)"
);

public static RegistrySetting<bool> ForcePipedConsole { get; }
= new RegistrySetting<bool>(nameof(ForcePipedConsole),
defaultValue: false,
deserializer: bool.Parse);
deserializer: bool.Parse,
description: "Forces gsudo to use legacy piped mode. Not recommended."
);

public static RegistrySetting<bool> ForceAttachedConsole { get; }
= new RegistrySetting<bool>(nameof(ForceAttachedConsole),
defaultValue: false,
deserializer: bool.Parse);
deserializer: bool.Parse,
description: "Forces gsudo to use Attached mode. Can fix some very specific problems. Same as --attached"
);

public static RegistrySetting<bool> ForceVTConsole { get; }
= new RegistrySetting<bool>(nameof(ForceVTConsole),
defaultValue: false,
deserializer: bool.Parse);
deserializer: bool.Parse,
description: "Forces gsudo to use VT mode. Experimental. Same as --vt"
);

public static RegistrySetting<bool> CopyEnvironmentVariables { get; }
= new RegistrySetting<bool>(nameof(CopyEnvironmentVariables),
defaultValue: false,
deserializer: bool.Parse);
deserializer: bool.Parse,
description: "Only applies to Attached Mode. Forces copying caller's env variables to the elevated context. Same as --CopyEv"
);

public static RegistrySetting<bool> CopyNetworkShares { get; } =
new RegistrySetting<bool>(nameof(CopyNetworkShares),
defaultValue: false,
deserializer: bool.Parse);
deserializer: bool.Parse,
description: "Reconnect network shares on the elevated context. Same as --CopyNs"
);

public static RegistrySetting<bool> PowerShellLoadProfile { get; } =
new RegistrySetting<bool>(nameof(PowerShellLoadProfile),
defaultValue: false,
bool.Parse);
bool.Parse,
description: "Loads the PowerShell profile when elevating PowerShell commands. Same as --LoadProfile"
);

public static RegistrySetting<bool> SecurityEnforceUacIsolation { get; } =
new RegistrySetting<bool>(nameof(SecurityEnforceUacIsolation),
defaultValue: false,
deserializer: bool.Parse,
scope: RegistrySettingScope.GlobalOnly);
scope: RegistrySettingScope.GlobalOnly,
description: "Elevates but with the input handle closed. More secure, less convenient. To be implemented soon also as --disableInput"
);

public static RegistrySetting<string> ExceptionList { get; } =
new RegistrySetting<string>(nameof(ExceptionList),
defaultValue: "notepad.exe;powershell.exe;whoami.exe;vim.exe;nano.exe;",
deserializer: (string s)=>s,
scope: RegistrySettingScope.GlobalOnly);
scope: RegistrySettingScope.GlobalOnly,
description: "List of executables with some issues so they will be started with \"cmd /c executable\""
);

public static RegistrySetting<bool> NewWindow_Force { get; } =
new RegistrySetting<bool>(nameof(NewWindow_Force),
defaultValue: false,
deserializer: bool.Parse,
scope: RegistrySettingScope.Any);
scope: RegistrySettingScope.Any,
description: "Always elevate in new window. Same as --new");

public static RegistrySetting<CloseBehaviour> NewWindow_CloseBehaviour { get; } =
new RegistrySetting<CloseBehaviour>(nameof(NewWindow_CloseBehaviour),
defaultValue: CloseBehaviour.OsDefault,
deserializer: ExtensionMethods.ParseEnum<CloseBehaviour>,
scope: RegistrySettingScope.Any);
scope: RegistrySettingScope.Any,
description: "When elevating in new window, let the window auto-close (OsDefault), KeepShellOpen or PressKeyToClose"
);

public static RegistrySetting<bool> PathOverrideSetting = new PathPrecedenceSetting();

Expand Down
16 changes: 14 additions & 2 deletions src/gsudo/Commands/ConfigCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,23 @@ public Task<int> Execute()

if (key == null)
{
// print all configs
// print all configs Descriptions
foreach (var k in Settings.AllKeys)
{
var scope = k.Value.HasGlobalValue() ? "(global)" :
Console.ForegroundColor = ConsoleColor.Yellow;
if (Settings.LogLevel <= LogLevel.Info)
{
Console.WriteLine($"# {k.Value.Name}: {k.Value.Description}");
}
Console.ResetColor();
}

// print all config values
foreach (var k in Settings.AllKeys)
{
var scope = k.Value.HasGlobalValue() ? "(global)" :
(k.Value.HasLocalValue() ? "(user)" : "(default)");

Console.WriteLine($"{k.Value.Name} = \"{ k.Value.GetStringValue().ToString()}\" ".PadRight(50) + scope);
}

Expand Down
Loading