Skip to content

Commit

Permalink
osbuild: simplify containers_input.go
Browse files Browse the repository at this point in the history
While writing tests to ensure the that the `ContainerDeployInputs`
generates the correct json I noticed that this was more involved
than I had hoped and it seems there are some indirections in the
code that may not be necessary. I removed them and IMHHO the code
is now a bit more direct and easier to read.
  • Loading branch information
mvo5 authored and supakeen committed Jan 5, 2024
1 parent cbf1322 commit dae9496
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pkg/osbuild/container_deploy_stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func (inputs ContainerDeployInputs) validate() error {
if inputs.Images.References == nil {
return fmt.Errorf("stage requires exactly 1 input container (got nil References)")
}
if ncontainers := inputs.Images.References.Len(); ncontainers != 1 {
if ncontainers := len(inputs.Images.References); ncontainers != 1 {
return fmt.Errorf("stage requires exactly 1 input container (got %d)", ncontainers)
}
return nil
Expand Down
21 changes: 3 additions & 18 deletions pkg/osbuild/containers_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,17 @@ import (
"github.com/osbuild/images/pkg/container"
)

type ContainersInputReferences interface {
isContainersInputReferences()
Len() int
}

type ContainersInputSourceRef struct {
Name string `json:"name"`
}

type ContainersInputSourceMap map[string]ContainersInputSourceRef

func (ContainersInputSourceMap) isContainersInputReferences() {}

func (cism ContainersInputSourceMap) Len() int {
return len(cism)
}

type ContainersInput struct {
inputCommon
References ContainersInputReferences `json:"references"`
References map[string]ContainersInputSourceRef `json:"references"`
}

const InputTypeContainers string = "org.osbuild.containers"

func NewContainersInputForSources(containers []container.Spec) ContainersInput {
refs := make(ContainersInputSourceMap, len(containers))
refs := make(map[string]ContainersInputSourceRef, len(containers))
for _, c := range containers {
ref := ContainersInputSourceRef{
Name: c.LocalName,
Expand All @@ -40,7 +25,7 @@ func NewContainersInputForSources(containers []container.Spec) ContainersInput {
return ContainersInput{
References: refs,
inputCommon: inputCommon{
Type: InputTypeContainers,
Type: "org.osbuild.containers",
Origin: InputOriginSource,
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/osbuild/ostree_deploy_container_stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (inputs OSTreeDeployContainerInputs) validate() error {
if inputs.Images.References == nil {
return fmt.Errorf("stage requires exactly 1 input container (got nil References)")
}
if ncontainers := inputs.Images.References.Len(); ncontainers != 1 {
if ncontainers := len(inputs.Images.References); ncontainers != 1 {
return fmt.Errorf("stage requires exactly 1 input container (got %d)", ncontainers)
}
return nil
Expand Down

0 comments on commit dae9496

Please sign in to comment.