Skip to content

Commit

Permalink
Merge pull request #5226 from nalind/commit-add-files
Browse files Browse the repository at this point in the history
commit: add a --add-file flag
  • Loading branch information
openshift-merge-bot[bot] authored Dec 16, 2023
2 parents 5436ddc + 041388f commit b9346c5
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 37 deletions.
26 changes: 26 additions & 0 deletions cmd/buildah/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"strings"
"time"

"github.com/containers/buildah"
Expand Down Expand Up @@ -49,6 +50,7 @@ type commitInputOptions struct {
encryptionKeys []string
encryptLayers []int
unsetenvs []string
addFile []string
}

func init() {
Expand Down Expand Up @@ -77,6 +79,7 @@ func commitListFlagSet(cmd *cobra.Command, opts *commitInputOptions) {
flags := cmd.Flags()
flags.SetInterspersed(false)

flags.StringArrayVar(&opts.addFile, "add-file", nil, "add contents of a file to the image at a specified path (`source:destination`)")
flags.StringVar(&opts.authfile, "authfile", auth.GetDefaultAuthFile(), "path of the authentication file. Use REGISTRY_AUTH_FILE environment variable to override")
_ = cmd.RegisterFlagCompletionFunc("authfile", completion.AutocompleteDefault)
flags.StringVar(&opts.blobCache, "blob-cache", "", "assume image blobs in the specified directory will be available for pushing")
Expand Down Expand Up @@ -223,6 +226,28 @@ func commitCmd(c *cobra.Command, args []string, iopts commitInputOptions) error
}
}

var addFiles map[string]string
if len(iopts.addFile) > 0 {
addFiles = make(map[string]string)
for _, spec := range iopts.addFile {
specSlice := strings.SplitN(spec, ":", 2)
if len(specSlice) == 1 {
specSlice = []string{specSlice[0], specSlice[0]}
}
if len(specSlice) != 2 {
return fmt.Errorf("parsing add-file argument %q: expected 1 or 2 parts, got %d", spec, len(strings.SplitN(spec, ":", 2)))
}
st, err := os.Stat(specSlice[0])
if err != nil {
return fmt.Errorf("parsing add-file argument %q: source %q: %w", spec, specSlice[0], err)
}
if st.IsDir() {
return fmt.Errorf("parsing add-file argument %q: source %q is not a regular file", spec, specSlice[0])
}
addFiles[specSlice[1]] = specSlice[0]
}
}

options := buildah.CommitOptions{
PreferredManifestType: format,
Manifest: iopts.manifest,
Expand All @@ -239,6 +264,7 @@ func commitCmd(c *cobra.Command, args []string, iopts commitInputOptions) error
UnsetEnvs: iopts.unsetenvs,
OverrideChanges: iopts.changes,
OverrideConfig: overrideConfig,
ExtraImageContent: addFiles,
}
exclusiveFlags := 0
if c.Flag("reference-time").Changed {
Expand Down
6 changes: 6 additions & 0 deletions commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ type CommitOptions struct {
// to the configuration of the image that is being committed, after
// OverrideConfig is applied.
OverrideChanges []string
// ExtraImageContent is a map which describes additional content to add
// to the committted image. The map's keys are filesystem paths in the
// image and the corresponding values are the paths of files whose
// contents will be used in their place. The contents will be owned by
// 0:0 and have mode 0644. Currently only accepts regular files.
ExtraImageContent map[string]string
}

var (
Expand Down
8 changes: 8 additions & 0 deletions docs/buildah-commit.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ The image ID of the image that was created. On error, 1 is returned and errno i

## OPTIONS

**--add-file** *source[:destination]*

Read the contents of the file `source` and add it to the committed image as a
file at `destination`. If `destination` is not specified, the path of `source`
will be used. The new file will be owned by UID 0, GID 0, have 0644
permissions, and be given a current timestamp unless the **--timestamp** option
is also specified. This option can be specified multiple times.

**--authfile** *path*

Path of the authentication file. Default is ${XDG_RUNTIME_DIR}/containers/auth.json. If XDG_RUNTIME_DIR is not set, the default is /run/containers/$UID/auth.json. This file is created using `buildah login`.
Expand Down
Loading

0 comments on commit b9346c5

Please sign in to comment.