Filter the list of templates shown by default (#3147)

If any templates are marked as `Important: true` then by default show only those templates along with an option to see additional templates.

Fixes #3094.
This commit is contained in:
Luke Hoban 2019-08-27 17:56:49 -07:00 committed by GitHub
parent 47dc3cfb8b
commit f0a24079ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 11 deletions

View file

@ -6,10 +6,14 @@ CHANGELOG
- Print a Welcome to Pulumi message for users during interactive logins to the Pulumi CLI.
[#3145](https://github.com/pulumi/pulumi/pull/3145)
- Filter the list of templates shown by default during `pulumi new`.
[#3147](https://github.com/pulumi/pulumi/pull/3147)
## 1.0.0-beta.4 (2019-08-22)
- Fix a crash when using StackReference from the `1.0.0-beta.3` version of
`@pulumi/pulumi` and `1.0.0-beta.2` or earlier of the CLI.
- Allow Un/MashalProperties to reject Asset and AssetArchive types. (partial fix
for https://github.com/pulumi/pulumi-kubernetes/issues/737)

View file

@ -356,7 +356,7 @@ func newNewCmd() *cobra.Command {
// If we have any templates, show them.
if len(templates) > 0 {
available, _ := templatesToOptionArrayAndMap(templates)
available, _ := templatesToOptionArrayAndMap(templates, true)
fmt.Println("")
fmt.Println("Available Templates:")
for _, t := range available {
@ -660,18 +660,39 @@ func chooseTemplate(templates []workspace.Template, opts display.Options) (works
message := "\rPlease choose a template:"
message = opts.Color.Colorize(colors.SpecPrompt + message + colors.Reset)
options, optionToTemplateMap := templatesToOptionArrayAndMap(templates)
showAll := false
var selectedOption workspace.Template
var option string
if err := survey.AskOne(&survey.Select{
Message: message,
Options: options,
PageSize: len(options),
}, &option, nil); err != nil {
return workspace.Template{}, errors.New(chooseTemplateErr)
for {
options, optionToTemplateMap := templatesToOptionArrayAndMap(templates, showAll)
// If showAll was false and we got only a single result, force showAll to be true and try
// again.
if !showAll && len(options) <= 1 {
showAll = true
continue
}
var option string
if err := survey.AskOne(&survey.Select{
Message: message,
Options: options,
PageSize: len(options),
}, &option, nil); err != nil {
return workspace.Template{}, errors.New(chooseTemplateErr)
}
var has bool
selectedOption, has = optionToTemplateMap[option]
if has {
break
} else {
showAll = true
}
}
return optionToTemplateMap[option], nil
return selectedOption, nil
}
// parseConfig parses the config values passed via command line flags.
@ -879,7 +900,9 @@ func promptForValue(
// templatesToOptionArrayAndMap returns an array of option strings and a map of option strings to templates.
// Each option string is made up of the template name and description with some padding in between.
func templatesToOptionArrayAndMap(templates []workspace.Template) ([]string, map[string]workspace.Template) {
func templatesToOptionArrayAndMap(templates []workspace.Template,
showAll bool) ([]string, map[string]workspace.Template) {
// Find the longest name length. Used to add padding between the name and description.
maxNameLength := 0
for _, template := range templates {
@ -892,6 +915,10 @@ func templatesToOptionArrayAndMap(templates []workspace.Template) ([]string, map
var options []string
nameToTemplateMap := make(map[string]workspace.Template)
for _, template := range templates {
// If showAll is false, then only include templates marked Important
if !showAll && !template.Important {
continue
}
// Create the option string that combines the name, padding, and description.
desc := workspace.ValueOrDefaultProjectDescription("", template.ProjectDescription, template.Description)
option := fmt.Sprintf(fmt.Sprintf("%%%ds %%s", -maxNameLength), template.Name, desc)
@ -902,6 +929,12 @@ func templatesToOptionArrayAndMap(templates []workspace.Template) ([]string, map
}
sort.Strings(options)
if !showAll {
// If showAll is false, include an option to show all
option := "Show additional templates"
options = append(options, option)
}
return options, nameToTemplateMap
}

View file

@ -40,6 +40,8 @@ type ProjectTemplate struct {
Quickstart string `json:"quickstart,omitempty" yaml:"quickstart,omitempty"`
// Config is an optional template config.
Config map[string]ProjectTemplateConfigValue `json:"config,omitempty" yaml:"config,omitempty"`
// Important indicates the template is important and should be listed by default.
Important bool `json:"important,omitempty" yaml:"important,omitempty"`
}
// ProjectTemplateConfigValue is a config value included in the project template manifest.

View file

@ -119,6 +119,7 @@ type Template struct {
Description string // Description of the template.
Quickstart string // Optional text to be displayed after template creation.
Config map[string]ProjectTemplateConfigValue // Optional template config.
Important bool // Indicates whether the template should be listed by default.
ProjectName string // Name of the project.
ProjectDescription string // Optional description of the project.
@ -314,6 +315,7 @@ func LoadTemplate(path string) (Template, error) {
template.Description = proj.Template.Description
template.Quickstart = proj.Template.Quickstart
template.Config = proj.Template.Config
template.Important = proj.Template.Important
}
if proj.Description != nil {
template.ProjectDescription = *proj.Description