-
Notifications
You must be signed in to change notification settings - Fork 903
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
(#2829) Adds Basic choco license Command (WIP) #2847
Draft
JPRuskin
wants to merge
1
commit into
chocolatey:develop
Choose a base branch
from
JPRuskin:license-command
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
src/chocolatey/infrastructure.app/commands/ChocolateyLicenseCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,138 @@ | ||||
// Copyright © 2017 - 2021 Chocolatey Software, Inc | ||||
// Copyright © 2011 - 2017 RealDimensions Software, LLC | ||||
// | ||||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||||
// you may not use this file except in compliance with the License. | ||||
// | ||||
// You may obtain a copy of the License at | ||||
// | ||||
// http://www.apache.org/licenses/LICENSE-2.0 | ||||
// | ||||
// Unless required by applicable law or agreed to in writing, software | ||||
// distributed under the License is distributed on an "AS IS" BASIS, | ||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
// See the License for the specific language governing permissions and | ||||
// limitations under the License. | ||||
|
||||
namespace chocolatey.infrastructure.app.commands | ||||
{ | ||||
using chocolatey.infrastructure.app.attributes; | ||||
using chocolatey.infrastructure.app.configuration; | ||||
using chocolatey.infrastructure.commandline; | ||||
using chocolatey.infrastructure.commands; | ||||
using chocolatey.infrastructure.filesystem; | ||||
using chocolatey.infrastructure.licensing; | ||||
using chocolatey.infrastructure.logging; | ||||
using System; | ||||
using System.Collections.Generic; | ||||
using System.Linq; | ||||
using System.Text; | ||||
|
||||
[CommandFor("license", "Retrieve or modify the Chocolatey License")] | ||||
public class ChocolateyLicenseCommand : ICommand | ||||
{ | ||||
public void configure_argument_parser(OptionSet optionSet, ChocolateyConfiguration configuration) | ||||
{ | ||||
// We don't currently expect to have any arguments | ||||
} | ||||
|
||||
public void handle_additional_argument_parsing(IList<string> unparsedArguments, ChocolateyConfiguration configuration) | ||||
{ | ||||
// We don't currently expect to have any additional arguments | ||||
} | ||||
|
||||
public void handle_validation(ChocolateyConfiguration configuration) | ||||
{ | ||||
// We don't currently accept any arguments, so there is no validation | ||||
} | ||||
|
||||
public void help_message(ChocolateyConfiguration configuration) | ||||
{ | ||||
this.Log().Info(ChocolateyLoggers.Important, "License Command"); | ||||
this.Log().Info(@" | ||||
Chocolatey will do license things. | ||||
"); | ||||
} | ||||
|
||||
public bool may_require_admin_access() | ||||
{ | ||||
return false; | ||||
} | ||||
|
||||
public void noop(ChocolateyConfiguration configuration) | ||||
{ | ||||
} | ||||
|
||||
public void run(ChocolateyConfiguration config) | ||||
{ | ||||
var ourLicense = LicenseValidation.validate(); | ||||
var nodeCount = parse_node_count(ourLicense.Name); | ||||
var logger = config.RegularOutput ? ChocolateyLoggers.Normal : ChocolateyLoggers.LogFileOnly; | ||||
|
||||
if (ourLicense.LicenseType == ChocolateyLicenseType.Foss) | ||||
{ | ||||
this.Log().Warn(logger, "No Commercial License Found, running with Open Source License."); | ||||
return; | ||||
} | ||||
|
||||
if (!ourLicense.IsValid) | ||||
{ | ||||
this.Log().Warn(logger, "We have found an invalid license for Chocolatey {0}: {1}", ourLicense.LicenseType, ourLicense.InvalidReason); | ||||
Environment.ExitCode = 1; | ||||
} | ||||
|
||||
if (config.RegularOutput) | ||||
{ | ||||
this.Log().Info("Registered to: {0}".format_with(ourLicense.Name)); | ||||
this.Log().Info("Expiration Date: {0} UTC".format_with(ourLicense.ExpirationDate)); | ||||
this.Log().Info("License type: {0}".format_with(ourLicense.LicenseType)); | ||||
this.Log().Info("Node Count: {0}".format_with(nodeCount)); | ||||
} | ||||
else | ||||
{ | ||||
// Headers: Name, LicenseType, ExpirationDate, NodeCount | ||||
this.Log().Info("{0}|{1}|{2}|{3}".format_with(ourLicense.Name, ourLicense.LicenseType, ourLicense.ExpirationDate, nodeCount)); | ||||
} | ||||
} | ||||
|
||||
private int? parse_node_count(string licenseName) | ||||
{ | ||||
if (string.IsNullOrWhiteSpace(licenseName)) | ||||
{ | ||||
return null; | ||||
} | ||||
|
||||
// Starting from the beginning of the license name | ||||
var startIndex = 0; | ||||
// while the start index is less than the length of the string | ||||
// and there is a '[' in the name | ||||
while (startIndex < licenseName.Length && (startIndex = licenseName.IndexOf('[', startIndex)) > -1) | ||||
{ | ||||
var endIndex = 1; | ||||
// increment endIndex (which is actually a relative position) while the next digit is still a digit | ||||
while (startIndex + endIndex < licenseName.Length && char.IsDigit(licenseName[startIndex + endIndex])) | ||||
{ | ||||
endIndex++; | ||||
} | ||||
|
||||
// If the length of the string is greater or equal than the length of the name, stop! | ||||
if (licenseName.Length <= startIndex + endIndex) | ||||
{ | ||||
break; | ||||
} | ||||
|
||||
// If next character is ']', return the content within the square braces | ||||
if (licenseName[startIndex + endIndex] == ']') | ||||
{ | ||||
return int.Parse(licenseName.Substring(startIndex + 1, endIndex - 1)); | ||||
} | ||||
|
||||
// If we get this far, start testing at the end of the current range | ||||
startIndex = startIndex + endIndex; | ||||
} | ||||
|
||||
return null; | ||||
|
||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gary wouldn't approve this.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. He also suggests using the regex used in other parsing parts, for consistency.
|
||||
} | ||||
} | ||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like we can use
is_licensed_version()
: https://github.com/chocolatey/choco/blob/develop/src/chocolatey/infrastructure/licensing/ChocolateyLicense.cs#L36