From 3279b5297339f799912ff8d3f410eb441f2419e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Mon, 24 May 2021 00:11:57 +0300 Subject: [PATCH] move rootfs to its own package --- BUILD | 10 +++++----- main.go | 28 ++++++++++++++++++++++++++ rootfs/BUILD | 8 ++++++++ rootfs.go => rootfs/rootfs.go | 37 +++-------------------------------- 4 files changed, 44 insertions(+), 39 deletions(-) create mode 100644 rootfs/BUILD rename rootfs.go => rootfs/rootfs.go (76%) diff --git a/BUILD b/BUILD index c10f473..7e62489 100644 --- a/BUILD +++ b/BUILD @@ -2,13 +2,13 @@ load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") go_library( name = "undocker_lib", - srcs = [ - "main.go", - "rootfs.go", - ], + srcs = ["main.go"], importpath = "github.com/motiejus/code/undocker", visibility = ["//visibility:private"], - deps = ["@com_github_jessevdk_go_flags//:go-flags"], + deps = [ + "//src/undocker/rootfs:rootfs_lib", + "@com_github_jessevdk_go_flags//:go-flags", + ], ) go_binary( diff --git a/main.go b/main.go index 1a6250c..700f2b6 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,11 @@ package main import ( + "errors" "os" goflags "github.com/jessevdk/go-flags" + "github.com/motiejus/code/undocker/rootfs" ) type ( @@ -31,3 +33,29 @@ func run(args []string) error { return nil } + +type cmdRootFS struct { + PositionalArgs struct { + Infile goflags.Filename `long:"infile" description:"Input tarball"` + Outfile string `long:"outfile" description:"Output tarball (flattened file system)"` + } `positional-args:"yes" required:"yes"` +} + +func (r *cmdRootFS) Execute(args []string) error { + if len(args) != 0 { + return errors.New("too many args") + } + + in, err := os.Open(string(r.PositionalArgs.Infile)) + if err != nil { + return err + } + defer in.Close() + + out, err := os.Create(string(r.PositionalArgs.Outfile)) + if err != nil { + return err + } + defer out.Close() + return rootfs.Rootfs(in, out) +} diff --git a/rootfs/BUILD b/rootfs/BUILD new file mode 100644 index 0000000..ca38d4e --- /dev/null +++ b/rootfs/BUILD @@ -0,0 +1,8 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "rootfs_lib", + srcs = ["rootfs.go"], + importpath = "github.com/motiejus/code/undocker/rootfs", + visibility = ["//visibility:public"], +) diff --git a/rootfs.go b/rootfs/rootfs.go similarity index 76% rename from rootfs.go rename to rootfs/rootfs.go index e919d05..4c3f1e8 100644 --- a/rootfs.go +++ b/rootfs/rootfs.go @@ -1,53 +1,23 @@ -package main +package rootfs import ( "archive/tar" "encoding/json" - "errors" "fmt" "io" - "os" "strings" - - goflags "github.com/jessevdk/go-flags" ) -type cmdRootFS struct { - PositionalArgs struct { - Infile goflags.Filename `long:"infile" description:"Input tarball"` - Outfile string `long:"outfile" description:"Output tarball (flattened file system)"` - } `positional-args:"yes" required:"yes"` -} - const ( _manifestJSON = "manifest.json" ) -func (r *cmdRootFS) Execute(args []string) error { - if len(args) != 0 { - return errors.New("too many args") - } - - in, err := os.Open(string(r.PositionalArgs.Infile)) - if err != nil { - return err - } - defer in.Close() - - out, err := os.Create(string(r.PositionalArgs.Outfile)) - if err != nil { - return err - } - defer out.Close() - return r.rootfs(in, out) -} - type dockerManifestJSON []struct { Config string `json:"Config"` Layers []string `json:"Layers"` } -// rootfs accepts a docker layer tarball and writes it to outfile. +// Rootfs accepts a docker layer tarball and writes it to outfile. // 1. create map[string]io.ReadSeeker for each layer. // 2. parse manifest.json and get the layer order. // 3. go through each layer in order and write: @@ -56,7 +26,7 @@ type dockerManifestJSON []struct { // I) layer name // II) offset (0 being the first file in the layer) // 4. go through -func (r *cmdRootFS) rootfs(in io.ReadSeeker, out io.Writer) error { +func Rootfs(in io.ReadSeeker, out io.Writer) error { tr := tar.NewReader(in) tw := tar.NewWriter(out) // layerOffsets maps a layer name (a9b123c0daa/layer.tar) to it's offset @@ -88,7 +58,6 @@ func (r *cmdRootFS) rootfs(in io.ReadSeeker, out io.Writer) error { return fmt.Errorf("seek: %w", err) } layerOffsets[hdr.Name] = here - //fmt.Printf("%s\t%x\n", hdr.Name, here) } }