Skip to content

Commit

Permalink
buildconfig: support passing build config via stdin
Browse files Browse the repository at this point in the history
This commit adds support for reading the build config via stdin.
To do that the `-config` switch now supports `-` and with that it
will use `os.Stdin` insteadof readin the file. Because we have no
file extension to auto-detect the format only JSON is supported
in this mode.

This feature will be used by osbuild-composer when it adds bootc
image mode integration.
  • Loading branch information
mvo5 authored and ondrejbudai committed Nov 5, 2024
1 parent 19acee6 commit 2217bc5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ sudo podman run \
quay.io/centos-bootc/centos-bootc:stream9
```

The configuration can also be passed in via stdin when `--config -`
is used. Only JSON configuration is supported in this mode.

### Users (`user`, array)

Possible fields:
Expand Down
23 changes: 16 additions & 7 deletions bib/internal/buildconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,26 @@ func decodeTomlBuildConfig(r io.Reader, what string) (*BuildConfig, error) {
return &conf, nil
}

var osStdin = os.Stdin

func loadConfig(path string) (*BuildConfig, error) {
fp, err := os.Open(path)
if err != nil {
return nil, err
var fp *os.File
var err error

if path == "-" {
fp = osStdin
} else {
fp, err = os.Open(path)
if err != nil {
return nil, err
}
defer fp.Close()
}
defer fp.Close()

switch filepath.Ext(path) {
case ".json":
switch {
case path == "-", filepath.Ext(path) == ".json":
return decodeJsonBuildConfig(fp, path)
case ".toml":
case filepath.Ext(path) == ".toml":
return decodeTomlBuildConfig(fp, path)
default:
return nil, fmt.Errorf("unsupported file extension for %q", path)
Expand Down
15 changes: 15 additions & 0 deletions bib/internal/buildconfig/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/osbuild/images/pkg/blueprint"

Expand Down Expand Up @@ -168,3 +169,17 @@ minsize = 1000
},
}, conf)
}

func TestReadWithFallbackFromStdin(t *testing.T) {
fakeUserCnfPath := makeFakeConfig(t, "fake-stdin", fakeConfigJSON)
fakeStdinFp, err := os.Open(fakeUserCnfPath)
require.NoError(t, err)
defer fakeStdinFp.Close()

restore := buildconfig.MockOsStdin(fakeStdinFp)
defer restore()

cfg, err := buildconfig.ReadWithFallback("-")
assert.NoError(t, err)
assert.Equal(t, expectedBuildConfig, cfg)
}
12 changes: 12 additions & 0 deletions bib/internal/buildconfig/export_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package buildconfig

import (
"os"
)

func MockConfigRootDir(newDir string) (restore func()) {
saved := configRootDir
configRootDir = newDir
return func() {
configRootDir = saved
}
}

func MockOsStdin(new *os.File) (restore func()) {
saved := osStdin
osStdin = new
return func() {
osStdin = saved
}
}

0 comments on commit 2217bc5

Please sign in to comment.