Skip to content

Commit

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

Extension of #11435, but for `pulumi policy new`. 
Part of #11434 

Co-authored-by: aq17 <aqiu@pulumi.com>
  • Loading branch information
bors[bot] and aq17 committed Nov 28, 2022
2 parents 377982b + 183ee2e commit 32bea5b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
38 changes: 14 additions & 24 deletions pkg/cmd/pulumi/new.go
Expand Up @@ -823,32 +823,22 @@ func chooseTemplate(templates []workspace.Template, opts display.Options) (works
// Customize the prompt a little bit (and disable color since it doesn't match our scheme).
surveycore.DisableColor = true

var selectedOption workspace.Template

for {
options, optionToTemplateMap := templatesToOptionArrayAndMap(templates, true)
nopts := len(options)
pageSize := optimalPageSize(optimalPageSizeOpts{nopts: nopts})
message := fmt.Sprintf("\rPlease choose a template (%d/%d shown):\n", pageSize, nopts)
message = opts.Color.Colorize(colors.SpecPrompt + message + colors.Reset)

var option string
if err := survey.AskOne(&survey.Select{
Message: message,
Options: options,
PageSize: pageSize,
}, &option, surveyIcons(opts.Color)); err != nil {
return workspace.Template{}, errors.New(chooseTemplateErr)
}

var has bool
selectedOption, has = optionToTemplateMap[option]
if has {
break
}
options, optionToTemplateMap := templatesToOptionArrayAndMap(templates, true)
nopts := len(options)
pageSize := optimalPageSize(optimalPageSizeOpts{nopts: nopts})
message := fmt.Sprintf("\rPlease choose a template (%d/%d shown):\n", pageSize, nopts)
message = opts.Color.Colorize(colors.SpecPrompt + message + colors.Reset)

var option string
if err := survey.AskOne(&survey.Select{
Message: message,
Options: options,
PageSize: pageSize,
}, &option, surveyIcons(opts.Color)); err != nil {
return workspace.Template{}, errors.New(chooseTemplateErr)
}

return selectedOption, nil
return optionToTemplateMap[option], nil
}

// parseConfig parses the config values passed via command line flags.
Expand Down
18 changes: 16 additions & 2 deletions pkg/cmd/pulumi/policy_new.go
Expand Up @@ -149,6 +149,9 @@ func runNewPolicyPack(ctx context.Context, args newPolicyArgs) error {
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.
if !args.force {
Expand Down Expand Up @@ -366,15 +369,26 @@ func policyTemplatesToOptionArrayAndMap(

// Build the array and map.
var options []string
var brokenOptions []string
nameToTemplateMap := make(map[string]workspace.PolicyPackTemplate)
for _, template := range templates {
// If template is broken, indicate it in the project description.
if template.Errored() {
template.Description = brokenTemplateDescription
}

// Create the option string that combines the name, padding, and description.
option := fmt.Sprintf(fmt.Sprintf("%%%ds %%s", -maxNameLength), template.Name, template.Description)

// 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...)
return options, nameToTemplateMap
}
12 changes: 11 additions & 1 deletion sdk/go/common/workspace/templates.go
Expand Up @@ -184,7 +184,11 @@ func (repo TemplateRepository) PolicyTemplates() ([]PolicyPackTemplate, error) {

template, err := LoadPolicyPackTemplate(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, PolicyPackTemplate{Name: name, Error: err})
} else if err == nil {
result = append(result, template)
}
Expand Down Expand Up @@ -217,6 +221,12 @@ type PolicyPackTemplate struct {
Dir string // The directory containing PulumiPolicy.yaml.
Name string // The name of the template.
Description string // Description of the template.
Error error // Non-nil if the template is broken.
}

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

// cleanupLegacyTemplateDir deletes an existing ~/.pulumi/templates directory if it isn't a git repository.
Expand Down

0 comments on commit 32bea5b

Please sign in to comment.