Skip to content

Commit

Permalink
(GH-1562) Fix: --version includes warnings/errors
Browse files Browse the repository at this point in the history
When asking for the version of Chocolatey that is installed, the only
output should be is the version. This breaks things that would use that
information for version parsing.
  • Loading branch information
ferventcoder committed Mar 12, 2019
1 parent 47cbdd8 commit 35e9f81
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/chocolatey/infrastructure/licensing/LicenseValidation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public static ChocolateyLicense validate()
LicenseType = ChocolateyLicenseType.Unknown
};

var regularLogOutput = determine_if_regular_output_for_logging();

string licenseFile = ApplicationParameters.LicenseFileLocation;
var userLicenseFile = ApplicationParameters.UserLicenseFileLocation;
if (File.Exists(userLicenseFile)) licenseFile = userLicenseFile;
Expand All @@ -50,7 +52,7 @@ public static ChocolateyLicense validate()
{
if (Directory.GetFiles(licenseDirectory).Length != 0)
{
"chocolatey".Log().Error(@"Files found in directory '{0}' but not a
"chocolatey".Log().Error(regularLogOutput ? ChocolateyLoggers.Normal : ChocolateyLoggers.LogFileOnly, @"Files found in directory '{0}' but not a
valid license file. License should be named '{1}'.".format_with(licenseDirectory, licenseFileName));
"chocolatey".Log().Warn(ChocolateyLoggers.Important,@" Rename license file to '{0}' to allow commercial features.".format_with(licenseFileName));
}
Expand All @@ -60,9 +62,9 @@ public static ChocolateyLicense validate()
// - user put the license file in the top level location and/or forgot to rename it
if (File.Exists(Path.Combine(ApplicationParameters.InstallLocation, licenseFileName)) || File.Exists(Path.Combine(ApplicationParameters.InstallLocation, licenseFileName + ".txt")))
{
"chocolatey".Log().Error(@"Chocolatey license found in the wrong location. File must be located at
"chocolatey".Log().Error(regularLogOutput ? ChocolateyLoggers.Normal : ChocolateyLoggers.LogFileOnly, @"Chocolatey license found in the wrong location. File must be located at
'{0}'.".format_with(ApplicationParameters.LicenseFileLocation));
"chocolatey".Log().Warn(ChocolateyLoggers.Important, @" Move license file to '{0}' to allow commercial features.".format_with(ApplicationParameters.LicenseFileLocation));
"chocolatey".Log().Warn(regularLogOutput ? ChocolateyLoggers.Important : ChocolateyLoggers.LogFileOnly, @" Move license file to '{0}' to allow commercial features.".format_with(ApplicationParameters.LicenseFileLocation));
}
}

Expand All @@ -88,15 +90,15 @@ public static ChocolateyLicense validate()
{
chocolateyLicense.IsValid = false;
chocolateyLicense.InvalidReason = e.Message;
"chocolatey".Log().Error("A license was not found for a licensed version of Chocolatey:{0} {1}{0} {2}".format_with(Environment.NewLine, e.Message,
"chocolatey".Log().Error(regularLogOutput ? ChocolateyLoggers.Normal : ChocolateyLoggers.LogFileOnly, "A license was not found for a licensed version of Chocolatey:{0} {1}{0} {2}".format_with(Environment.NewLine, e.Message,
"A license was also not found in the user profile: '{0}'.".format_with(ApplicationParameters.UserLicenseFileLocation)));
}
catch (Exception e)
{
//license may be invalid
chocolateyLicense.IsValid = false;
chocolateyLicense.InvalidReason = e.Message;
"chocolatey".Log().Error("A license was found for a licensed version of Chocolatey, but is invalid:{0} {1}".format_with(Environment.NewLine, e.Message));
"chocolatey".Log().Error(regularLogOutput ? ChocolateyLoggers.Normal : ChocolateyLoggers.LogFileOnly, "A license was found for a licensed version of Chocolatey, but is invalid:{0} {1}".format_with(Environment.NewLine, e.Message));
}

var chocolateyLicenseType = ChocolateyLicenseType.Unknown;
Expand Down Expand Up @@ -134,5 +136,16 @@ public static ChocolateyLicense validate()

return chocolateyLicense;
}

private static bool determine_if_regular_output_for_logging()
{
var args = Environment.GetCommandLineArgs();
if (args == null || args.Length < 2) return true;

var firstArg = args[1].to_string();
if (firstArg.is_equal_to("-v") || firstArg.is_equal_to("--version")) return false;

return true;
}
}
}

0 comments on commit 35e9f81

Please sign in to comment.