This commit is contained in:
Motiejus Jakštys 2021-05-24 00:11:58 +03:00
parent 46aed94077
commit 890d99cd3e
3 changed files with 29 additions and 17 deletions

View File

@ -9,6 +9,7 @@ import (
"go.uber.org/multierr" "go.uber.org/multierr"
) )
// CmdRootFS is "rootfs" command
type CmdRootFS struct { type CmdRootFS struct {
PositionalArgs struct { PositionalArgs struct {
Infile goflags.Filename `long:"infile" description:"Input tarball"` Infile goflags.Filename `long:"infile" description:"Input tarball"`
@ -16,6 +17,7 @@ type CmdRootFS struct {
} `positional-args:"yes" required:"yes"` } `positional-args:"yes" required:"yes"`
} }
// Execute executes CmdRootFS
func (r *CmdRootFS) Execute(args []string) (err error) { func (r *CmdRootFS) Execute(args []string) (err error) {
if len(args) != 0 { if len(args) != 0 {
return errors.New("too many args") return errors.New("too many args")

View File

@ -10,33 +10,39 @@ import (
) )
type ( type (
// Tarrer is an object that can tar itself to an archive
Tarrer interface { Tarrer interface {
Tar(*tar.Writer) error Tar(*tar.Writer) error
} }
// Tarball is a list of tarrables
Tarball []Tarrer Tarball []Tarrer
// Extractable is an empty interface for comparing extracted outputs in tests. // Extractable is an empty interface for comparing extracted outputs in tests.
// Using that just to avoid the ugly `interface{}`. // Using that just to avoid the ugly `interface{}`.
Extractable interface{} Extractable interface{}
// Dir is a tarrable directory representation
Dir struct { Dir struct {
Name string Name string
Uid int UID int
} }
// File is a tarrable file representation
File struct { File struct {
Name string Name string
Uid int UID int
Contents *bytes.Buffer Contents *bytes.Buffer
} }
// Hardlink is a representation of a hardlink
Hardlink struct { Hardlink struct {
Name string Name string
Uid int UID int
} }
) )
// Buffer returns a byte buffer
func (tb Tarball) Buffer() *bytes.Buffer { func (tb Tarball) Buffer() *bytes.Buffer {
var buf bytes.Buffer var buf bytes.Buffer
tw := tar.NewWriter(&buf) tw := tar.NewWriter(&buf)
@ -47,16 +53,18 @@ func (tb Tarball) Buffer() *bytes.Buffer {
return &buf return &buf
} }
// Tar tars the Dir
func (d Dir) Tar(tw *tar.Writer) error { func (d Dir) Tar(tw *tar.Writer) error {
hdr := &tar.Header{ hdr := &tar.Header{
Typeflag: tar.TypeDir, Typeflag: tar.TypeDir,
Name: d.Name, Name: d.Name,
Mode: 0644, Mode: 0644,
Uid: d.Uid, Uid: d.UID,
} }
return tw.WriteHeader(hdr) return tw.WriteHeader(hdr)
} }
// Tar tars the File
func (f File) Tar(tw *tar.Writer) error { func (f File) Tar(tw *tar.Writer) error {
var contents []byte var contents []byte
if f.Contents != nil { if f.Contents != nil {
@ -66,7 +74,7 @@ func (f File) Tar(tw *tar.Writer) error {
Typeflag: tar.TypeReg, Typeflag: tar.TypeReg,
Name: f.Name, Name: f.Name,
Mode: 0644, Mode: 0644,
Uid: f.Uid, Uid: f.UID,
Size: int64(len(contents)), Size: int64(len(contents)),
} }
if err := tw.WriteHeader(hdr); err != nil { if err := tw.WriteHeader(hdr); err != nil {
@ -78,15 +86,17 @@ func (f File) Tar(tw *tar.Writer) error {
return nil return nil
} }
// Tar tars the Hardlink
func (h Hardlink) Tar(tw *tar.Writer) error { func (h Hardlink) Tar(tw *tar.Writer) error {
return tw.WriteHeader(&tar.Header{ return tw.WriteHeader(&tar.Header{
Typeflag: tar.TypeLink, Typeflag: tar.TypeLink,
Name: h.Name, Name: h.Name,
Mode: 0644, Mode: 0644,
Uid: h.Uid, Uid: h.UID,
}) })
} }
// Extract extracts a tarball to a slice of extractables
func Extract(t *testing.T, r io.Reader) []Extractable { func Extract(t *testing.T, r io.Reader) []Extractable {
t.Helper() t.Helper()
ret := []Extractable{} ret := []Extractable{}
@ -101,11 +111,11 @@ func Extract(t *testing.T, r io.Reader) []Extractable {
var elem Extractable var elem Extractable
switch hdr.Typeflag { switch hdr.Typeflag {
case tar.TypeDir: case tar.TypeDir:
elem = Dir{Name: hdr.Name, Uid: hdr.Uid} elem = Dir{Name: hdr.Name, UID: hdr.Uid}
case tar.TypeLink: case tar.TypeLink:
elem = Hardlink{Name: hdr.Name} elem = Hardlink{Name: hdr.Name}
case tar.TypeReg: case tar.TypeReg:
f := File{Name: hdr.Name, Uid: hdr.Uid} f := File{Name: hdr.Name, UID: hdr.Uid}
if hdr.Size > 0 { if hdr.Size > 0 {
var buf bytes.Buffer var buf bytes.Buffer
io.Copy(&buf, tr) io.Copy(&buf, tr)

View File

@ -21,16 +21,16 @@ type (
func TestRootFS(t *testing.T) { func TestRootFS(t *testing.T) {
layer0 := tarball{ layer0 := tarball{
dir{Name: "/", Uid: 0}, dir{Name: "/", UID: 0},
file{Name: "/file", Uid: 0, Contents: bytes.NewBufferString("from 0")}, file{Name: "/file", UID: 0, Contents: bytes.NewBufferString("from 0")},
} }
layer1 := tarball{ layer1 := tarball{
file{Name: "/file", Uid: 1, Contents: bytes.NewBufferString("from 1")}, file{Name: "/file", UID: 1, Contents: bytes.NewBufferString("from 1")},
} }
layer2 := tarball{ layer2 := tarball{
dir{Name: "/", Uid: 2}, dir{Name: "/", UID: 2},
} }
tests := []struct { tests := []struct {
@ -57,8 +57,8 @@ func TestRootFS(t *testing.T) {
manifest{"layer0/layer.tar", "layer1/layer.tar"}, manifest{"layer0/layer.tar", "layer1/layer.tar"},
}, },
want: []extractable{ want: []extractable{
dir{Name: "/", Uid: 0}, dir{Name: "/", UID: 0},
file{Name: "/file", Uid: 1, Contents: bytes.NewBufferString("from 1")}, file{Name: "/file", UID: 1, Contents: bytes.NewBufferString("from 1")},
}, },
}, },
{ {
@ -85,9 +85,9 @@ func TestRootFS(t *testing.T) {
manifest{"layer0/layer.tar", "layer1/layer.tar", "layer2/layer.tar"}, manifest{"layer0/layer.tar", "layer1/layer.tar", "layer2/layer.tar"},
}, },
want: []extractable{ want: []extractable{
dir{Name: "/", Uid: 0}, dir{Name: "/", UID: 0},
file{Name: "/file", Uid: 1, Contents: bytes.NewBufferString("from 1")}, file{Name: "/file", UID: 1, Contents: bytes.NewBufferString("from 1")},
dir{Name: "/", Uid: 2}, dir{Name: "/", UID: 2},
}, },
}, },
{ {