cmd: add new describe-image command

This commit adds a new `describe-image` comamnd that contains
the details about the given image type. The output is yaml as
it is both nicely human readable and also machine readable.

Note that this version carries an invalid yaml header on
purpose to avoid people replying on the feature for scripts
before it is stable.

The output looks like this:
```yaml
$ ./image-builder describe-image rhel-9.1 tar
@WARNING - the output format is not stable yet and may change
distro: rhel-9.1
type: tar
arch: x86_64
os_vesion: "9.1"
bootmode: none
partition_type: ""
default_filename: root.tar.xz
packages:
  include:
    - policycoreutils
    - selinux-policy-targeted
    - selinux-policy-targeted
  exclude:
    - rng-tools
```

Thanks to Ondrej Budai for the idea and the example.
This commit is contained in:
Michael Vogt 2024-12-12 10:27:01 +01:00 committed by Achilleas Koutsou
parent 2455b9d586
commit 3c2e8dd9af
5 changed files with 235 additions and 3 deletions

View file

@ -202,6 +202,32 @@ func cmdBuild(cmd *cobra.Command, args []string) error {
return buildImage(pbar, res, mf.Bytes(), buildOpts)
}
func cmdDescribeImg(cmd *cobra.Command, args []string) error {
// XXX: boilderplate identical to cmdManifest() above
dataDir, err := cmd.Flags().GetString("datadir")
if err != nil {
return err
}
distroStr, err := cmd.Flags().GetString("distro")
if err != nil {
return err
}
archStr, err := cmd.Flags().GetString("arch")
if err != nil {
return err
}
if archStr == "" {
archStr = arch.Current().String()
}
imgTypeStr := args[0]
res, err := getOneImage(dataDir, distroStr, imgTypeStr, archStr)
if err != nil {
return err
}
return describeImage(res, osStdout)
}
func run() error {
// images generates a lot of noisy logs a bunch of stuff to
// Debug/Info that is distracting the user (at least by
@ -271,6 +297,20 @@ operating systems like Fedora, CentOS and RHEL with easy customizations support.
buildCmd.Flags().String("progress", "auto", "type of progress bar to use (e.g. verbose,term)")
rootCmd.AddCommand(buildCmd)
// XXX: add --format=json too?
describeImgCmd := &cobra.Command{
Use: "describe-image <image-type>",
Short: "Describe the given image-type, e.g. qcow2 (tip: combine with --distro,--arch)",
RunE: cmdDescribeImg,
SilenceUsage: true,
Args: cobra.ExactArgs(1),
Hidden: true,
}
describeImgCmd.Flags().String("arch", "", `use the different architecture`)
describeImgCmd.Flags().String("distro", "", `build manifest for a different distroname (e.g. centos-9)`)
rootCmd.AddCommand(describeImgCmd)
return rootCmd.Execute()
}