2021-05-24 00:11:58 +03:00
|
|
|
package cmdrootfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-05-24 00:11:58 +03:00
|
|
|
"fmt"
|
2021-05-24 00:11:58 +03:00
|
|
|
"io"
|
2021-05-24 00:11:58 +03:00
|
|
|
"os"
|
|
|
|
|
|
|
|
goflags "github.com/jessevdk/go-flags"
|
|
|
|
"github.com/motiejus/code/undocker/rootfs"
|
|
|
|
)
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
const _description = "Flatten a docker container image to a tarball"
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
// Command is an implementation of go-flags.Command
|
|
|
|
type Command struct {
|
|
|
|
flattener func(io.ReadSeeker, io.Writer) error
|
|
|
|
Stdout io.Writer
|
2021-05-24 00:11:58 +03:00
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
PositionalArgs struct {
|
|
|
|
Infile goflags.Filename `long:"infile" desc:"Input tarball"`
|
|
|
|
Outfile string `long:"outfile" desc:"Output path, stdout is '-'"`
|
|
|
|
} `positional-args:"yes" required:"yes"`
|
|
|
|
}
|
2021-05-24 00:11:58 +03:00
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
// NewCommand creates a new Command struct
|
2021-05-24 00:11:58 +03:00
|
|
|
func NewCommand() *Command {
|
|
|
|
return &Command{
|
|
|
|
flattener: rootfs.Flatten,
|
|
|
|
Stdout: os.Stdout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
// ShortDesc returns the command's short description
|
2021-05-24 00:11:58 +03:00
|
|
|
func (*Command) ShortDesc() string { return _description }
|
2021-05-24 00:11:58 +03:00
|
|
|
|
|
|
|
// LongDesc returns the command's long description
|
|
|
|
func (*Command) LongDesc() string { return _description }
|
2021-05-24 00:11:58 +03:00
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
// Execute executes rootfs Command
|
|
|
|
func (c *Command) Execute(args []string) (err error) {
|
2021-05-24 00:11:58 +03:00
|
|
|
if len(args) != 0 {
|
|
|
|
return errors.New("too many args")
|
|
|
|
}
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
rd, err := os.Open(string(c.PositionalArgs.Infile))
|
2021-05-24 00:11:58 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-24 00:11:58 +03:00
|
|
|
defer func() {
|
|
|
|
err1 := rd.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
}()
|
2021-05-24 00:11:58 +03:00
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
var out io.Writer
|
|
|
|
if fname := string(c.PositionalArgs.Outfile); fname == "-" {
|
2021-05-24 00:11:58 +03:00
|
|
|
out = c.Stdout
|
2021-05-24 00:11:58 +03:00
|
|
|
} else {
|
2021-05-24 00:11:58 +03:00
|
|
|
outf, err := os.Create(fname)
|
2021-05-24 00:11:58 +03:00
|
|
|
if err != nil {
|
2021-05-24 00:11:58 +03:00
|
|
|
return fmt.Errorf("create: %w", err)
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
2021-05-24 00:11:58 +03:00
|
|
|
defer func() {
|
2021-05-24 00:11:58 +03:00
|
|
|
err1 := outf.Close()
|
2021-05-24 00:11:58 +03:00
|
|
|
if err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
}()
|
2021-05-24 00:11:58 +03:00
|
|
|
out = outf
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
return c.flattener(rd, out)
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|