Skip to content

Commit a22d727

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 a22d727

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-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: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// Copyright © 2017 - 2025 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+
using System;
18+
using System.Collections.Generic;
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.licensing;
24+
using chocolatey.infrastructure.logging;
25+
26+
namespace chocolatey.infrastructure.app.commands
27+
{
28+
[CommandFor("license", "Retrieve Chocolatey License information", Version = "2.5.0")]
29+
public class ChocolateyLicenseCommand : ICommand
30+
{
31+
public void ConfigureArgumentParser(OptionSet optionSet, ChocolateyConfiguration configuration)
32+
{
33+
// We don't currently expect to have any arguments
34+
}
35+
36+
public void ParseAdditionalArguments(IList<string> unparsedArguments, ChocolateyConfiguration configuration)
37+
{
38+
// We don't currently expect to have any additional arguments
39+
}
40+
41+
public void Validate(ChocolateyConfiguration configuration)
42+
{
43+
// We don't currently accept any arguments, so there is no validation
44+
}
45+
46+
public void HelpMessage(ChocolateyConfiguration configuration)
47+
{
48+
this.Log().Info(ChocolateyLoggers.Important, "License Command");
49+
this.Log().Info(@"
50+
Chocolatey will do license things.
51+
");
52+
}
53+
54+
public bool MayRequireAdminAccess()
55+
{
56+
return false;
57+
}
58+
59+
public void DryRun(ChocolateyConfiguration configuration)
60+
{
61+
}
62+
63+
public void Run(ChocolateyConfiguration config)
64+
{
65+
var ourLicense = LicenseValidation.Validate();
66+
var nodeCount = ParseNodeCountFromLicenseName(ourLicense.Name);
67+
var logger = config.RegularOutput ? ChocolateyLoggers.Normal : ChocolateyLoggers.LogFileOnly;
68+
69+
if (ourLicense.LicenseType == ChocolateyLicenseType.Foss)
70+
{
71+
this.Log().Warn(logger, "No Commercial License Found, running with Open Source License.");
72+
return;
73+
}
74+
75+
if (!ourLicense.IsValid)
76+
{
77+
this.Log().Warn(logger, "We have found an invalid license for Chocolatey {0}: {1}", ourLicense.LicenseType, ourLicense.InvalidReason);
78+
Environment.ExitCode = 1;
79+
}
80+
81+
if (config.RegularOutput)
82+
{
83+
this.Log().Info("Registered to: {0}".FormatWith(ourLicense.Name));
84+
this.Log().Info("Expiration Date: {0} UTC".FormatWith(ourLicense.ExpirationDate));
85+
this.Log().Info("License type: {0}".FormatWith(ourLicense.LicenseType));
86+
this.Log().Info("Node Count: {0}".FormatWith(nodeCount));
87+
}
88+
else
89+
{
90+
// Headers: Name, LicenseType, ExpirationDate, NodeCount
91+
this.Log().Info("{0}|{1}|{2}|{3}".FormatWith(ourLicense.Name, ourLicense.LicenseType, ourLicense.ExpirationDate, nodeCount));
92+
}
93+
}
94+
95+
private int? ParseNodeCountFromLicenseName(string licenseName)
96+
{
97+
if (string.IsNullOrWhiteSpace(licenseName))
98+
{
99+
return null;
100+
}
101+
102+
// Starting from the beginning of the license name
103+
var startIndex = 0;
104+
// while the start index is less than the length of the string
105+
// and there is a '[' in the name
106+
while (startIndex < licenseName.Length && (startIndex = licenseName.IndexOf('[', startIndex)) > -1)
107+
{
108+
var endIndex = 1;
109+
// increment endIndex (which is actually a relative position) while the next digit is still a digit
110+
while (startIndex + endIndex < licenseName.Length && char.IsDigit(licenseName[startIndex + endIndex]))
111+
{
112+
endIndex++;
113+
}
114+
115+
// If the length of the string is greater or equal than the length of the name, stop!
116+
if (licenseName.Length <= startIndex + endIndex)
117+
{
118+
break;
119+
}
120+
121+
// If next character is ']', return the content within the square braces
122+
if (licenseName[startIndex + endIndex] == ']')
123+
{
124+
return int.Parse(licenseName.Substring(startIndex + 1, endIndex - 1));
125+
}
126+
127+
// If we get this far, start testing at the end of the current range
128+
startIndex = startIndex + endIndex;
129+
}
130+
131+
return null;
132+
133+
}
134+
135+
#pragma warning disable IDE0022, IDE1006
136+
137+
[Obsolete("This overload is deprecated and will be removed in v3.")]
138+
public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyConfiguration configuration)
139+
=> ConfigureArgumentParser(optionSet, configuration);
140+
141+
[Obsolete("This overload is deprecated and will be removed in v3.")]
142+
public virtual void handle_additional_argument_parsing(IList<string> unparsedArguments, ChocolateyConfiguration configuration)
143+
=> ParseAdditionalArguments(unparsedArguments, configuration);
144+
145+
[Obsolete("This overload is deprecated and will be removed in v3.")]
146+
public virtual void handle_validation(ChocolateyConfiguration configuration)
147+
=> Validate(configuration);
148+
149+
[Obsolete("This overload is deprecated and will be removed in v3.")]
150+
public virtual void help_message(ChocolateyConfiguration configuration)
151+
=> HelpMessage(configuration);
152+
153+
[Obsolete("This overload is deprecated and will be removed in v3.")]
154+
public virtual bool may_require_admin_access()
155+
=> MayRequireAdminAccess();
156+
157+
[Obsolete("This overload is deprecated and will be removed in v3.")]
158+
public virtual void noop(ChocolateyConfiguration configuration)
159+
=> DryRun(configuration);
160+
161+
[Obsolete("This overload is deprecated and will be removed in v3.")]
162+
public virtual void run(ChocolateyConfiguration configuration)
163+
=> Run(configuration);
164+
165+
#pragma warning restore IDE0022, IDE1006
166+
}
167+
}

0 commit comments

Comments
 (0)