forked from chocolatey/choco
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(chocolatey#449) Add template command
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
1 parent
212e1dd
commit ca9cfc3
Showing
7 changed files
with
370 additions
and
2 deletions.
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
157 changes: 157 additions & 0 deletions
157
src/chocolatey/infrastructure.app/commands/ChocolateyTemplateCommand.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,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; | ||
} | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
src/chocolatey/infrastructure.app/domain/TemplateCommandType.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,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 | ||
} | ||
} |
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
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
Oops, something went wrong.