Skip to content

Commit 13235a5

Browse files
JPRuskingep13
authored andcommitted
(#2829) Adds Basic choco license Command
This commit introduces a new command to Chocolatey, `license`. Currently, the functionality is limited, and it will only display details on the current license.
1 parent db1eb58 commit 13235a5

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

src/chocolatey/chocolatey.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178
<Compile Include="AssemblyExtensions.cs" />
179179
<Compile Include="ExceptionExtensions.cs" />
180180
<Compile Include="infrastructure.app\attributes\MultiServiceAttribute.cs" />
181+
<Compile Include="infrastructure.app\commands\ChocolateyLicenseCommand.cs" />
181182
<Compile Include="infrastructure.app\commands\ChocolateyCacheCommand.cs" />
182183
<Compile Include="infrastructure.app\commands\ChocolateyListCommand.cs" />
183184
<Compile Include="infrastructure.app\commands\ChocolateyRuleCommand.cs" />
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Copyright © 2017 - 2021 Chocolatey Software, Inc
2+
// Copyright © 2011 - 2017 RealDimensions Software, LLC
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
//
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
namespace chocolatey.infrastructure.app.commands
18+
{
19+
using chocolatey.infrastructure.app.attributes;
20+
using chocolatey.infrastructure.app.configuration;
21+
using chocolatey.infrastructure.commandline;
22+
using chocolatey.infrastructure.commands;
23+
using chocolatey.infrastructure.filesystem;
24+
using chocolatey.infrastructure.licensing;
25+
using chocolatey.infrastructure.logging;
26+
using System;
27+
using System.Collections.Generic;
28+
using System.Linq;
29+
using System.Text;
30+
31+
[CommandFor("license", "Retrieve or modify the Chocolatey License")]
32+
public class ChocolateyLicenseCommand : ICommand
33+
{
34+
public void configure_argument_parser(OptionSet optionSet, ChocolateyConfiguration configuration)
35+
{
36+
// We don't currently expect to have any arguments
37+
}
38+
39+
public void handle_additional_argument_parsing(IList<string> unparsedArguments, ChocolateyConfiguration configuration)
40+
{
41+
// We don't currently expect to have any additional arguments
42+
}
43+
44+
public void handle_validation(ChocolateyConfiguration configuration)
45+
{
46+
// We don't currently accept any arguments, so there is no validation
47+
}
48+
49+
public void help_message(ChocolateyConfiguration configuration)
50+
{
51+
this.Log().Info(ChocolateyLoggers.Important, "License Command");
52+
this.Log().Info(@"
53+
Chocolatey will do license things.
54+
");
55+
}
56+
57+
public bool may_require_admin_access()
58+
{
59+
return false;
60+
}
61+
62+
public void noop(ChocolateyConfiguration configuration)
63+
{
64+
}
65+
66+
public void run(ChocolateyConfiguration config)
67+
{
68+
var ourLicense = LicenseValidation.validate();
69+
var nodeCount = parse_node_count(ourLicense.Name);
70+
var logger = config.RegularOutput ? ChocolateyLoggers.Normal : ChocolateyLoggers.LogFileOnly;
71+
72+
if (ourLicense.LicenseType == ChocolateyLicenseType.Foss)
73+
{
74+
this.Log().Warn(logger, "No Commercial License Found, running with Open Source License.");
75+
return;
76+
}
77+
78+
if (!ourLicense.IsValid)
79+
{
80+
this.Log().Warn(logger, "We have found an invalid license for Chocolatey {0}: {1}", ourLicense.LicenseType, ourLicense.InvalidReason);
81+
Environment.ExitCode = 1;
82+
}
83+
84+
if (config.RegularOutput)
85+
{
86+
this.Log().Info("Registered to: {0}".format_with(ourLicense.Name));
87+
this.Log().Info("Expiration Date: {0} UTC".format_with(ourLicense.ExpirationDate));
88+
this.Log().Info("License type: {0}".format_with(ourLicense.LicenseType));
89+
this.Log().Info("Node Count: {0}".format_with(nodeCount));
90+
}
91+
else
92+
{
93+
// Headers: Name, LicenseType, ExpirationDate, NodeCount
94+
this.Log().Info("{0}|{1}|{2}|{3}".format_with(ourLicense.Name, ourLicense.LicenseType, ourLicense.ExpirationDate, nodeCount));
95+
}
96+
}
97+
98+
private int? parse_node_count(string licenseName)
99+
{
100+
if (string.IsNullOrWhiteSpace(licenseName))
101+
{
102+
return null;
103+
}
104+
105+
// Starting from the beginning of the license name
106+
var startIndex = 0;
107+
// while the start index is less than the length of the string
108+
// and there is a '[' in the name
109+
while (startIndex < licenseName.Length && (startIndex = licenseName.IndexOf('[', startIndex)) > -1)
110+
{
111+
var endIndex = 1;
112+
// increment endIndex (which is actually a relative position) while the next digit is still a digit
113+
while (startIndex + endIndex < licenseName.Length && char.IsDigit(licenseName[startIndex + endIndex]))
114+
{
115+
endIndex++;
116+
}
117+
118+
// If the length of the string is greater or equal than the length of the name, stop!
119+
if (licenseName.Length <= startIndex + endIndex)
120+
{
121+
break;
122+
}
123+
124+
// If next character is ']', return the content within the square braces
125+
if (licenseName[startIndex + endIndex] == ']')
126+
{
127+
return int.Parse(licenseName.Substring(startIndex + 1, endIndex - 1));
128+
}
129+
130+
// If we get this far, start testing at the end of the current range
131+
startIndex = startIndex + endIndex;
132+
}
133+
134+
return null;
135+
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)