Skip to content

Commit

Permalink
(chocolatey#449) Add template command
Browse files Browse the repository at this point in the history
This allows a user to get information about all installed templates, or
a specific template. The list subcommand lists all installed templates.
The info subcommand lists verbose information about a specific template

Usage with -r should output a machine parsable list of the installed
templates. Usage with verbose (implied with the info subcommand)
outputs more information about the template(s).

The information displayed here can be gathered by running the list
command, config command and inspecting the contents of the
TemplatesLocation. However, this command allows the information to be
displayed in one location.
  • Loading branch information
TheCakeIsNaOH committed Dec 27, 2021
1 parent 0ab7c15 commit d06eb98
Show file tree
Hide file tree
Showing 7 changed files with 365 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@
<Link>Properties\SolutionVersion.cs</Link>
</Compile>
<Compile Include="AssemblyExtensions.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyTemplateCommand.cs" />
<Compile Include="infrastructure.app\domain\TemplateCommandType.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyExportCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyInfoCommand.cs" />
<Compile Include="infrastructure.app\commands\ChocolateyHelpCommand.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// 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 System;
using System.Collections.Generic;
using System.Linq;
using attributes;
using commandline;
using configuration;
using domain;
using infrastructure.commands;
using logging;
using services;
using templates;

[CommandFor("template", "get information about installed templates")]
[CommandFor("templates", "get information about installed templates (alias for template)")]
public class ChocolateyTemplateCommand : ICommand
{
private readonly ITemplateService _templateService;

public ChocolateyTemplateCommand(ITemplateService templateService)
{
_templateService = templateService;
}

public void configure_argument_parser(OptionSet optionSet, ChocolateyConfiguration configuration)
{
optionSet
.Add("n=|name=",
"The name of the template to get information about.",
option => configuration.TemplateCommand.Name = option.remove_surrounding_quotes().ToLower());
// TODO Allow for templates from external path? If PR 1477 is merged
}

public virtual void handle_additional_argument_parsing(IList<string> unparsedArguments, ChocolateyConfiguration configuration)
{
// don't set configuration.Input or it will be passed to list

if (unparsedArguments.Count > 1)
{
throw new ApplicationException("A single template command must be listed. Please see the help menu for those commands");
}

var command = TemplateCommandType.unknown;
string unparsedCommand = unparsedArguments.DefaultIfEmpty(string.Empty).FirstOrDefault();
Enum.TryParse(unparsedCommand, true, out command);

if (command == TemplateCommandType.unknown)
{
if (!string.IsNullOrWhiteSpace(unparsedCommand)) this.Log().Warn("Unknown command {0}. Setting to list.".format_with(unparsedCommand));
command = TemplateCommandType.list;
}

configuration.TemplateCommand.Command = command;
}

public virtual void handle_validation(ChocolateyConfiguration configuration)
{
if (configuration.TemplateCommand.Command != TemplateCommandType.list && string.IsNullOrWhiteSpace(configuration.TemplateCommand.Name))
{
throw new ApplicationException("When specifying the subcommand '{0}', you must also specify --name.".format_with(configuration.TemplateCommand.Command.to_string()));
}
}

public virtual void help_message(ChocolateyConfiguration configuration)
{
"chocolatey".Log().Info(ChocolateyLoggers.Important, "Template Command");
"chocolatey".Log().Info(@"
List information installed templates.
Both manually installed templates and templates installed via
.template packages are displayed.");

"chocolatey".Log().Info(ChocolateyLoggers.Important, "Usage");
"chocolatey".Log().Info(@"
choco pin [list]|info [<options/switches>]");

"chocolatey".Log().Info(ChocolateyLoggers.Important, "Examples");
"chocolatey".Log().Info(@"
choco template
choco templates
choco template list
choco template info --name msi
choco template list --reduce-output
choco template list --verbose
NOTE: See scripting in the command reference (`choco -?`) for how to
write proper scripts and integrations.
");

"chocolatey".Log().Info(ChocolateyLoggers.Important, "Exit Codes");
"chocolatey".Log().Info(@"
Exit codes that normally result from running this command.
Normal:
- 0: operation was successful, no issues detected
- -1 or 1: an error has occurred
If you find other exit codes that we have not yet documented, please
file a ticket so we can document it at
https://github.com/chocolatey/choco/issues/new/choose.
");

"chocolatey".Log().Info(ChocolateyLoggers.Important, "Options and Switches");
}

public virtual void noop(ChocolateyConfiguration configuration)
{
switch (configuration.TemplateCommand.Command)
{
case TemplateCommandType.list:
_templateService.list_noop(configuration);
break;
case TemplateCommandType.info:
_templateService.list_noop(configuration);
break;
}
}

public virtual void run(ChocolateyConfiguration configuration)
{
switch (configuration.TemplateCommand.Command)
{
case TemplateCommandType.list:
_templateService.list(configuration);
break;
case TemplateCommandType.info:
configuration.Verbose = true;
_templateService.list(configuration);
break;
}
}

public virtual bool may_require_admin_access()
{
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public ChocolateyConfiguration()
OutdatedCommand = new OutdatedCommandConfiguration();
Proxy = new ProxyConfiguration();
ExportCommand = new ExportCommandConfiguration();
TemplateCommand = new TemplateCommandConfiguration();
#if DEBUG
AllowUnofficialBuild = true;
#endif
Expand Down Expand Up @@ -345,6 +346,14 @@ private void append_output(StringBuilder propertyValues, string append)
/// On .NET 4.0, get error CS0200 when private set - see http://stackoverflow.com/a/23809226/18475
/// </remarks>
public ProxyConfiguration Proxy { get; set; }

/// <summary>
/// Configuration related specifically to Template command
/// </summary>
/// <remarks>
/// On .NET 4.0, get error CS0200 when private set - see http://stackoverflow.com/a/23809226/18475
/// </remarks>
public TemplateCommandConfiguration TemplateCommand { get; set; }
}

[Serializable]
Expand Down Expand Up @@ -557,4 +566,11 @@ public sealed class ExportCommandConfiguration

public string OutputFilePath { get; set; }
}

[Serializable]
public sealed class TemplateCommandConfiguration
{
public TemplateCommandType Command { get; set; }
public string Name { get; set; }
}
}
25 changes: 25 additions & 0 deletions src/chocolatey/infrastructure.app/domain/TemplateCommandType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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.domain
{
public enum TemplateCommandType
{
unknown,
list,
info
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public void RegisterComponents(Container container)
new ChocolateyUnpackSelfCommand(container.GetInstance<IFileSystem>()),
new ChocolateyVersionCommand(container.GetInstance<IChocolateyPackageService>()),
new ChocolateyUpdateCommand(container.GetInstance<IChocolateyPackageService>()),
new ChocolateyExportCommand(container.GetInstance<INugetService>(), container.GetInstance<IFileSystem>())
new ChocolateyExportCommand(container.GetInstance<INugetService>(), container.GetInstance<IFileSystem>()),
new ChocolateyTemplateCommand(container.GetInstance<ITemplateService>())
};
return list.AsReadOnly();
}, Lifestyle.Singleton);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ public interface ITemplateService
{
void generate_noop(ChocolateyConfiguration configuration);
void generate(ChocolateyConfiguration configuration);
void list_noop(ChocolateyConfiguration configuration);
void list(ChocolateyConfiguration configuration);
}
}
Loading

0 comments on commit d06eb98

Please sign in to comment.