This repository was archived by the owner on Sep 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
cmd: add new describe-image command
#9
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "slices" | ||
|
|
||
| "gopkg.in/yaml.v3" | ||
|
|
||
| "github.com/osbuild/images/pkg/blueprint" | ||
| "github.com/osbuild/images/pkg/distro" | ||
| "github.com/osbuild/images/pkg/imagefilter" | ||
| ) | ||
|
|
||
| // Use yaml output by default because it is both nicely human and | ||
| // machine readable and parts of our image defintions will be written | ||
| // in yaml too. This means this should be a possible input a | ||
| // "flattended" image definiton. | ||
| type describeImgYAML struct { | ||
| Distro string `yaml:"distro"` | ||
| Type string `yaml:"type"` | ||
| Arch string `yaml:"arch"` | ||
|
|
||
| // XXX: think about ordering (as this is what the user will see) | ||
| OsVersion string `yaml:"os_vesion"` | ||
|
|
||
| Bootmode string `yaml:"bootmode"` | ||
| PartitionType string `yaml:"partition_type"` | ||
| DefaultFilename string `yaml:"default_filename"` | ||
|
|
||
| BuildPipelines []string `yaml:"build_pipelines"` | ||
| PayloadPipelines []string `yaml:"payload_pipelines"` | ||
| Packages map[string]*packagesYAML `yaml:"packages"` | ||
| } | ||
|
|
||
| type packagesYAML struct { | ||
| Include []string `yaml:"include"` | ||
| Exclude []string `yaml:"exclude"` | ||
| } | ||
|
|
||
| func packageSetsFor(imgType distro.ImageType) (map[string]*packagesYAML, error) { | ||
| var bp blueprint.Blueprint | ||
| manifest, _, err := imgType.Manifest(&bp, distro.ImageOptions{}, nil, nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| res := make(map[string]*packagesYAML) | ||
|
|
||
| for pipelineName, pkgSets := range manifest.GetPackageSetChains() { | ||
| incM := map[string]bool{} | ||
| excM := map[string]bool{} | ||
| for _, pkgSet := range pkgSets { | ||
| for _, s := range pkgSet.Include { | ||
| incM[s] = true | ||
| } | ||
| for _, s := range pkgSet.Exclude { | ||
| excM[s] = true | ||
| } | ||
| } | ||
| inc := make([]string, 0, len(incM)) | ||
| exc := make([]string, 0, len(excM)) | ||
| for name := range incM { | ||
| inc = append(inc, name) | ||
| } | ||
| for name := range excM { | ||
| exc = append(exc, name) | ||
| } | ||
| slices.Sort(inc) | ||
| slices.Sort(exc) | ||
|
|
||
| res[pipelineName] = &packagesYAML{ | ||
| Include: inc, | ||
| Exclude: exc, | ||
| } | ||
| } | ||
| return res, nil | ||
| } | ||
|
|
||
| // XXX: should this live in images instead? | ||
| func describeImage(img *imagefilter.Result, out io.Writer) error { | ||
| // see | ||
| // https://github.com/osbuild/images/pull/1019#discussion_r1832376568 | ||
| // for what is available on an image (without depsolve or partitioning) | ||
| pkgSets, err := packageSetsFor(img.ImgType) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| outYaml := &describeImgYAML{ | ||
| Distro: img.Distro.Name(), | ||
| OsVersion: img.Distro.OsVersion(), | ||
| Arch: img.Arch.Name(), | ||
| Type: img.ImgType.Name(), | ||
| Bootmode: img.ImgType.BootMode().String(), | ||
| PartitionType: img.ImgType.PartitionType().String(), | ||
| DefaultFilename: img.ImgType.Filename(), | ||
| BuildPipelines: img.ImgType.BuildPipelines(), | ||
| PayloadPipelines: img.ImgType.PayloadPipelines(), | ||
| Packages: pkgSets, | ||
| } | ||
| // deliberately break the yaml until the feature is stable | ||
| fmt.Fprint(out, "@WARNING - the output format is not stable yet and may change\n") | ||
| enc := yaml.NewEncoder(out) | ||
| enc.SetIndent(2) | ||
| return enc.Encode(outYaml) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package main_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| testrepos "github.com/osbuild/images/test/data/repositories" | ||
|
|
||
| "github.com/osbuild/image-builder-cli/cmd/image-builder" | ||
| ) | ||
|
|
||
| func TestDescribeImage(t *testing.T) { | ||
| restore := main.MockNewRepoRegistry(testrepos.New) | ||
| defer restore() | ||
|
|
||
| res, err := main.GetOneImage("", "centos-9", "tar", "x86_64") | ||
| assert.NoError(t, err) | ||
|
|
||
| var buf bytes.Buffer | ||
| err = main.DescribeImage(res, &buf) | ||
| assert.NoError(t, err) | ||
|
|
||
| expectedOutput := `@WARNING - the output format is not stable yet and may change | ||
| distro: centos-9 | ||
| type: tar | ||
| arch: x86_64 | ||
| os_vesion: 9-stream | ||
| bootmode: none | ||
| partition_type: "" | ||
| default_filename: root.tar.xz | ||
| build_pipelines: | ||
| - build | ||
| payload_pipelines: | ||
| - os | ||
| - archive | ||
| packages: | ||
| build: | ||
| include: | ||
| - coreutils | ||
| - glibc | ||
| - platform-python | ||
| - policycoreutils | ||
| - python3 | ||
| - rpm | ||
| - selinux-policy-targeted | ||
| - systemd | ||
| - tar | ||
| - xz | ||
| exclude: [] | ||
| os: | ||
| include: | ||
| - policycoreutils | ||
| - selinux-policy-targeted | ||
| exclude: | ||
| - rng-tools | ||
| ` | ||
| assert.Equal(t, expectedOutput, buf.String()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.