Skip to content

Commit c18d926

Browse files
authored
Merge pull request #2856 from corbob/add_headers
(#2591) Add headers to limit output commands
2 parents 6b1bc1f + 0297f39 commit c18d926

File tree

18 files changed

+349
-51
lines changed

18 files changed

+349
-51
lines changed

src/chocolatey.resources/helpers/ChocolateyTabExpansion.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function script:chocoCmdOperations($commands, $command, $filter, $currentArgumen
3838
$script:chocoCommands = @('apikey','cache','config','export','feature','help','info','install','license','list','new','outdated','pack','pin','push','rule','search','source','support','template','uninstall','upgrade','--help','--version')
3939

4040
# ensure these all have a space to start, or they will cause issues
41-
$allCommands = " --accept-license --cache-location='' --debug --fail-on-standard-error --force --help --ignore-http-cache --limit-output --log-file='' --no-color --no-progress --noop --online --proxy='' --proxy-bypass-list='' --proxy-bypass-on-local --proxy-password='' --proxy-user='' --skip-compatibility-checks --timeout='' --trace --use-system-powershell --verbose --yes"
41+
$allCommands = " --accept-license --cache-location='' --debug --fail-on-standard-error --force --help --ignore-http-cache --include-headers --limit-output --log-file='' --no-color --no-progress --noop --online --proxy='' --proxy-bypass-list='' --proxy-bypass-on-local --proxy-password='' --proxy-user='' --skip-compatibility-checks --timeout='' --trace --use-system-powershell --verbose --yes"
4242

4343
$commandOptions = @{
4444
apikey = "--api-key='' --source=''"

src/chocolatey/OutputHelpers.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using chocolatey.infrastructure.app.configuration;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace chocolatey
9+
{
10+
public static class OutputHelpers
11+
{
12+
public static void LimitedOutput(params string[] output)
13+
{
14+
"chocolatey".Log().Info(output.Join("|"));
15+
}
16+
}
17+
}

src/chocolatey/StringResources.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,5 +655,15 @@ public static class ErrorMessages
655655
internal const string UnableToDowngrade = "A newer version of {0} (v{1}) is already installed.{2} Use --allow-downgrade or --force to attempt to install older versions.";
656656
internal const string DependencyFailedToInstall = "Failed to install {0} because a previous dependency failed.";
657657
}
658+
659+
public static class OptionDescriptions
660+
{
661+
public const string IncludeHeaders = "Include header names when --limit-output is used. Requires Chocolatey CLI 2.5.0+";
662+
}
663+
664+
public static class Options
665+
{
666+
public const string IncludeHeaders = "include-headers";
667+
}
658668
}
659-
}
669+
}

src/chocolatey/chocolatey.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@
459459
<Compile Include="LogExtensions.cs" />
460460
<Compile Include="NuGetVersionExtensions.cs" />
461461
<Compile Include="ObjectExtensions.cs" />
462+
<Compile Include="OutputHelpers.cs" />
462463
<Compile Include="Properties\AssemblyInfo.cs" />
463464
<Compile Include="RuleResultExtensions.cs" />
464465
<Compile Include="StringExtensions.cs" />

src/chocolatey/infrastructure.app/ApplicationParameters.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ public static class Features
275275
public static readonly string UsePackageRepositoryOptimizations = "usePackageRepositoryOptimizations";
276276
public static readonly string DisableCompatibilityChecks = "disableCompatibilityChecks";
277277
public static readonly string UsePackageHashValidation = "usePackageHashValidation";
278+
public static readonly string AlwaysIncludeHeaders = "alwaysIncludeHeaders";
278279
}
279280

