1
Fork 0

add WithFilePrefix

This adds an option to prefix every file path with a given string.
main
Motiejus Jakštys 2021-08-29 16:55:32 +03:00 committed by Motiejus Jakštys
parent 71aab65b75
commit 436a866f5d
6 changed files with 95 additions and 26 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/undocker
/undocker-*
coverage.html
sha256sum.txt*

54
main.go
View File

@ -2,6 +2,7 @@
package main
import (
"flag"
"fmt"
"io"
"os"
@ -15,33 +16,56 @@ var Version = "unknown"
var VersionHash = "unknown"
const _usage = `Usage:
%s <infile> <outfile>
%s [OPTION]... <infile> <outfile>
Flatten a Docker container image to a root file system.
Arguments:
<infile>: Input Docker container. Tarball.
<outfile>: Output tarball, the root file system. '-' is stdout.
<infile> Input Docker container. Tarball.
<outfile> Output tarball, the root file system. '-' is stdout.
Options:
--prefix=<prefix> prefix all destination files with a given string.
undocker %s (%s)
Built with %s
`
func usage(pre string, out io.Writer) {
fmt.Fprintf(out, pre+_usage,
filepath.Base(os.Args[0]),
Version,
VersionHash,
runtime.Version(),
)
}
func usageErr(pre string) {
usage(pre, os.Stderr)
os.Exit(2)
}
func main() {
runtime.GOMAXPROCS(1) // no need to create that many threads
if len(os.Args) != 3 {
fmt.Fprintf(os.Stderr, _usage,
filepath.Base(os.Args[0]),
Version,
VersionHash,
runtime.Version(),
)
os.Exit(1)
var filePrefix string
fs := flag.NewFlagSet("undocker", flag.ExitOnError)
fs.Usage = func() { usageErr("") }
fs.StringVar(&filePrefix, "prefix", "", "prefix files in the tarball")
if len(os.Args) == 1 {
usageErr("")
}
_ = fs.Parse(os.Args[1:]) // ExitOnError captures it
args := fs.Args()
if len(args) != 2 {
usageErr("invalid number of arguments\n")
}
c := &command{flattener: rootfs.Flatten, Stdout: os.Stdout}
if err := c.execute(os.Args[1], os.Args[2]); err != nil {
if err := c.execute(args[0], args[1], filePrefix); err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
@ -49,11 +73,11 @@ func main() {
}
type command struct {
flattener func(io.ReadSeeker, io.Writer) error
flattener func(io.ReadSeeker, io.Writer, ...rootfs.Option) error
Stdout io.Writer
}
func (c *command) execute(infile string, outfile string) (_err error) {
func (c *command) execute(infile, outfile, filePrefix string) (_err error) {
rd, err := os.Open(infile)
if err != nil {
return err
@ -84,5 +108,5 @@ func (c *command) execute(infile string, outfile string) (_err error) {
out = outf
}
return c.flattener(rd, out)
return c.flattener(rd, out, rootfs.WithFilePrefix(filePrefix))
}

View File

@ -9,6 +9,8 @@ import (
"path/filepath"
"regexp"
"testing"
"git.sr.ht/~motiejus/undocker/rootfs"
)
func TestExecute(t *testing.T) {
@ -17,7 +19,7 @@ func TestExecute(t *testing.T) {
tests := []struct {
name string
fixture func(*testing.T, string)
flattener func(io.ReadSeeker, io.Writer) error
flattener func(io.ReadSeeker, io.Writer, ...rootfs.Option) error
infile string
outfile string
wantErr string
@ -99,7 +101,7 @@ func TestExecute(t *testing.T) {
inf := filepath.Join(dir, tt.infile)
c := &command{Stdout: &stdout, flattener: tt.flattener}
err := c.execute(inf, tt.outfile)
err := c.execute(inf, tt.outfile, "")
if tt.assertion != nil {
tt.assertion(t, dir)
@ -135,11 +137,11 @@ func TestExecute(t *testing.T) {
}
}
func flattenPassthrough(r io.ReadSeeker, w io.Writer) error {
func flattenPassthrough(r io.ReadSeeker, w io.Writer, _ ...rootfs.Option) error {
_, err := io.Copy(w, r)
return err
}
func flattenBad(_ io.ReadSeeker, _ io.Writer) error {
func flattenBad(_ io.ReadSeeker, _ io.Writer, _ ...rootfs.Option) error {
return errors.New("some error")
}

20
rootfs/options.go Normal file
View File

@ -0,0 +1,20 @@
package rootfs
type options struct {
filePrefix string
}
type Option interface {
apply(*options)
}
type filePrefixOption string
func (p filePrefixOption) apply(opts *options) {
opts.filePrefix = string(p)
}
// WithFilePrefixOption adds a prefix to all files in the output archive.
func WithFilePrefix(p string) Option {
return filePrefixOption(p)
}

View File

@ -35,7 +35,14 @@ type (
// Flatten flattens a docker image to a tarball. The underlying io.Writer
// should be an open file handle, which the caller is responsible for closing
// themselves
func Flatten(rd io.ReadSeeker, w io.Writer) (_err error) {
func Flatten(rd io.ReadSeeker, w io.Writer, opts ...Option) (_err error) {
options := options{
filePrefix: "",
}
for _, o := range opts {
o.apply(&options)
}
tr := tar.NewReader(rd)
var closer func() error
var err error
@ -177,7 +184,8 @@ func Flatten(rd io.ReadSeeker, w io.Writer) (_err error) {
if hdr.Typeflag != tar.TypeDir && file2layer[hdr.Name] != i {
continue
}
if err := writeFile(tr, tw, hdr); err != nil {
prefix := options.filePrefix
if err := writeFile(tr, tw, hdr, prefix); err != nil {
return err
}
}
@ -188,10 +196,10 @@ func Flatten(rd io.ReadSeeker, w io.Writer) (_err error) {
return nil
}
func writeFile(tr *tar.Reader, tw *tar.Writer, hdr *tar.Header) error {
func writeFile(tr *tar.Reader, tw *tar.Writer, hdr *tar.Header, prefix string) error {
hdrOut := &tar.Header{
Typeflag: hdr.Typeflag,
Name: hdr.Name,
Name: prefix + hdr.Name,
Linkname: hdr.Linkname,
Size: hdr.Size,
Mode: int64(hdr.Mode & 0777),

View File

@ -34,6 +34,7 @@ func TestRootFS(t *testing.T) {
tests := []struct {
name string
opts []Option
image tarball
want []extractable
wantErr string
@ -54,7 +55,7 @@ func TestRootFS(t *testing.T) {
wantErr: "layer0/layer.tar defined in manifest, missing in tarball",
},
{
name: "basic file overwrite, layer order mixed",
name: "ok: basic file overwrite, layer order mixed",
image: tarball{
file{Name: "layer1/layer.tar", Contents: layer1.Buffer()},
file{Name: "layer0/layer.tar", Contents: layer0.Buffer()},
@ -172,7 +173,7 @@ func TestRootFS(t *testing.T) {
},
},
{
name: "archived layer",
name: "compressed layer",
image: tarball{
file{Name: "layer1/layer.tar", Contents: layer1.Gzip()},
file{Name: "layer0/layer.tar", Contents: layer0.Gzip()},
@ -183,6 +184,19 @@ func TestRootFS(t *testing.T) {
file{Name: "/file", UID: 1, Contents: bytes.NewBufferString("from 1")},
},
},
{
name: "ok: add a file prefix",
opts: []Option{WithFilePrefix("/opt")},
image: tarball{
file{Name: "layer1/layer.tar", Contents: layer1.Buffer()},
file{Name: "layer0/layer.tar", Contents: layer0.Buffer()},
manifest{"layer0/layer.tar", "layer1/layer.tar"},
},
want: []extractable{
dir{Name: "/opt/", UID: 0},
file{Name: "/opt/file", UID: 1, Contents: bytes.NewBufferString("from 1")},
},
},
}
for _, tt := range tests {
@ -190,7 +204,7 @@ func TestRootFS(t *testing.T) {
in := bytes.NewReader(tt.image.Buffer().Bytes())
out := bytes.Buffer{}
err := Flatten(in, &out)
err := Flatten(in, &out, tt.opts...)
if tt.wantErr != "" {
if err == nil {
t.Fatal("expected error, got nil")