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:
parent
f02af98ac5
commit
dc03b29e6e
54
main.go
54
main.go
@ -2,7 +2,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
@ -16,56 +15,33 @@ var Version = "unknown"
|
||||
var VersionHash = "unknown"
|
||||
|
||||
const _usage = `Usage:
|
||||
%s [OPTION]... <infile> <outfile>
|
||||
%s <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.
|
||||
|
||||
Options:
|
||||
--prefix=<prefix> prefix all destination files with a given string.
|
||||
<infile>: Input Docker container. Tarball.
|
||||
<outfile>: Output tarball, the root file system. '-' is stdout.
|
||||
|
||||
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
|
||||
|
||||
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")
|
||||
if len(os.Args) != 3 {
|
||||
fmt.Fprintf(os.Stderr, _usage,
|
||||
filepath.Base(os.Args[0]),
|
||||
Version,
|
||||
VersionHash,
|
||||
runtime.Version(),
|
||||
)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
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)
|
||||
os.Exit(1)
|
||||
}
|
||||
@ -73,11 +49,11 @@ func main() {
|
||||
}
|
||||
|
||||
type command struct {
|
||||
flattener func(io.ReadSeeker, io.Writer, ...rootfs.Option) error
|
||||
flattener func(io.ReadSeeker, io.Writer) error
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -108,5 +84,5 @@ func (c *command) execute(infile, outfile, filePrefix string) (_err error) {
|
||||
out = outf
|
||||
}
|
||||
|
||||
return c.flattener(rd, out, rootfs.WithFilePrefix(filePrefix))
|
||||
return c.flattener(rd, out)
|
||||
}
|
||||
|
10
main_test.go
10
main_test.go
@ -9,8 +9,6 @@ import (
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"git.sr.ht/~motiejus/undocker/rootfs"
|
||||
)
|
||||
|
||||
func TestExecute(t *testing.T) {
|
||||
@ -19,7 +17,7 @@ func TestExecute(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
fixture func(*testing.T, string)
|
||||
flattener func(io.ReadSeeker, io.Writer, ...rootfs.Option) error
|
||||
flattener func(io.ReadSeeker, io.Writer) error
|
||||
infile string
|
||||
outfile string
|
||||
wantErr string
|
||||
@ -101,7 +99,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)
|
||||
@ -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)
|
||||
return err
|
||||
}
|
||||
|
||||
func flattenBad(_ io.ReadSeeker, _ io.Writer, _ ...rootfs.Option) error {
|
||||
func flattenBad(_ io.ReadSeeker, _ io.Writer) error {
|
||||
return errors.New("some error")
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
@ -35,14 +35,7 @@ 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, opts ...Option) (_err error) {
|
||||
options := options{
|
||||
filePrefix: "",
|
||||
}
|
||||
for _, o := range opts {
|
||||
o.apply(&options)
|
||||
}
|
||||
|
||||
func Flatten(rd io.ReadSeeker, w io.Writer) (_err error) {
|
||||
tr := tar.NewReader(rd)
|
||||
var closer func() 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 {
|
||||
continue
|
||||
}
|
||||
prefix := options.filePrefix
|
||||
if err := writeFile(tr, tw, hdr, prefix); err != nil {
|
||||
if err := writeFile(tr, tw, hdr); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -195,10 +187,10 @@ func Flatten(rd io.ReadSeeker, w io.Writer, opts ...Option) (_err error) {
|
||||
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{
|
||||
Typeflag: hdr.Typeflag,
|
||||
Name: prefix + hdr.Name,
|
||||
Name: hdr.Name,
|
||||
Linkname: hdr.Linkname,
|
||||
Size: hdr.Size,
|
||||
Mode: int64(hdr.Mode & 0777),
|
||||
|
@ -34,7 +34,6 @@ func TestRootFS(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
opts []Option
|
||||
image tarball
|
||||
want []extractable
|
||||
wantErr string
|
||||
@ -55,7 +54,7 @@ func TestRootFS(t *testing.T) {
|
||||
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{
|
||||
file{Name: "layer1/layer.tar", Contents: layer1.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{
|
||||
file{Name: "layer1/layer.tar", Contents: layer1.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")},
|
||||
},
|
||||
},
|
||||
{
|
||||
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 {
|
||||
@ -204,7 +190,7 @@ func TestRootFS(t *testing.T) {
|
||||
in := bytes.NewReader(tt.image.Buffer().Bytes())
|
||||
out := bytes.Buffer{}
|
||||
|
||||
err := Flatten(in, &out, tt.opts...)
|
||||
err := Flatten(in, &out)
|
||||
if tt.wantErr != "" {
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
|
Loading…
Reference in New Issue
Block a user