280281
public static class Messages

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,8 @@ private static void SetAllFeatureFlags(ChocolateyConfiguration config, ConfigFil
341341
config.Features.UsePackageRepositoryOptimizations = SetFeatureFlag(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 earlier versions of Chocolatey.");
342342
config.Features.UsePackageHashValidation = SetFeatureFlag(ApplicationParameters.Features.UsePackageHashValidation, configFileSettings, defaultEnabled: false, description: "Use Package Hash Validation - Check the hash of the downloaded package file against the source provided hash. Only supports sources that provide SHA512 hashes. Disabled by default. Available in 2.3.0+");
343343
config.PromptForConfirmation = !SetFeatureFlag(ApplicationParameters.Features.AllowGlobalConfirmation, configFileSettings, defaultEnabled: false, description: "Prompt for confirmation in scripts or bypass.");
344-
config.DisableCompatibilityChecks = SetFeatureFlag(ApplicationParameters.Features.DisableCompatibilityChecks, configFileSettings, defaultEnabled: false, description: "Disable Compatibility Checks - Disable showing a warning when there is an incompatibility between Chocolatey CLI and Chocolatey Licensed Extension.");
344+
config.DisableCompatibilityChecks = SetFeatureFlag(ApplicationParameters.Features.DisableCompatibilityChecks, configFileSettings, defaultEnabled: false, description: "Disable Compatibility Checks - Disable showing a warning when there is an incompatibility between Chocolatey CLI and Chocolatey Licensed Extension. Available in 1.1.0+");
345+
config.IncludeHeaders = SetFeatureFlag(ApplicationParameters.Features.AlwaysIncludeHeaders, configFileSettings, defaultEnabled: false, description: StringResources.OptionDescriptions.IncludeHeaders);
345346
}
346347

347348
private static bool SetFeatureFlag(string featureName, ConfigFileSettings configFileSettings, bool defaultEnabled, string description)
@@ -411,6 +412,9 @@ private static void SetGlobalOptions(IList<string> args, ChocolateyConfiguration
411412
.Add("r|limitoutput|limit-output",
412413
"LimitOutput - Limit the output to essential information",
413414
option => config.RegularOutput = option == null)
415+
.Add(StringResources.Options.IncludeHeaders,
416+
StringResources.OptionDescriptions.IncludeHeaders,
417+
option => config.IncludeHeaders = true)
414418
.Add("timeout=|execution-timeout=",
415419
"CommandExecutionTimeout (in seconds) - The time to allow a command to finish before timing out. Overrides the default execution timeout in the configuration of {0} seconds. Supply '0' to disable the timeout.".FormatWith(config.CommandExecutionTimeoutSeconds.ToStringSafe()),
416420
option =>
@@ -453,7 +457,7 @@ private static void SetGlobalOptions(IList<string> args, ChocolateyConfiguration
453457
.Add("proxy-bypass-on-local",
454458
"Proxy Bypass On Local - Bypass proxy for local connections. Requires explicit proxy (`--proxy` or config setting). Overrides the default proxy bypass on local setting of '{0}'.".FormatWith(config.Proxy.BypassOnLocal),
455459
option => config.Proxy.BypassOnLocal = option != null)
456-
.Add("log-file=",
460+
.Add("log-file=",
457461
"Log File to output to in addition to regular loggers.",
458462
option => config.AdditionalLogFileLocation = option.UnquoteSafe())
459463
.Add("skipcompatibilitychecks|skip-compatibility-checks",

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ public virtual void Run(ChocolateyConfiguration configuration)
197197
_configSettingsService.SetApiKey(configuration);
198198
break;
199199
default:
200+
// This is required, since we only want to show the header if there are any ApiKeys
201+
// to be shown, which will be when we are printing the first ApiKey
202+
var hasHeaderRowBeenOutput = false;
203+
200204
_configSettingsService.GetApiKey(configuration, (key) =>
201205
{
202206
var authenticatedString = string.IsNullOrWhiteSpace(key.Key) ? string.Empty : "(Authenticated)";
@@ -207,7 +211,13 @@ public virtual void Run(ChocolateyConfiguration configuration)
207211
}
208212
else
209213
{
210-
this.Log().Info(() => "{0}|{1}".FormatWith(key.Source, authenticatedString));
214+
if (configuration.IncludeHeaders && !hasHeaderRowBeenOutput)
215+
{
216+
OutputHelpers.LimitedOutput("Source", "ApiKey");
217+
hasHeaderRowBeenOutput = true;
218+
}
219+
220+
OutputHelpers.LimitedOutput(key.Source, authenticatedString);
211221
}
212222
});
213223
break;

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,14 @@ private void GetLicense(ChocolateyConfiguration config)
116116
}
117117
else
118118
{
119-
// Headers: Name, LicenseType, ExpirationDate, NodeCount
120-
this.Log().Info("{0}|{1}|{2}|{3}".FormatWith(ourLicense.Name, ourLicense.LicenseType, ourLicense.ExpirationDate?.ToString("yyyy-MM-dd"), nodeCount));
119+
// This if we get here, there is a license to display, it is "safe" to always
120+
// output the header row
121+
if (config.IncludeHeaders)
122+
{
123+
OutputHelpers.LimitedOutput("Name", "Type", "ExpirationDate", "NodeCount");
124+
}
125+
126+
OutputHelpers.LimitedOutput(ourLicense.Name, ourLicense.LicenseType.ToString(), ourLicense.ExpirationDate?.ToString("yyyy-MM-dd"), nodeCount.ToString());
121127
}
122128
}
123129

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,22 @@ public virtual void ListPins(ChocolateyConfiguration config)
178178
config.QuietOutput = quiet;
179179
config.Input = input;
180180

181+
// This is required, since we only want to show the header if there are any Pinned Packages
182+
// to be shown, which will be when we are printing the first Pinned Package
183+
var hasHeaderRowBeenOutput = false;
184+
181185
foreach (var pkg in packages.OrEmpty())
182186
{
183187
var pkgInfo = _packageInfoService.Get(pkg.PackageMetadata);
184188
if (pkgInfo != null && pkgInfo.IsPinned)
185189
{
186-
this.Log().Info(() => "{0}|{1}".FormatWith(pkgInfo.Package.Id, pkgInfo.Package.Version));
190+
if (!config.RegularOutput && config.IncludeHeaders && !hasHeaderRowBeenOutput)
191+
{
192+
OutputHelpers.LimitedOutput("Id", "Version");
193+
hasHeaderRowBeenOutput = true;
194+
}
195+
196+
OutputHelpers.LimitedOutput(pkgInfo.Package.Id, pkgInfo.Package.Version.ToNormalizedStringChecked());
187197
}
188198
}
189199
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ protected virtual void OutputRules(string type, ChocolateyConfiguration config,
185185
}
186186
}
187187

188+
// This is required, since we only want to show the header if there are any Rules
189+
// to be shown, which will be when we are printing the first Rule
190+
var hasHeaderRowBeenOutput = false;
191+
188192
foreach (var rule in rules)
189193
{
190194
if (config.RegularOutput)
@@ -193,7 +197,13 @@ protected virtual void OutputRules(string type, ChocolateyConfiguration config,
193197
}
194198
else
195199
{
196-
this.Log().Info("{0}|{1}|{2}|{3}", rule.Severity, rule.Id, rule.Summary, rule.HelpUrl);
200+
if (config.IncludeHeaders && !hasHeaderRowBeenOutput)
201+
{
202+
OutputHelpers.LimitedOutput("Severity", "Id", "Summary", "HelpUrl");
203+
hasHeaderRowBeenOutput = true;
204+
}
205+
206+
OutputHelpers.LimitedOutput(rule.Severity.ToString(), rule.Id, rule.Summary, rule.HelpUrl);
197207
}
198208
}
199209
}

0 commit comments

Comments
 (0)