Skip to content

Commit de62fd2

Browse files
committed
(chocolatey#2591) Add headers to limit output commands
When a user asks for limited output from Chocolatey, it is not uncommon to pipe that output to `ConvertFrom-String` or `ConvertFrom-Csv` and manually add headers to get back an object. This allows for getting a header row back so that the end user doesn't need to add their own headers and discern what they are. This also adds a StringResources static class that allows us to store constant strings in and use them across the code to reduce duplication.
1 parent e35fe2b commit de62fd2

18 files changed

Lines changed: 271 additions & 26 deletions

src/chocolatey/StringResources.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace chocolatey
7+
{
8+
public static class StringResources
9+
{
10+
public static class OptionDescriptions
11+
{
12+
public const string DISPLAY_HEADERS = "Display headers - Display headers when limit-output is used. Requires <INSERT RELEASED VERSION HERE>";
13+
}
14+
15+
public static class Options
16+
{
17+
public const string DISPLAY_HEADERS = "display-headers|use-headers";
18+
}
19+
}
20+
}

src/chocolatey/chocolatey.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@
360360
<Compile Include="ObjectExtensions.cs" />
361361
<Compile Include="Properties\AssemblyInfo.cs" />
362362
<Compile Include="StringExtensions.cs" />
363+
<Compile Include="StringResources.cs" />
363364
<Compile Include="TypeExtensions.cs" />
364365
</ItemGroup>
365366
<ItemGroup>

src/chocolatey/infrastructure.app/ApplicationParameters.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ public static class Features
208208
public static readonly string LogValidationResultsOnWarnings = "logValidationResultsOnWarnings";
209209
public static readonly string UsePackageRepositoryOptimizations = "usePackageRepositoryOptimizations";
210210
public static readonly string DisableCompatibilityChecks = "disableCompatibilityChecks";
211+
public static readonly string AlwaysDisplayHeaders = "alwaysDisplayHeaders";
211212
}
212213

213214
public static class Messages

src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ private static void set_feature_flags(ChocolateyConfiguration config, ConfigFile
318318
config.Features.LogValidationResultsOnWarnings = set_feature_flag(ApplicationParameters.Features.LogValidationResultsOnWarnings, configFileSettings, defaultEnabled: true, description: "Log validation results on warnings - Should the validation results be logged if there are warnings? Available in 0.10.12+.");
319319
config.Features.UsePackageRepositoryOptimizations = set_feature_flag(ApplicationParameters.Features.UsePackageRepositoryOptimizations, configFileSettings, defaultEnabled: true, description: "Use Package Repository Optimizations - Turn on optimizations for reducing bandwidth with repository queries during package install/upgrade/outdated operations. Should generally be left enabled, unless a repository needs to support older methods of query. When disabled, this makes queries similar to the way they were done in Chocolatey v0.10.11 and before. Available in 0.10.14+.");
320320
config.PromptForConfirmation = !set_feature_flag(ApplicationParameters.Features.AllowGlobalConfirmation, configFileSettings, defaultEnabled: false, description: "Prompt for confirmation in scripts or bypass.");
321-
config.DisableCompatibilityChecks = set_feature_flag(ApplicationParameters.Features.DisableCompatibilityChecks, configFileSettings, defaultEnabled: false, description: "Disable Compatibility Checks - Should a warning we shown, before and after command execution, when a runtime compatibility check determines that there is an incompatibility between Chocolatey and Chocolatey Licensed Extension. Available in 1.1.0+");
321+
config.DisableCompatibilityChecks = set_feature_flag(ApplicationParameters.Features.DisableCompatibilityChecks, configFileSettings, defaultEnabled: false, description: "Disable Compatibility Checks - Should a warning be shown, before and after command execution, when a runtime compatibility check determines that there is an incompatibility between Chocolatey and Chocolatey Licensed Extension. Available in 1.1.0+");
322+
config.DisplayHeaders = set_feature_flag(ApplicationParameters.Features.AlwaysDisplayHeaders, configFileSettings, defaultEnabled: false, description: StringResources.OptionDescriptions.DISPLAY_HEADERS);
322323
}
323324

324325
private static bool set_feature_flag(string featureName, ConfigFileSettings configFileSettings, bool defaultEnabled, string description)

src/chocolatey/infrastructure.app/commands/ChocolateyApiKeyCommand.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
5151
.Add("rem|remove",
5252
"Removes an API key from Chocolatey",
5353
option => configuration.ApiKeyCommand.Remove = true)
54+
.Add(StringResources.Options.DISPLAY_HEADERS,
55+
StringResources.OptionDescriptions.DISPLAY_HEADERS,
56+
option => configuration.DisplayHeaders = true)
5457
;
5558
}
5659

@@ -156,6 +159,11 @@ public virtual void run(ChocolateyConfiguration configuration)
156159
}
157160
else if (string.IsNullOrWhiteSpace(configuration.ApiKeyCommand.Key))
158161
{
162+
if (!configuration.RegularOutput && configuration.DisplayHeaders)
163+
{
164+
this.Log().Info("Source|Key");
165+
}
166+
159167
_configSettingsService.get_api_key(configuration, (key) =>
160168
{
161169
string authenticatedString = string.IsNullOrWhiteSpace(key.Key) ? string.Empty : "(Authenticated)";

src/chocolatey/infrastructure.app/commands/ChocolateyConfigCommand.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
5151
"value=",
5252
"Value - the value of the config setting. Required with some actions. Defaults to empty.",
5353
option => configuration.ConfigCommand.ConfigValue = option.remove_surrounding_quotes())
54+
.Add(StringResources.Options.DISPLAY_HEADERS,
55+
StringResources.OptionDescriptions.DISPLAY_HEADERS,
56+
option => configuration.DisplayHeaders = true)
5457
;
5558
}
5659

src/chocolatey/infrastructure.app/commands/ChocolateyFeatureCommand.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
4747
.Add("n=|name=",
4848
"Name - the name of the source. Required with actions other than list. Defaults to empty.",
4949
option => configuration.FeatureCommand.Name = option.remove_surrounding_quotes())
50+
.Add(StringResources.Options.DISPLAY_HEADERS,
51+
StringResources.OptionDescriptions.DISPLAY_HEADERS,
52+
option => configuration.DisplayHeaders = true)
5053
;
5154
}
5255

src/chocolatey/infrastructure.app/commands/ChocolateyInfoCommand.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public override void configure_argument_parser(OptionSet optionSet, ChocolateyCo
7373
configuration.Features.UsePackageRepositoryOptimizations = false;
7474
}
7575
})
76+
.Add(StringResources.Options.DISPLAY_HEADERS,
77+
StringResources.OptionDescriptions.DISPLAY_HEADERS,
78+
option => configuration.DisplayHeaders = true)
7679
;
7780
}
7881

src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
136136
configuration.Features.UsePackageRepositoryOptimizations = false;
137137
}
138138
})
139+
.Add(StringResources.Options.DISPLAY_HEADERS,
140+
StringResources.OptionDescriptions.DISPLAY_HEADERS,
141+
option => configuration.DisplayHeaders = true)
139142
;
140143
}
141144

src/chocolatey/infrastructure.app/commands/ChocolateyOutdatedCommand.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
7171
configuration.Features.UsePackageRepositoryOptimizations = false;
7272
}
7373
})
74+
.Add(StringResources.Options.DISPLAY_HEADERS,
75+
StringResources.OptionDescriptions.DISPLAY_HEADERS,
76+
option => configuration.DisplayHeaders = true)
7477
;
7578
}
7679

0 commit comments

Comments
 (0)