github.com/chenbh/concourse/v6@v6.4.2/fly/commands/internal/validatepipelinehelpers/validate.go (about)

     1  package validatepipelinehelpers
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/chenbh/concourse/v6/atc"
     6  
     7  	"github.com/chenbh/concourse/v6/atc/configvalidate"
     8  	"github.com/chenbh/concourse/v6/fly/commands/internal/displayhelpers"
     9  	"github.com/chenbh/concourse/v6/fly/commands/internal/templatehelpers"
    10  	"github.com/chenbh/concourse/v6/go-concourse/concourse"
    11  	"sigs.k8s.io/yaml"
    12  )
    13  
    14  func Validate(yamlTemplate templatehelpers.YamlTemplateWithParams, strict bool, output bool) error {
    15  	evaluatedTemplate, err := yamlTemplate.Evaluate(true, strict)
    16  	if err != nil {
    17  		return err
    18  	}
    19  
    20  	var unmarshalledTemplate atc.Config
    21  	if strict {
    22  		// UnmarshalStrict will pick up fields in structs that have the wrong names, as well as any duplicate keys in maps
    23  		// we should consider always using this everywhere in a later release...
    24  		if err := yaml.UnmarshalStrict([]byte(evaluatedTemplate), &unmarshalledTemplate); err != nil {
    25  			return err
    26  		}
    27  	} else {
    28  		if err := yaml.Unmarshal([]byte(evaluatedTemplate), &unmarshalledTemplate); err != nil {
    29  			return err
    30  		}
    31  	}
    32  
    33  	warnings, errorMessages := configvalidate.Validate(unmarshalledTemplate)
    34  
    35  	if len(warnings) > 0 {
    36  		configWarnings := make([]concourse.ConfigWarning, len(warnings))
    37  		for idx, warning := range warnings {
    38  			configWarnings[idx] = concourse.ConfigWarning(warning)
    39  		}
    40  		displayhelpers.ShowWarnings(configWarnings)
    41  	}
    42  
    43  	if len(errorMessages) > 0 {
    44  		displayhelpers.ShowErrors("Error loading existing config", errorMessages)
    45  	}
    46  
    47  	if len(errorMessages) > 0 || (strict && len(warnings) > 0) {
    48  		displayhelpers.Failf("configuration invalid")
    49  	}
    50  
    51  	if output {
    52  		fmt.Println(string(evaluatedTemplate))
    53  	} else {
    54  		fmt.Println("looks good")
    55  	}
    56  
    57  	return nil
    58  }