Skip to content

Commit

Permalink
Merge #11435
Browse files Browse the repository at this point in the history
11435: Do not fail pulumi new on invalid template(s) r=aq17 a=aq17

Fixes #11434 
If one or more templates are invalid in a repo, we should omit showing those to users instead of failing completely.

Co-authored-by: aq17 <aqiu@pulumi.com>
  • Loading branch information
bors[bot] and aq17 committed Nov 23, 2022
2 parents c2e9ea1 + da17e3f commit c19597a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
23 changes: 21 additions & 2 deletions pkg/cmd/pulumi/new.go
Expand Up @@ -50,6 +50,10 @@ import (
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
)

const (
brokenTemplateDescription = "(This template is currently broken)"
)

type promptForValueFunc func(yes bool, valueType string, defaultValue string, secret bool,
isValidFn func(value string) error, opts display.Options) (string, error)

Expand Down Expand Up @@ -154,6 +158,10 @@ func runNew(ctx context.Context, args newArgs) error {
if template, err = chooseTemplate(templates, opts); err != nil {
return err
}

}
if template.Errored() {
return fmt.Errorf("template '%s' is currently broken: %w", template.Name, template.Error)
}

// Do a dry run, if we're not forcing files to be overwritten.
Expand Down Expand Up @@ -1073,21 +1081,32 @@ func templatesToOptionArrayAndMap(templates []workspace.Template,

// Build the array and map.
var options []string
var brokenOptions []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
}
// If template is broken, indicate it in the project description.
if template.Errored() {
template.ProjectDescription = brokenTemplateDescription
}

// 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)

// Add it to the array and map.
options = append(options, option)
nameToTemplateMap[option] = template
if template.Errored() {
brokenOptions = append(brokenOptions, option)
} else {
options = append(options, option)
}
}
// After sorting the options, add the broken templates to the end
sort.Strings(options)
options = append(options, brokenOptions...)

if !showAll {
// If showAll is false, include an option to show all
Expand Down
12 changes: 11 additions & 1 deletion sdk/go/common/workspace/templates.go
Expand Up @@ -130,7 +130,11 @@ func (repo TemplateRepository) Templates() ([]Template, error) {

template, err := LoadTemplate(filepath.Join(path, name))
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return nil, err
logging.V(2).Infof(
"Failed to load template %s: %s",
name, err.Error(),
)
result = append(result, Template{Name: name, Error: err})
} else if err == nil {
result = append(result, template)
}
Expand Down Expand Up @@ -197,11 +201,17 @@ type Template struct {
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.
Error error // Non-nil if the template is broken.

ProjectName string // Name of the project.
ProjectDescription string // Optional description of the project.
}

// Errored returns if the template has an error
func (t Template) Errored() bool {
return t.Error != nil
}

// PolicyPackTemplate represents a Policy Pack template.
type PolicyPackTemplate struct {
Dir string // The directory containing PulumiPolicy.yaml.
Expand Down

0 comments on commit c19597a

Please sign in to comment.