buildx-manager/buildx/load.go
2024-10-05 01:43:59 +02:00

106 lines
1.9 KiB
Go

package buildx
import (
"os"
"github.com/drone/envsubst"
"gopkg.in/yaml.v3"
)
func subst(ref *string) error {
result, err := envsubst.EvalEnv(*ref)
*ref = result
return err
}
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
}
// 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
}