buildx-manager/buildx/load.go

138 lines
2.6 KiB
Go
Raw Permalink Normal View History

2024-10-04 23:43:59 +00:00
package buildx
import (
2024-10-05 09:39:40 +00:00
"errors"
2024-10-04 23:43:59 +00:00
"os"
"github.com/drone/envsubst"
"gopkg.in/yaml.v3"
)
func subst(ref *string) error {
result, err := envsubst.EvalEnv(*ref)
*ref = result
return err
}
2024-10-05 09:39:40 +00:00
func defaultValue(ref *string, value string) {
if *ref == "" {
*ref = value
}
}
2024-10-04 23:43:59 +00:00
func LoadJob(path string) (*Job, error) {
var job Job
file, err := os.ReadFile(path)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(file, &job)
if err != nil {
return nil, err
}
2024-10-05 09:39:40 +00:00
// check for empty fields
// registry
if job.Registries == nil {
job.Registries = []string{"docker.io"}
}
// repos
for i, repo := range job.Repositories {
if repo.Name == "" {
return nil, errors.New("missing repository name")
}
if repo.Library == "" {
return nil, errors.New("missing library")
}
defaultValue(&job.Repositories[i].Path, repo.Name)
// builds
for j := range job.Repositories[i].Builds {
defaultValue(&job.Repositories[i].Builds[j].Context, ".")
defaultValue(&job.Repositories[i].Builds[j].Dockerfile, "Dockerfile")
}
}
2024-10-04 23:43:59 +00:00
// envsubst
// registries
for i := range job.Registries {
if err = subst(&job.Registries[i]); err != nil {
return nil, err
}
}
// repos
for i := range job.Repositories {
// name
if err = subst(&job.Repositories[i].Name); err != nil {
return nil, err
}
// library
if err = subst(&job.Repositories[i].Library); err != nil {
return nil, err
}
// path
if err = subst(&job.Repositories[i].Path); err != nil {
return nil, err
}
// global args
for j := range job.Repositories[i].GlobalArguments {
// key
if err = subst(&job.Repositories[i].GlobalArguments[j].Key); err != nil {
return nil, err
}
// value
if err = subst(&job.Repositories[i].GlobalArguments[j].Value); err != nil {
return nil, err
}
}
// tags
for j := range job.Repositories[i].Tags {
// name
if err = subst(&job.Repositories[i].Tags[j].Name); err != nil {
return nil, err
}
// tags
for u := range job.Repositories[i].Tags[j].Tags {
if err = subst(&job.Repositories[i].Tags[j].Tags[u]); err != nil {
return nil, err
}
}
}
// builds
for j := range job.Repositories[i].Builds {
// arch
if err = subst(&job.Repositories[i].Builds[j].Architecture); err != nil {
return nil, err
}
// dockerfile
if err = subst(&job.Repositories[i].Builds[j].Dockerfile); err != nil {
return nil, err
}
// tags
for u := range job.Repositories[i].Builds[j].Tags {
if err = subst(&job.Repositories[i].Builds[j].Tags[u]); err != nil {
return nil, err
}
}
}
}
return &job, err
}