From 8f43e7a70c0a8b3b0884b23de2c8eefcebd448cc Mon Sep 17 00:00:00 2001 From: Lei Jitang Date: Mon, 24 Oct 2016 08:26:48 -0400 Subject: [PATCH] Output to stdout by default Signed-off-by: Lei Jitang --- cmd/oci-create-layer/main.go | 38 ++++++++++++++++++---- cmd/oci-create-layer/oci-create-layer.1.md | 4 +-- image/layer.go | 26 +-------------- image/layer_test.go | 20 ++++++++++-- 4 files changed, 52 insertions(+), 36 deletions(-) diff --git a/cmd/oci-create-layer/main.go b/cmd/oci-create-layer/main.go index 9789884..1ce637c 100644 --- a/cmd/oci-create-layer/main.go +++ b/cmd/oci-create-layer/main.go @@ -15,31 +15,32 @@ package main import ( + "io" "log" "os" + "path/filepath" "github.com/opencontainers/image-tools/image" "github.com/spf13/cobra" ) type layerCmd struct { - stdout *log.Logger + stdout io.Writer stderr *log.Logger dest string } func main() { - stdout := log.New(os.Stdout, "", 0) stderr := log.New(os.Stderr, "", 0) - cmd := newLayerCmd(stdout, stderr) + cmd := newLayerCmd(os.Stdout, stderr) if err := cmd.Execute(); err != nil { stderr.Println(err) os.Exit(1) } } -func newLayerCmd(stdout, stderr *log.Logger) *cobra.Command { +func newLayerCmd(stdout io.Writer, stderr *log.Logger) *cobra.Command { v := &layerCmd{ stdout: stdout, stderr: stderr, @@ -66,15 +67,38 @@ func (v *layerCmd) Run(cmd *cobra.Command, args []string) { } os.Exit(1) } - var err error + var ( + err error + out io.ReadCloser + ) if len(args) == 1 { - err = image.CreateLayer(args[0], "", v.dest) + out, err = image.CreateLayer(args[0], "") } else { - err = image.CreateLayer(args[0], args[1], v.dest) + out, err = image.CreateLayer(args[0], args[1]) } if err != nil { v.stderr.Printf("create layer failed: %v", err) os.Exit(1) } + if v.dest == "" { + _, err := io.Copy(v.stdout, out) + if err != nil { + v.stderr.Printf("create layer failed: %v", err) + os.Exit(1) + } + } else { + filename := filepath.Clean(v.dest) + f, err := os.Create(filename) + if err != nil { + v.stderr.Printf("create layer failed: %v", err) + os.Exit(1) + } + defer f.Close() + _, err = io.Copy(f, out) + if err != nil { + v.stderr.Printf("create layer failed: %v", err) + os.Exit(1) + } + } os.Exit(0) } diff --git a/cmd/oci-create-layer/oci-create-layer.1.md b/cmd/oci-create-layer/oci-create-layer.1.md index 27236fe..5feb5c4 100644 --- a/cmd/oci-create-layer/oci-create-layer.1.md +++ b/cmd/oci-create-layer/oci-create-layer.1.md @@ -8,7 +8,7 @@ oci-create-layer \- Create filesystem changeset **oci-create-layer** [child] [parent] [flags] # DESCRIPTION -`oci-create-layer` creates a filesystem changeset from two layers. It compares child with parent and generates a filsystem diff, pack the diff into a uncompressed tar archive. The output tar archive name is the child name with .tar suffix by default, use `--dest` to specify a custom one. +`oci-create-layer` creates a filesystem changeset from two layers. It compares child with parent and generates a filsystem diff, pack the diff into a uncompressed tar archive. The default output is stdout, use `--dest` to specify a custom one. # OPTIONS **--help** @@ -19,7 +19,7 @@ The dest specify a particular filename where the layer write to # EXAMPLES ``` -$ oci-create-layer rootfs-1-s rootfs-1 +$ oci-create-layer --dest rootfs-1-s.tar rootfs-1-s rootfs-1 $ ls rootfs-1 rootfs-1-s rootfs-1-s.tar diff --git a/image/layer.go b/image/layer.go index e968257..b66c30a 100644 --- a/image/layer.go +++ b/image/layer.go @@ -15,37 +15,13 @@ package image import ( - "fmt" "io" - "os" - "path/filepath" "github.com/opencontainers/image-tools/utils" ) // CreateLayer cretes filesystem changset from child and parent -func CreateLayer(child, parent, dest string) error { - arch, err := Diff(child, parent) - if err != nil { - return err - } - defer arch.Close() - filename := fmt.Sprintf("%s.tar", filepath.Clean(child)) - if dest != "" { - filename = filepath.Clean(dest) - } - f, err := os.Create(filename) - if err != nil { - return err - } - defer f.Close() - _, err = io.Copy(f, arch) - return err -} - -// Diff produces an archive of the changes between the specified -// layer and its parent layer which may be "". -func Diff(child, parent string) (rc io.ReadCloser, err error) { +func CreateLayer(child, parent string) (io.ReadCloser, error) { changes, err := utils.ChangesDirs(child, parent) if err != nil { return nil, err diff --git a/image/layer_test.go b/image/layer_test.go index cd0b6e0..3aa8482 100644 --- a/image/layer_test.go +++ b/image/layer_test.go @@ -56,7 +56,7 @@ func TestCreateFilesystemChangeset(t *testing.T) { } // create base layer tarfile := filepath.Join(tmp1, "base.tar") - err = CreateLayer(basepath, "", tarfile) + err = createLayer(basepath, "", tarfile) if err != nil { t.Fatal(err) } @@ -103,7 +103,7 @@ func TestCreateFilesystemChangeset(t *testing.T) { // create layer diff tarfile1 := filepath.Join(tmp1, "base.s1.tar") - err = CreateLayer(snapshot1path, basepath, tarfile1) + err = createLayer(snapshot1path, basepath, tarfile1) if err != nil { t.Fatal(err) } @@ -133,6 +133,22 @@ func TestCreateFilesystemChangeset(t *testing.T) { } } +func createLayer(child, parent, dest string) error { + filename := filepath.Clean(dest) + f, err := os.Create(filename) + if err != nil { + return err + } + defer f.Close() + out, err := CreateLayer(child, parent) + if err != nil { + return err + } + + _, err = io.Copy(f, out) + return err +} + func createFilesystem(path string, files map[string]bool, modify map[string]func(string) error) error { for f, add := range files { // add file