cmd: add new build command

This commit adds the `build` command. It takes the same flags as
`manifest` and will build the specified image.
This commit is contained in:
Michael Vogt 2024-12-03 10:17:47 +01:00 committed by Ondřej Budai
parent ce8dd45821
commit e5b3ccd6ed
5 changed files with 193 additions and 16 deletions

View file

@ -1,6 +1,7 @@
package main
import (
"bytes"
"fmt"
"io"
"os"
@ -9,9 +10,9 @@ import (
"github.com/spf13/cobra"
"github.com/osbuild/images/pkg/arch"
"github.com/osbuild/images/pkg/imagefilter"
"github.com/osbuild/image-builder-cli/internal/blueprintload"
"github.com/osbuild/image-builder-cli/internal/manifestgen"
)
var (
@ -36,21 +37,21 @@ func cmdListImages(cmd *cobra.Command, args []string) error {
return listImages(dataDir, output, filter)
}
func cmdManifest(cmd *cobra.Command, args []string) error {
func cmdManifestWrapper(cmd *cobra.Command, args []string, w io.Writer, archChecker func(string) error) (*imagefilter.Result, error) {
dataDir, err := cmd.Flags().GetString("datadir")
if err != nil {
return err
return nil, err
}
archStr, err := cmd.Flags().GetString("arch")
if err != nil {
return err
return nil, err
}
if archStr == "" {
archStr = arch.Current().String()
}
distroStr, err := cmd.Flags().GetString("distro")
if err != nil {
return err
return nil, err
}
var blueprintPath string
@ -60,30 +61,47 @@ func cmdManifest(cmd *cobra.Command, args []string) error {
}
bp, err := blueprintload.Load(blueprintPath)
if err != nil {
return err
return nil, err
}
distroStr, err = findDistro(distroStr, bp.Distro)
if err != nil {
return err
return nil, err
}
res, err := getOneImage(dataDir, distroStr, imgTypeStr, archStr)
if err != nil {
return err
return nil, err
}
repos, err := newRepoRegistry(dataDir)
if err != nil {
return err
if archChecker != nil {
if err := archChecker(res.Arch.Name()); err != nil {
return nil, err
}
}
// XXX: add --rpmmd/cachedir option like bib
mg, err := manifestgen.New(repos, &manifestgen.Options{
Output: osStdout,
err = generateManifest(dataDir, blueprintPath, res, w)
return res, err
}
func cmdManifest(cmd *cobra.Command, args []string) error {
_, err := cmdManifestWrapper(cmd, args, osStdout, nil)
return err
}
func cmdBuild(cmd *cobra.Command, args []string) error {
var mf bytes.Buffer
// XXX: check env here, i.e. if user is root and osbuild is installed
res, err := cmdManifestWrapper(cmd, args, &mf, func(archStr string) error {
if archStr != arch.Current().String() {
return fmt.Errorf("cannot build for arch %q from %q", archStr, arch.Current().String())
}
return nil
})
if err != nil {
return err
}
return mg.Generate(bp, res.Distro, res.ImgType, res.Arch, nil)
return buildImage(res, mf.Bytes())
}
func run() error {
@ -128,6 +146,16 @@ operating sytsems like centos and RHEL with easy customizations support.`,
manifestCmd.Flags().String("distro", "", `build manifest for a different distroname (e.g. centos-9)`)
rootCmd.AddCommand(manifestCmd)
buildCmd := &cobra.Command{
Use: "build <image-type> [blueprint]",
Short: "Build the given distro/image-type, e.g. centos-9 qcow2",
RunE: cmdBuild,
SilenceUsage: true,
Args: cobra.RangeArgs(1, 2),
}
buildCmd.Flags().AddFlagSet(manifestCmd.Flags())
rootCmd.AddCommand(buildCmd)
return rootCmd.Execute()
}