Skip to content

Commit

Permalink
generate: Add --output=PATH and write to stdout by default
Browse files Browse the repository at this point in the history
This makes it easier to post-process output before writing it to disk.
For example, you may want to make adjustments with jq, or write part
of the configuration to a different file for consumption by non-OCI
tools [1].

[1]: opencontainers#128 (comment)

Signed-off-by: W. Trevor King <[email protected]>
  • Loading branch information
wking committed Jul 5, 2016
1 parent 239e8e9 commit f8e1a3d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ ocitools is a collection of tools for working with the [OCI runtime specificatio

## Generating an OCI runtime spec configuration files

[`ocitools generate`][generate.1] generates a [`config.json`][config.json] for an [OCI bundle][bundle].
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.
[`ocitools generate`][generate.1] generates [configuration JSON][config.json] for an [OCI bundle][bundle].
[OCI-compatible runtimes][runtime-spec] like [runC][] expect to read the configuration from `config.json`.

```sh
$ ocitools generate
$ ocitools generate --output config.json
$ cat config.json
{
"ociVersion": "0.5.0",
Expand Down
14 changes: 11 additions & 3 deletions generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

var generateFlags = []cli.Flag{
cli.StringFlag{Name: "output", Value: "output", Usage: "output file (defaults to stdout)"},
cli.StringFlag{Name: "rootfs", Value: "rootfs", Usage: "path to the rootfs"},
cli.BoolFlag{Name: "read-only", Usage: "make the container's rootfs read-only"},
cli.BoolFlag{Name: "privileged", Usage: "enabled privileged container settings"},
Expand Down Expand Up @@ -101,13 +102,20 @@ var generateCommand = cli.Command{
if err != nil {
return err
}
cName := "config.json"
data, err := json.MarshalIndent(&spec, "", "\t")
if err != nil {
return err
}
if err := ioutil.WriteFile(cName, data, 0666); err != nil {
return err
if context.IsSet("output") {
output := context.String("output")
if err := ioutil.WriteFile(output, data, 0666); err != nil {
return err
}
} else {
_, err = os.Stdout.Write(data)
if err != nil {
return err
}
}
return nil
},
Expand Down
12 changes: 9 additions & 3 deletions man/ocitools-generate.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ ocitools-generate - Generate a config.json for an OCI container

# DESCRIPTION

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

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

**--output**=PATH
Instead of writing the configuration JSON to stdout, write it to a
file at *PATH* (overwriting the existing content if a file already
exists at *PATH*).

**--os**=OS
Operating system used within the container

Expand Down

0 comments on commit f8e1a3d

Please sign in to comment.