Skip to content

Commit 06d61a3

Browse files
committed
generate: Add --output=PATH and write to stdout by default
This makes it easier to post-process output before writing it to disk. For example, you may want to make adjustments with jq and/or write part of the configuration to a different file for consumption by non-OCI tools [1]. [1]: #128 (comment) Signed-off-by: W. Trevor King <[email protected]>
1 parent 239e8e9 commit 06d61a3

4 files changed

Lines changed: 24 additions & 10 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ ocitools is a collection of tools for working with the [OCI runtime specificatio
44

55
## Generating an OCI runtime spec configuration files
66

7-
[`ocitools generate`][generate.1] generates a [`config.json`][config.json] for an [OCI bundle][bundle].
8-
This `config.json` file can be placed into a directory and used by an [OCI compatible runtime][runtime-spec] like [runC][] to run a container.
7+
[`ocitools generate`][generate.1] generates [configuration JSON][config.json] for an [OCI bundle][bundle].
8+
[OCI-compatible runtimes][runtime-spec] like [runC][] expect to read the configuration from `config.json`.
99

1010
```sh
11-
$ ocitools generate
11+
$ ocitools generate --output config.json
1212
$ cat config.json
1313
{
1414
"ociVersion": "0.5.0",

generate.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
)
1717

1818
var generateFlags = []cli.Flag{
19+
cli.StringFlag{Name: "output", Value: "output", Usage: "output file (defaults to stdout)"},
1920
cli.StringFlag{Name: "rootfs", Value: "rootfs", Usage: "path to the rootfs"},
2021
cli.BoolFlag{Name: "read-only", Usage: "make the container's rootfs read-only"},
2122
cli.BoolFlag{Name: "privileged", Usage: "enabled privileged container settings"},
@@ -101,13 +102,20 @@ var generateCommand = cli.Command{
101102
if err != nil {
102103
return err
103104
}
104-
cName := "config.json"
105105
data, err := json.MarshalIndent(&spec, "", "\t")
106106
if err != nil {
107107
return err
108108
}
109-
if err := ioutil.WriteFile(cName, data, 0666); err != nil {
110-
return err
109+
if context.IsSet("output") {
110+
output := context.String("output")
111+
if err := ioutil.WriteFile(output, data, 0666); err != nil {
112+
return err
113+
}
114+
} else {
115+
_, err = os.Stdout.Write(data)
116+
if err != nil {
117+
return err
118+
}
111119
}
112120
return nil
113121
},

man/ocitools-generate.1.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ ocitools-generate - Generate a config.json for an OCI container
99

1010
# DESCRIPTION
1111

12-
`ocitools generate` generates a `config.json` for an OCI bundle. This
13-
`config.json` file can be placed into a directory and used by an OCI
14-
compatible runtime like runC to run a container.
12+
`ocitools generate` generates configuration JSON for an OCI bundle.
13+
By default, it writes the JSON to stdout, but you can use **--output**
14+
to direct it to a file. OCI-compatible runtimes like runC expect to
15+
read the configuration from `config.json`.
1516

1617
# OPTIONS
1718
**--apparmor**=PROFILE
@@ -112,6 +113,11 @@ inside of the container.
112113
using tools like setuid apps. It is a good idea to run unprivileged
113114
containers with this flag.
114115

116+
**--output**=PATH
117+
Instead of writing the configuration JSON to stdout, write it to a
118+
file at *PATH* (overwriting the existing content if a file already
119+
exists at *PATH*).
120+
115121
**--os**=OS
116122
Operating system used within the container
117123

test_runtime.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ tar -xf rootfs.tar.gz -C ${TESTDIR}
7878
cp runtimetest ${TESTDIR}
7979

8080
pushd $TESTDIR > /dev/null
81-
ocitools generate "${TEST_ARGS[@]}" --rootfs '.'
81+
ocitools generate --output config.json "${TEST_ARGS[@]}" --rootfs '.'
8282
popd > /dev/null
8383

8484
TESTCMD="${RUNTIME} start $(uuidgen)"

0 commit comments

Comments
 (0)