refactor go-flags initialization
This commit is contained in:
@@ -6,7 +6,6 @@ go_library(
|
||||
importpath = "github.com/motiejus/code/undocker/internal/cmdrootfs",
|
||||
visibility = ["//src/undocker:__subpackages__"],
|
||||
deps = [
|
||||
"//src/undocker/internal/cmd:go_default_library",
|
||||
"//src/undocker/rootfs:go_default_library",
|
||||
"@com_github_jessevdk_go_flags//:go_default_library",
|
||||
"@org_uber_go_multierr//:go_default_library",
|
||||
@@ -18,7 +17,6 @@ go_test(
|
||||
srcs = ["cmdrootfs_test.go"],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//src/undocker/internal/cmd:go_default_library",
|
||||
"@com_github_jessevdk_go_flags//:go_default_library",
|
||||
"@com_github_stretchr_testify//assert:go_default_library",
|
||||
"@com_github_stretchr_testify//require:go_default_library",
|
||||
|
||||
@@ -7,34 +7,37 @@ import (
|
||||
"os"
|
||||
|
||||
goflags "github.com/jessevdk/go-flags"
|
||||
"github.com/motiejus/code/undocker/internal/cmd"
|
||||
"github.com/motiejus/code/undocker/rootfs"
|
||||
"go.uber.org/multierr"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
// Command is "rootfs" command
|
||||
Command struct {
|
||||
cmd.BaseCommand
|
||||
flattener func(io.ReadSeeker, io.Writer) error
|
||||
Stdout io.Writer
|
||||
|
||||
PositionalArgs struct {
|
||||
Infile goflags.Filename `long:"infile" description:"Input tarball"`
|
||||
Outfile string `long:"outfile" description:"Output path, stdout is '-'"`
|
||||
Infile goflags.Filename `long:"infile" desc:"Input tarball"`
|
||||
Outfile string `long:"outfile" desc:"Output path, stdout is '-'"`
|
||||
} `positional-args:"yes" required:"yes"`
|
||||
|
||||
flattener func(io.ReadSeeker, io.Writer) error
|
||||
}
|
||||
)
|
||||
|
||||
func NewCommand() *Command {
|
||||
return &Command{
|
||||
flattener: rootfs.Flatten,
|
||||
Stdout: os.Stdout,
|
||||
}
|
||||
}
|
||||
|
||||
func (*Command) ShortDesc() string { return "Flatten a docker container image to a tarball" }
|
||||
func (*Command) LongDesc() string { return "" }
|
||||
|
||||
// Execute executes rootfs Command
|
||||
func (c *Command) Execute(args []string) (err error) {
|
||||
if len(args) != 0 {
|
||||
return errors.New("too many args")
|
||||
}
|
||||
if c.flattener == nil {
|
||||
c.init()
|
||||
}
|
||||
|
||||
rd, err := os.Open(string(c.PositionalArgs.Infile))
|
||||
if err != nil {
|
||||
@@ -56,12 +59,3 @@ func (c *Command) Execute(args []string) (err error) {
|
||||
|
||||
return c.flattener(rd, out)
|
||||
}
|
||||
|
||||
// init() initializes Command with the default options.
|
||||
//
|
||||
// Since constructors for sub-commands requires lots of boilerplate,
|
||||
// command will initialize itself.
|
||||
func (c *Command) init() {
|
||||
c.BaseCommand.Init()
|
||||
c.flattener = rootfs.Flatten
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"testing"
|
||||
|
||||
goflags "github.com/jessevdk/go-flags"
|
||||
"github.com/motiejus/code/undocker/internal/cmd"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -57,7 +56,7 @@ func TestExecute(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
var stdout bytes.Buffer
|
||||
c := &Command{BaseCommand: cmd.BaseCommand{Stdout: &stdout}}
|
||||
c := &Command{Stdout: &stdout}
|
||||
if tt.fixture != nil {
|
||||
tt.fixture(t, dir)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user