MCPcopy Index your code
hub / github.com/coder/coder / handleMissingTemplateVariables

Function handleMissingTemplateVariables

cli/templatepush.go:537–599  ·  view source on GitHub ↗
(inv *serpent.Invocation, args createValidTemplateVersionArgs, failedVersionID uuid.UUID)

Source from the content-addressed store, hash-verified

535}
536
537func handleMissingTemplateVariables(inv *serpent.Invocation, args createValidTemplateVersionArgs, failedVersionID uuid.UUID) (*codersdk.TemplateVersion, error) {
538 client := args.Client
539
540 templateVariables, err := client.TemplateVersionVariables(inv.Context(), failedVersionID)
541 if err != nil {
542 return nil, xerrors.Errorf("fetch template variables: %w", err)
543 }
544
545 existingValues := make(map[string]string)
546 for _, v := range args.UserVariableValues {
547 existingValues[v.Name] = v.Value
548 }
549
550 var missingVariables []codersdk.TemplateVersionVariable
551 for _, variable := range templateVariables {
552 if !variable.Required {
553 continue
554 }
555
556 if existingValue, exists := existingValues[variable.Name]; exists && existingValue != "" {
557 continue
558 }
559
560 // Only prompt for variables that don't have a default value or have a redacted default
561 // Sensitive variables have a default value of "*redacted*"
562 // See: https://github.com/coder/coder/blob/a78790c632974e04babfef6de0e2ddf044787a7a/coderd/provisionerdserver/provisionerdserver.go#L3206
563 if variable.DefaultValue == "" || (variable.Sensitive && variable.DefaultValue == "*redacted*") {
564 missingVariables = append(missingVariables, variable)
565 }
566 }
567
568 if len(missingVariables) == 0 {
569 return nil, xerrors.New("no missing required variables found")
570 }
571
572 _, _ = fmt.Fprintf(inv.Stderr, "Found %d missing required variables:\n", len(missingVariables))
573 for _, v := range missingVariables {
574 _, _ = fmt.Fprintf(inv.Stderr, " - %s (%s): %s\n", v.Name, v.Type, v.Description)
575 }
576
577 _, _ = fmt.Fprintln(inv.Stderr, "\nThe template requires values for the following variables:")
578
579 var promptedValues []codersdk.VariableValue
580 for _, variable := range missingVariables {
581 value, err := promptForTemplateVariable(inv, variable)
582 if err != nil {
583 return nil, xerrors.Errorf("prompt for variable %q: %w", variable.Name, err)
584 }
585 promptedValues = append(promptedValues, codersdk.VariableValue{
586 Name: variable.Name,
587 Value: value,
588 })
589 }
590
591 combinedValues := codersdk.CombineVariableValues(args.UserVariableValues, promptedValues)
592
593 _, _ = fmt.Fprintln(inv.Stderr, "\nRetrying template build with provided variables...")
594

Callers 1

Calls 7

CombineVariableValuesFunction · 0.92
ContextMethod · 0.65
NewMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected