Skip to content

Commit

Permalink
cmd: remove (some) duplicaton from makeManifest{,Job}
Browse files Browse the repository at this point in the history
We currently duplicate `BuildConfig` in `cmd/build` and
`cmd/gen-manifest`. Start by moving it into a shared package.
  • Loading branch information
mvo5 committed May 8, 2024
1 parent 2ae8c07 commit 4ee472f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 36 deletions.
24 changes: 13 additions & 11 deletions cmd/build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"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"
Expand All @@ -35,21 +36,14 @@ func check(err error) {
}
}

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 loadConfig(path string) BuildConfig {
func loadConfig(path string) manifestutil.BuildConfig {
fp, err := os.Open(path)
check(err)
defer fp.Close()

dec := json.NewDecoder(fp)
dec.DisallowUnknownFields()
var conf BuildConfig
var conf manifestutil.BuildConfig

check(dec.Decode(&conf))
if dec.More() {
Expand All @@ -58,7 +52,15 @@ func loadConfig(path string) BuildConfig {
return conf
}

func makeManifest(imgType distro.ImageType, config BuildConfig, distribution distro.Distro, repos []rpmmd.RepoConfig, archName string, seedArg int64, cacheRoot string) (manifest.OSBuildManifest, error) {
func makeManifest(
config manifestutil.BuildConfig,
imgType distro.ImageType,
distribution distro.Distro,
repos []rpmmd.RepoConfig,
archName string,
seedArg int64,
cacheRoot string,
) (manifest.OSBuildManifest, error) {
cacheDir := filepath.Join(cacheRoot, archName+distribution.Name())

options := config.Options
Expand Down Expand Up @@ -271,7 +273,7 @@ func main() {
}

fmt.Printf("Generating manifest for %s: ", config.Name)
mf, err := makeManifest(imgType, config, distribution, repos, archName, rngSeed, rpmCacheRoot)
mf, err := makeManifest(config, imgType, distribution, repos, archName, rngSeed, rpmCacheRoot)
check(err)
fmt.Print("DONE\n")

Expand Down
44 changes: 19 additions & 25 deletions cmd/gen-manifests/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/gobwas/glob"

"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"
Expand All @@ -43,18 +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 *BuildConfig `json:"config"`
}

type BuildConfig struct {
Name string `json:"name"`
Blueprint *blueprint.Blueprint `json:"blueprint,omitempty"`
Options distro.ImageOptions `json:"options"`
Depends interface{} `json:"depends,omitempty"` // ignored
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"`
}

type BuildDependency struct {
Expand All @@ -64,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][]BuildConfig
type BuildConfigs map[string]map[string]map[string][]manifestutil.BuildConfig

func (bc BuildConfigs) Insert(distro, arch, imageType string, cfg BuildConfig) {
func (bc BuildConfigs) Insert(distro, arch, imageType string, cfg manifestutil.BuildConfig) {
distroCfgs := bc[distro]
if distroCfgs == nil {
distroCfgs = make(map[string]map[string][]BuildConfig)
distroCfgs = make(map[string]map[string][]manifestutil.BuildConfig)
}

distroArchCfgs := distroCfgs[arch]
if distroArchCfgs == nil {
distroArchCfgs = make(map[string][]BuildConfig)
distroArchCfgs = make(map[string][]manifestutil.BuildConfig)
}

distroArchItCfgs := distroArchCfgs[imageType]
if distroArchItCfgs == nil {
distroArchItCfgs = make([]BuildConfig, 0)
distroArchItCfgs = make([]manifestutil.BuildConfig, 0)
}

distroArchItCfgs = append(distroArchItCfgs, cfg)
Expand All @@ -88,8 +82,8 @@ func (bc BuildConfigs) Insert(distro, arch, imageType string, cfg BuildConfig) {
bc[distro] = distroCfgs
}

func (bc BuildConfigs) Get(distro, arch, imageType string) []BuildConfig {
configs := make([]BuildConfig, 0)
func (bc BuildConfigs) Get(distro, arch, imageType string) []manifestutil.BuildConfig {
configs := make([]manifestutil.BuildConfig, 0)
for distroName, distroCfgs := range bc {
distroGlob := glob.MustCompile(distroName)
if distroGlob.Match(distro) {
Expand All @@ -109,7 +103,7 @@ func (bc BuildConfigs) Get(distro, arch, imageType string) []BuildConfig {
return configs
}

func loadConfig(path string) BuildConfig {
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()))
Expand All @@ -118,7 +112,7 @@ func loadConfig(path string) BuildConfig {

dec := json.NewDecoder(fp)
dec.DisallowUnknownFields()
var conf BuildConfig
var conf manifestutil.BuildConfig

if err := dec.Decode(&conf); err != nil {
panic(fmt.Sprintf("failed to unmarshal config %q: %s", path, err.Error()))
Expand Down Expand Up @@ -194,18 +188,18 @@ func loadImgConfig(configPath string) BuildConfigs {
type manifestJob func(chan string) error

func makeManifestJob(
name string,
bc manifestutil.BuildConfig,
imgType distro.ImageType,
bc BuildConfig,
distribution distro.Distro,
repos []rpmmd.RepoConfig,
archName string,
seedArg int64,
path string,
cacheRoot string,
path string,
content map[string]bool,
metadata bool,
) manifestJob {
name := bc.Name
distroName := distribution.Name()
filename := fmt.Sprintf("%s-%s-%s-%s.json", u(distroName), u(archName), u(imgType.Name()), u(name))
cacheDir := filepath.Join(cacheRoot, archName+distribution.Name())
Expand Down Expand Up @@ -618,7 +612,7 @@ func main() {
}

for _, itConfig := range imgTypeConfigs {
job := makeManifestJob(itConfig.Name, imgType, itConfig, distribution, repos, archName, rngSeed, outputDir, cacheRoot, contentResolve, metadata)
job := makeManifestJob(itConfig, imgType, distribution, repos, archName, rngSeed, cacheRoot, outputDir, contentResolve, metadata)
jobs = append(jobs, job)
}
}
Expand Down
13 changes: 13 additions & 0 deletions internal/manifestutil/manifest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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
}

0 comments on commit 4ee472f

Please sign in to comment.