move command implementations to internal/

This commit is contained in:
Motiejus Jakštys 2021-05-24 00:11:58 +03:00
parent 02cec0f76f
commit a9b4f1ecd4
4 changed files with 60 additions and 40 deletions

3
BUILD
View File

@ -6,9 +6,8 @@ go_library(
importpath = "github.com/motiejus/code/undocker",
visibility = ["//visibility:private"],
deps = [
"//src/undocker/rootfs:go_default_library",
"//src/undocker/internal/cmdrootfs:go_default_library",
"@com_github_jessevdk_go_flags//:go_default_library",
"@org_uber_go_multierr//:go_default_library",
],
)

13
internal/cmdrootfs/BUILD Normal file
View File

@ -0,0 +1,13 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = ["cmdrootfs.go"],
importpath = "github.com/motiejus/code/undocker/internal/cmdrootfs",
visibility = ["//src/undocker:__subpackages__"],
deps = [
"//src/undocker/rootfs:go_default_library",
"@com_github_jessevdk_go_flags//:go_default_library",
"@org_uber_go_multierr//:go_default_library",
],
)

View File

@ -0,0 +1,43 @@
package cmdrootfs
import (
"errors"
"os"
goflags "github.com/jessevdk/go-flags"
"github.com/motiejus/code/undocker/rootfs"
"go.uber.org/multierr"
)
type CmdRootFS struct {
PositionalArgs struct {
Infile goflags.Filename `long:"infile" description:"Input tarball"`
Outfile string `long:"outfile" description:"Output path, stdout is '-'"`
} `positional-args:"yes" required:"yes"`
}
func (r *CmdRootFS) Execute(args []string) (err 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 func() { err = multierr.Append(err, in.Close()) }()
var out *os.File
outf := string(r.PositionalArgs.Outfile)
if outf == "-" {
out = os.Stdout
} else {
out, err = os.Create(outf)
if err != nil {
return err
}
}
defer func() { err = multierr.Append(err, out.Close()) }()
return rootfs.RootFS(in, out)
}

41
main.go
View File

@ -1,18 +1,16 @@
package main
import (
"errors"
"os"
goflags "github.com/jessevdk/go-flags"
"github.com/motiejus/code/undocker/rootfs"
"go.uber.org/multierr"
"github.com/motiejus/code/undocker/internal/cmdrootfs"
)
type (
params struct {
RootFS cmdRootFS `command:"rootfs" description:"Unpack a docker container image to a single filesystem tarball"`
Manifest cmdManifest `command:"manifest"`
RootFS cmdrootfs.CmdRootFS `command:"rootfs" description:"Unpack a docker container image to a single filesystem tarball"`
Manifest cmdManifest `command:"manifest"`
}
cmdManifest struct{} // stub
@ -33,36 +31,3 @@ 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 path, stdout is '-'"`
} `positional-args:"yes" required:"yes"`
}
func (r *cmdRootFS) Execute(args []string) (err 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 func() { err = multierr.Append(err, in.Close()) }()
var out *os.File
outf := string(r.PositionalArgs.Outfile)
if outf == "-" {
out = os.Stdout
} else {
out, err = os.Create(outf)
if err != nil {
return err
}
}
defer func() { err = multierr.Append(err, out.Close()) }()
return rootfs.RootFS(in, out)
}