@@ -3,6 +3,7 @@ package main
33import (
44 "bytes"
55 "errors"
6+ "fmt"
67 "gopkg.in/yaml.v3"
78 "os"
89 "strings"
@@ -14,13 +15,6 @@ var errRequiredValue = errors.New("required value missing")
1415
1516type Config struct {
1617 Artifacts []Artifact `yaml:"artifacts"`
17- defaults Defaults `yaml:"-"`
18- }
19-
20- type Defaults struct {
21- Destination string `yaml:"destination"`
22- URL string `yaml:"url"`
23- StagingURL string `yaml:"staging_url"`
2418}
2519
2620type Artifact struct {
@@ -63,68 +57,63 @@ func (f File) parseSrc(artifact Artifact) (string, error) {
6357 return renderTemplate (tpl , artifact )
6458}
6559
66- func configFromFile (staging bool , arch string , versions map [string ]string ) (Config , error ) {
60+ func configFromFile (arch string , versions map [string ]string ) (Config , error ) {
6761 // Read the YAML file into a byte slice
6862 yamlFile , err := os .ReadFile ("embedded.yaml" )
6963 if err != nil {
7064 return Config {}, errors .Join (errLoadingConfig , err )
7165 }
72- return config (staging , arch , versions , yamlFile )
66+ return config (arch , versions , yamlFile )
7367}
7468
75- func config (staging bool , arch string , versions map [string ]string , content []byte ) (Config , error ) {
69+ func config (arch string , versions map [string ]string , content []byte ) (Config , error ) {
7670 // Unmarshal YAML into Config struct
7771 var cnf Config
7872 err := yaml .Unmarshal (content , & cnf )
7973 if err != nil {
8074 return Config {}, errors .Join (errLoadingConfig , err )
8175 }
8276
83- // Unmarshal YAML into Config Defaults struct
84- err = yaml .Unmarshal (content , & cnf .defaults )
85- if err != nil {
86- return Config {}, errors .Join (errLoadingConfig , err )
87- }
88-
89- // validate required
90- if cnf .defaults .URL == "" {
91- return Config {}, errors .Join (errRequiredValue , errors .New ("cnf.defaults.URL is missing" ))
92- }
93- if cnf .defaults .Destination == "" {
94- return Config {}, errors .Join (errRequiredValue , errors .New ("cnf.defaults.Destination is missing" ))
95- }
96- if cnf .defaults .StagingURL == "" {
97- return Config {}, errors .Join (errRequiredValue , errors .New ("cnf.defaults.StagingURL is missing" ))
77+ if err := processAndValidateConfig (arch , versions , & cnf ); err != nil {
78+ return Config {}, err
9879 }
9980
100- expandDefaults (staging , arch , versions , & cnf )
101-
10281 return cnf , nil
10382}
10483
105- func expandDefaults (staging bool , arch string , versions map [string ]string , cnf * Config ) {
106- // fill the non specified values with defaults
107- defaultUrl := cnf .defaults .URL
108- if staging {
109- defaultUrl = cnf .defaults .StagingURL
110- }
111-
84+ func processAndValidateConfig (arch string , versions map [string ]string , cnf * Config ) error {
11285 for i := range cnf .Artifacts {
113- if version , ok := versions [cnf .Artifacts [i ].Name ]; ok {
114- cnf .Artifacts [i ].Version = version
86+ artifact := & cnf .Artifacts [i ]
87+
88+ if artifact .Name == "" {
89+ return fmt .Errorf ("artifact at index %d is missing a required 'name'" , i )
11590 }
116- if cnf .Artifacts [i ].URL == "" {
117- cnf .Artifacts [i ].URL = defaultUrl
91+
92+ if artifact .URL == "" {
93+ return fmt .Errorf ("artifact '%s' is missing required field 'url'" , artifact .Name )
11894 }
119- if cnf .Artifacts [i ].Arch == "" {
120- cnf .Artifacts [i ].Arch = arch
95+
96+ artifact .Arch = arch
97+
98+ if v , ok := versions [artifact .Name ]; ok {
99+ artifact .Version = v
100+ } else {
101+ return fmt .Errorf ("version not found for artifact: '%s'" , artifact .Name )
121102 }
122- for j := range cnf .Artifacts [i ].Files {
123- if cnf .Artifacts [i ].Files [j ].Dest == "" {
124- cnf .Artifacts [i ].Files [j ].Dest = cnf .defaults .Destination
103+
104+ for j := range artifact .Files {
105+ file := & artifact .Files [j ]
106+
107+ if file .Src == "" {
108+ return fmt .Errorf ("file '%s' in artifact '%s' is missing a required 'src' field" , file .Name , artifact .Name )
109+ }
110+
111+ if file .Dest == "" {
112+ return fmt .Errorf ("file '%s' in artifact '%s' is missing required field 'dest'" , file .Name , artifact .Name )
125113 }
126114 }
127115 }
116+ return nil
128117}
129118
130119// newTemplate creates a new template and adds the helper trimv function
0 commit comments