Revert "add WithFilePrefix"

This reverts commit 436a866f5d.

This is not a good place for such auxiliary functionality; will be moved
elsewhere.
This commit is contained in:
Motiejus Jakštys 2021-08-29 18:39:46 +03:00 committed by Motiejus Jakštys
parent f02af98ac5
commit dc03b29e6e
5 changed files with 26 additions and 94 deletions

54
main.go
View File

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

View File

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

View File

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

View File

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