From 0315a97e495ad8a6fd6f715fd415c6bc0a81b40f Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Wed, 8 May 2024 12:57:29 +0200 Subject: [PATCH] cmd: remove duplicated `loadConfig()` and move into buildconfig --- cmd/build/main.go | 23 ++--------- cmd/gen-manifests/main.go | 60 +++++++++++------------------ internal/buildconfig/buildconfig.go | 37 ++++++++++++++++++ internal/manifestutil/manifest.go | 13 ------- 4 files changed, 64 insertions(+), 69 deletions(-) create mode 100644 internal/buildconfig/buildconfig.go delete mode 100644 internal/manifestutil/manifest.go diff --git a/cmd/build/main.go b/cmd/build/main.go index d8f921d75a..9f6180c351 100644 --- a/cmd/build/main.go +++ b/cmd/build/main.go @@ -9,8 +9,8 @@ import ( "path/filepath" "strings" + "github.com/osbuild/images/internal/buildconfig" "github.com/osbuild/images/internal/cmdutil" - "github.com/osbuild/images/internal/manifestutil" "github.com/osbuild/images/pkg/arch" "github.com/osbuild/images/pkg/blueprint" "github.com/osbuild/images/pkg/container" @@ -36,24 +36,8 @@ func check(err error) { } } -func loadConfig(path string) manifestutil.BuildConfig { - fp, err := os.Open(path) - check(err) - defer fp.Close() - - dec := json.NewDecoder(fp) - dec.DisallowUnknownFields() - var conf manifestutil.BuildConfig - - check(dec.Decode(&conf)) - if dec.More() { - fail(fmt.Sprintf("multiple configuration objects or extra data found in %q", path)) - } - return conf -} - func makeManifest( - config manifestutil.BuildConfig, + config *buildconfig.BuildConfig, imgType distro.ImageType, distribution distro.Distro, repos []rpmmd.RepoConfig, @@ -234,7 +218,8 @@ func main() { } distroFac := distrofactory.NewDefault() - config := loadConfig(configFile) + config, err := buildconfig.New(configFile) + check(err) if err := os.MkdirAll(outputDir, 0777); err != nil { fail(fmt.Sprintf("failed to create target directory: %s", err.Error())) diff --git a/cmd/gen-manifests/main.go b/cmd/gen-manifests/main.go index 30b2779bf6..fede29450b 100644 --- a/cmd/gen-manifests/main.go +++ b/cmd/gen-manifests/main.go @@ -17,8 +17,8 @@ import ( "github.com/gobwas/glob" + "github.com/osbuild/images/internal/buildconfig" "github.com/osbuild/images/internal/cmdutil" - "github.com/osbuild/images/internal/manifestutil" "github.com/osbuild/images/pkg/blueprint" "github.com/osbuild/images/pkg/container" "github.com/osbuild/images/pkg/distro" @@ -44,11 +44,11 @@ func (mv *multiValue) Set(v string) error { } type buildRequest struct { - Distro string `json:"distro,omitempty"` - Arch string `json:"arch,omitempty"` - ImageType string `json:"image-type,omitempty"` - Repositories []rpmmd.RepoConfig `json:"repositories,omitempty"` - Config *manifestutil.BuildConfig `json:"config"` + Distro string `json:"distro,omitempty"` + Arch string `json:"arch,omitempty"` + ImageType string `json:"image-type,omitempty"` + Repositories []rpmmd.RepoConfig `json:"repositories,omitempty"` + Config *buildconfig.BuildConfig `json:"config"` } type BuildDependency struct { @@ -58,22 +58,22 @@ type BuildDependency struct { // BuildConfigs is a nested map representing the configs to use for each // distro/arch/image-type. If any component is empty, it maps to all values. -type BuildConfigs map[string]map[string]map[string][]manifestutil.BuildConfig +type BuildConfigs map[string]map[string]map[string][]*buildconfig.BuildConfig -func (bc BuildConfigs) Insert(distro, arch, imageType string, cfg manifestutil.BuildConfig) { +func (bc BuildConfigs) Insert(distro, arch, imageType string, cfg *buildconfig.BuildConfig) { distroCfgs := bc[distro] if distroCfgs == nil { - distroCfgs = make(map[string]map[string][]manifestutil.BuildConfig) + distroCfgs = make(map[string]map[string][]*buildconfig.BuildConfig) } distroArchCfgs := distroCfgs[arch] if distroArchCfgs == nil { - distroArchCfgs = make(map[string][]manifestutil.BuildConfig) + distroArchCfgs = make(map[string][]*buildconfig.BuildConfig) } distroArchItCfgs := distroArchCfgs[imageType] if distroArchItCfgs == nil { - distroArchItCfgs = make([]manifestutil.BuildConfig, 0) + distroArchItCfgs = make([]*buildconfig.BuildConfig, 0) } distroArchItCfgs = append(distroArchItCfgs, cfg) @@ -82,8 +82,8 @@ func (bc BuildConfigs) Insert(distro, arch, imageType string, cfg manifestutil.B bc[distro] = distroCfgs } -func (bc BuildConfigs) Get(distro, arch, imageType string) []manifestutil.BuildConfig { - configs := make([]manifestutil.BuildConfig, 0) +func (bc BuildConfigs) Get(distro, arch, imageType string) []*buildconfig.BuildConfig { + configs := make([]*buildconfig.BuildConfig, 0) for distroName, distroCfgs := range bc { distroGlob := glob.MustCompile(distroName) if distroGlob.Match(distro) { @@ -103,26 +103,6 @@ func (bc BuildConfigs) Get(distro, arch, imageType string) []manifestutil.BuildC return configs } -func loadConfig(path string) manifestutil.BuildConfig { - fp, err := os.Open(path) - if err != nil { - panic(fmt.Sprintf("failed to open config %q: %s", path, err.Error())) - } - defer fp.Close() - - dec := json.NewDecoder(fp) - dec.DisallowUnknownFields() - var conf manifestutil.BuildConfig - - if err := dec.Decode(&conf); err != nil { - panic(fmt.Sprintf("failed to unmarshal config %q: %s", path, err.Error())) - } - if dec.More() { - panic(fmt.Sprintf("multiple configuration objects or extra data found in %q", path)) - } - return conf -} - func loadConfigMap(configPath string) BuildConfigs { type configFilters struct { ImageTypes []string `json:"image-types"` @@ -162,7 +142,10 @@ func loadConfigMap(configPath string) BuildConfigs { cfgDir := filepath.Dir(configPath) path = filepath.Join(cfgDir, path) } - config := loadConfig(path) + config, err := buildconfig.New(path) + if err != nil { + panic(err) + } for _, d := range emptyFallback(filters.Distros) { for _, a := range emptyFallback(filters.Arches) { for _, it := range emptyFallback(filters.ImageTypes) { @@ -180,7 +163,10 @@ func loadConfigMap(configPath string) BuildConfigs { // image types. func loadImgConfig(configPath string) BuildConfigs { cm := make(BuildConfigs) - config := loadConfig(configPath) + config, err := buildconfig.New(configPath) + if err != nil { + panic(err) + } cm.Insert("*", "*", "*", config) return cm } @@ -188,7 +174,7 @@ func loadImgConfig(configPath string) BuildConfigs { type manifestJob func(chan string) error func makeManifestJob( - bc manifestutil.BuildConfig, + bc *buildconfig.BuildConfig, imgType distro.ImageType, distribution distro.Distro, repos []rpmmd.RepoConfig, @@ -279,7 +265,7 @@ func makeManifestJob( Arch: archName, ImageType: imgType.Name(), Repositories: repos, - Config: &bc, + Config: bc, } err = save(mf, packageSpecs, containerSpecs, commitSpecs, request, path, filename, metadata) return diff --git a/internal/buildconfig/buildconfig.go b/internal/buildconfig/buildconfig.go new file mode 100644 index 0000000000..e94e4ae037 --- /dev/null +++ b/internal/buildconfig/buildconfig.go @@ -0,0 +1,37 @@ +package buildconfig + +import ( + "encoding/json" + "fmt" + "os" + + "github.com/osbuild/images/pkg/blueprint" + "github.com/osbuild/images/pkg/distro" +) + +type BuildConfig struct { + Name string `json:"name"` + Blueprint *blueprint.Blueprint `json:"blueprint,omitempty"` + Options distro.ImageOptions `json:"options"` + Depends interface{} `json:"depends,omitempty"` // ignored +} + +func New(path string) (*BuildConfig, error) { + fp, err := os.Open(path) + if err != nil { + return nil, err + } + defer fp.Close() + + dec := json.NewDecoder(fp) + dec.DisallowUnknownFields() + var conf BuildConfig + + if err := dec.Decode(&conf); err != nil { + return nil, err + } + if dec.More() { + return nil, fmt.Errorf("multiple configuration objects or extra data found in %q", path) + } + return &conf, nil +} diff --git a/internal/manifestutil/manifest.go b/internal/manifestutil/manifest.go deleted file mode 100644 index 201406192c..0000000000 --- a/internal/manifestutil/manifest.go +++ /dev/null @@ -1,13 +0,0 @@ -package manifestutil - -import ( - "github.com/osbuild/images/pkg/blueprint" - "github.com/osbuild/images/pkg/distro" -) - -type BuildConfig struct { - Name string `json:"name"` - Blueprint *blueprint.Blueprint `json:"blueprint,omitempty"` - Options distro.ImageOptions `json:"options"` - Depends interface{} `json:"depends,omitempty"` // ignored -}