Skip to content

Commit

Permalink
add --include-options for config list
Browse files Browse the repository at this point in the history
  • Loading branch information
riina committed Mar 30, 2023
1 parent 23e751c commit c0f8440
Showing 1 changed file with 61 additions and 14 deletions.
75 changes: 61 additions & 14 deletions src/Art.Tesler/Config/ConfigCommandList.cs
Expand Up @@ -13,9 +13,10 @@ public class ConfigCommandList : CommandBase
private readonly IDefaultPropertyProvider _defaultPropertyProvider;
private readonly IProfileResolver _profileResolver;

protected Option<string> DefaultOption;
protected Option<string> DefaultsOption;
protected Option<string> ProfileOption;
protected Option<bool> SimpleOption;
protected Option<bool> IncludeDefaultsOption;

public ConfigCommandList(
IOutputPair toolOutput,
Expand All @@ -27,50 +28,60 @@ public class ConfigCommandList : CommandBase
{
_defaultPropertyProvider = defaultPropertyProvider;
_profileResolver = profileResolver;
DefaultOption = new Option<string>(new[] { "-d", "--default" }, "Tool to get defaults for")
DefaultsOption = new Option<string>(new[] { "-d", "--defaults" }, "Tool to get defaults for")
{
ArgumentHelpName = "tool-string"
};
AddOption(DefaultOption);
AddOption(DefaultsOption);
ProfileOption = new Option<string>(new[] { "-p", "--profile" }, "Profile to get options for")
{
ArgumentHelpName = "profile-path"
};
AddOption(ProfileOption);
SimpleOption = new Option<bool>(new[] { "-s", "--simple" }, "Print simple output");
AddOption(SimpleOption);
IncludeDefaultsOption =
new Option<bool>(new[] { "-i", "--include-defaults" }, "Include default options for profile");
AddOption(IncludeDefaultsOption);
AddValidator(result =>
{
var optionSet = new HashSet<Option>();
if (result.GetValueForOption(DefaultOption) != null)
if (result.GetValueForOption(DefaultsOption) != null)
{
optionSet.Add(DefaultOption);
optionSet.Add(DefaultsOption);
}
if (result.GetValueForOption(ProfileOption) != null)
{
optionSet.Add(ProfileOption);
}
var all = new Option[] { DefaultOption, ProfileOption };
var all = new Option[] { DefaultsOption, ProfileOption };
switch (optionSet.Count)
{
case 0:
result.ErrorMessage = $"One filter from {GetOptionAliasList(all)} must be specified";
break;
return;
case > 1:
result.ErrorMessage = $"Exactly one filter from {GetOptionAliasList(all)} must be specified";
break;
return;
}
if (result.GetValueForOption(ProfileOption) == null && result.GetValueForOption(IncludeDefaultsOption))
{
result.ErrorMessage =
$"Cannot use {IncludeDefaultsOption.Aliases.FirstOrDefault()} with {optionSet.FirstOrDefault()}";
}
});
}

protected override Task<int> RunAsync(InvocationContext context)
{
bool simple = context.ParseResult.HasOption(SimpleOption);
if (context.ParseResult.HasOption(DefaultOption))
bool includeDefaults = context.ParseResult.HasOption(IncludeDefaultsOption);
if (context.ParseResult.HasOption(DefaultsOption))
{
string toolString = context.ParseResult.GetValueForOption(DefaultOption)!;
string toolString = context.ParseResult.GetValueForOption(DefaultsOption)!;
if (!ArtifactToolIDUtil.TryParseID(toolString, out var toolID))
{
PrintErrorMessage($"Unable to parse tool string \"{toolString}\"", ToolOutput);
Expand All @@ -95,15 +106,51 @@ protected override Task<int> RunAsync(InvocationContext context)
var profileList = profiles.ToList();
if (profileList.Count == 1)
{
WriteOutput($"Properties for {profileString}",
profileList[0].Options ?? ImmutableDictionary<string, JsonElement>.Empty, simple);
if (includeDefaults)
{
var profile = profileList[0];
var properties = new Dictionary<string, JsonElement>();
_defaultPropertyProvider.WriteDefaultProperties(profile.GetID(), properties);
if (profile.Options is { } options)
{
foreach (var pair in options)
{
properties[pair.Key] = pair.Value;
}
}

WriteOutput($"Properties for {profileString}", properties, simple);
}
else
{
WriteOutput($"Properties for {profileString}",
profileList[0].Options ?? ImmutableDictionary<string, JsonElement>.Empty, simple);
}
}
else
{
for (int i = 0; i < profileList.Count; i++)
{
WriteOutput($"Properties for {profileString}:{i}",
profileList[i].Options ?? ImmutableDictionary<string, JsonElement>.Empty, simple);
if (includeDefaults)
{
var profile = profileList[i];
var properties = new Dictionary<string, JsonElement>();
_defaultPropertyProvider.WriteDefaultProperties(profile.GetID(), properties);
if (profile.Options is { } options)
{
foreach (var pair in options)
{
properties[pair.Key] = pair.Value;
}
}

WriteOutput($"Properties for {profileString}:{i}", properties, simple);
}
else
{
WriteOutput($"Properties for {profileString}:{i}",
profileList[i].Options ?? ImmutableDictionary<string, JsonElement>.Empty, simple);
}
}
}

Expand Down

0 comments on commit c0f8440

Please sign in to comment.