From cf92ebf3897e801df7b8ddebb988daf69fb63367 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Wed, 13 Nov 2024 18:29:58 +0100 Subject: [PATCH] image-builder: add `ib build --filename foo.img` support --- cmd/image-builder/build.go | 7 +++++-- cmd/image-builder/main.go | 12 +++++++++--- cmd/image-builder/manifest.go | 20 ++++++++++++++------ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/cmd/image-builder/build.go b/cmd/image-builder/build.go index ea1b081502..038f6c3772 100644 --- a/cmd/image-builder/build.go +++ b/cmd/image-builder/build.go @@ -11,7 +11,7 @@ import ( "github.com/osbuild/images/pkg/osbuild" ) -func buildImage(out io.Writer, distroName, imgTypeStr string) error { +func buildImage(out io.Writer, distroName, imgTypeStr, outputFilename string) error { // cross arch building is not possible, we would have to download // a pre-populated buildroot (tar,container) with rpm for that archStr := arch.Current().String() @@ -22,7 +22,10 @@ func buildImage(out io.Writer, distroName, imgTypeStr string) error { imgType := filterResult.ImgType var mf bytes.Buffer - if err := outputManifest(&mf, distroName, imgTypeStr, archStr); err != nil { + opts := &genManifestOptions{ + OutputFilename: outputFilename, + } + if err := outputManifest(&mf, distroName, imgTypeStr, archStr, opts); err != nil { return err } diff --git a/cmd/image-builder/main.go b/cmd/image-builder/main.go index b06d92f19d..3fe5d460cf 100644 --- a/cmd/image-builder/main.go +++ b/cmd/image-builder/main.go @@ -38,15 +38,19 @@ func cmdManifest(cmd *cobra.Command, args []string) error { archStr = arch.Current().String() } - return outputManifest(osStdout, distroName, imgType, archStr) + return outputManifest(osStdout, distroName, imgType, archStr, nil) } func cmdBuild(cmd *cobra.Command, args []string) error { // support prefixes to make it easy to copy/paste from list-images distroName := strings.TrimPrefix(args[0], "distro:") imgType := strings.TrimPrefix(args[1], "type:") + outputFilename, err := cmd.Flags().GetString("filename") + if err != nil { + return err + } - return buildImage(osStdout, distroName, imgType) + return buildImage(osStdout, distroName, imgType, outputFilename) } func run() error { @@ -95,8 +99,10 @@ operating sytsems like centos and RHEL with easy customizations support.`, // XXX: show error with available types if only one arg given Args: cobra.ExactArgs(2), } - rootCmd.AddCommand(buildCmd) + // XXX: add this for "manifest" too in a nice way + buildCmd.Flags().String("filename", "", "Output as a specific filename") // XXX: add --output=text,json and streaming + rootCmd.AddCommand(buildCmd) return rootCmd.Execute() } diff --git a/cmd/image-builder/manifest.go b/cmd/image-builder/manifest.go index b2d55a6e54..b597e09512 100644 --- a/cmd/image-builder/manifest.go +++ b/cmd/image-builder/manifest.go @@ -29,15 +29,20 @@ func depsolve(cacheDir string, packageSets map[string][]rpmmd.PackageSet, d dist return depsolvedSets, repoSets, nil } -func outputManifest(out io.Writer, distroName, imgTypeStr, archStr string) error { - // XXX: what/how much do we expose here? - var options distro.ImageOptions +type genManifestOptions struct { + OutputFilename string +} + +func outputManifest(out io.Writer, distroName, imgTypeStr, archStr string, opts *genManifestOptions) error { + if opts == nil { + opts = &genManifestOptions{} + } filterResult, err := getOneImage(distroName, imgTypeStr, archStr) if err != nil { return err } - distro := filterResult.Distro + dist := filterResult.Distro imgType := filterResult.ImgType reporeg, err := newRepoRegistry() @@ -50,7 +55,10 @@ func outputManifest(out io.Writer, distroName, imgTypeStr, archStr string) error } var bp blueprint.Blueprint - preManifest, warnings, err := imgType.Manifest(&bp, options, repos, 0) + imgOpts := distro.ImageOptions{ + OutputFilename: opts.OutputFilename, + } + preManifest, warnings, err := imgType.Manifest(&bp, imgOpts, repos, 0) if err != nil { return err } @@ -64,7 +72,7 @@ func outputManifest(out io.Writer, distroName, imgTypeStr, archStr string) error if err != nil { return err } - packageSpecs, _, err := depsolve(cacheDir, preManifest.GetPackageSetChains(), distro, archStr) + packageSpecs, _, err := depsolve(cacheDir, preManifest.GetPackageSetChains(), dist, archStr) if err != nil { return err }