github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/pkg/packager/creator/template.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package creator contains functions for creating Jackal packages.
     5  package creator
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/Racer159/jackal/src/config"
    11  	"github.com/Racer159/jackal/src/config/lang"
    12  	"github.com/Racer159/jackal/src/pkg/interactive"
    13  	"github.com/Racer159/jackal/src/pkg/utils"
    14  	"github.com/Racer159/jackal/src/types"
    15  )
    16  
    17  // FillActiveTemplate merges user-specified variables into the configuration templates of a jackal.yaml.
    18  func FillActiveTemplate(pkg types.JackalPackage, setVariables map[string]string) (types.JackalPackage, []string, error) {
    19  	templateMap := map[string]string{}
    20  	warnings := []string{}
    21  
    22  	promptAndSetTemplate := func(templatePrefix string, deprecated bool) error {
    23  		yamlTemplates, err := utils.FindYamlTemplates(&pkg, templatePrefix, "###")
    24  		if err != nil {
    25  			return err
    26  		}
    27  
    28  		for key := range yamlTemplates {
    29  			if deprecated {
    30  				warnings = append(warnings, fmt.Sprintf(lang.PkgValidateTemplateDeprecation, key, key, key))
    31  			}
    32  
    33  			_, present := setVariables[key]
    34  			if !present && !config.CommonOptions.Confirm {
    35  				setVal, err := interactive.PromptVariable(types.JackalPackageVariable{
    36  					Name: key,
    37  				})
    38  				if err != nil {
    39  					return err
    40  				}
    41  				setVariables[key] = setVal
    42  			} else if !present {
    43  				return fmt.Errorf("template %q must be '--set' when using the '--confirm' flag", key)
    44  			}
    45  		}
    46  
    47  		for key, value := range setVariables {
    48  			templateMap[fmt.Sprintf("%s%s###", templatePrefix, key)] = value
    49  		}
    50  
    51  		return nil
    52  	}
    53  
    54  	// update the component templates on the package
    55  	if err := ReloadComponentTemplatesInPackage(&pkg); err != nil {
    56  		return types.JackalPackage{}, nil, err
    57  	}
    58  
    59  	if err := promptAndSetTemplate(types.JackalPackageTemplatePrefix, false); err != nil {
    60  		return types.JackalPackage{}, nil, err
    61  	}
    62  	// [DEPRECATION] Set the Package Variable syntax as well for backward compatibility
    63  	if err := promptAndSetTemplate(types.JackalPackageVariablePrefix, true); err != nil {
    64  		return types.JackalPackage{}, nil, err
    65  	}
    66  
    67  	// Add special variable for the current package architecture
    68  	templateMap[types.JackalPackageArch] = pkg.Metadata.Architecture
    69  
    70  	if err := utils.ReloadYamlTemplate(&pkg, templateMap); err != nil {
    71  		return types.JackalPackage{}, nil, err
    72  	}
    73  
    74  	return pkg, warnings, nil
    75  }
    76  
    77  // ReloadComponentTemplate appends ###JACKAL_COMPONENT_NAME### for the component, assigns value, and reloads
    78  // Any instance of ###JACKAL_COMPONENT_NAME### within a component will be replaced with that components name
    79  func ReloadComponentTemplate(component *types.JackalComponent) error {
    80  	mappings := map[string]string{}
    81  	mappings[types.JackalComponentName] = component.Name
    82  	err := utils.ReloadYamlTemplate(component, mappings)
    83  	if err != nil {
    84  		return err
    85  	}
    86  	return nil
    87  }
    88  
    89  // ReloadComponentTemplatesInPackage appends ###JACKAL_COMPONENT_NAME###  for each component, assigns value, and reloads
    90  func ReloadComponentTemplatesInPackage(jackalPackage *types.JackalPackage) error {
    91  	// iterate through components to and find all ###JACKAL_COMPONENT_NAME, assign to component Name and value
    92  	for i := range jackalPackage.Components {
    93  		if err := ReloadComponentTemplate(&jackalPackage.Components[i]); err != nil {
    94  			return err
    95  		}
    96  	}
    97  
    98  	return nil
    99  }