Skip to content

Commit

Permalink
Output to stdout by default
Browse files Browse the repository at this point in the history
Signed-off-by: Lei Jitang <[email protected]>
  • Loading branch information
coolljt0725 committed Oct 24, 2016
1 parent a694c4e commit 7181863
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 36 deletions.
38 changes: 31 additions & 7 deletions cmd/oci-create-layer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions cmd/oci-create-layer/oci-create-layer.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand All @@ -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
Expand Down
26 changes: 1 addition & 25 deletions image/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 21 additions & 2 deletions image/layer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -133,6 +133,25 @@ 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)
if err != nil {
return err
}
return nil
}

func createFilesystem(path string, files map[string]bool, modify map[string]func(string) error) error {
for f, add := range files {
// add file
Expand Down

0 comments on commit 7181863

Please sign in to comment.