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