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"
|
2021-05-24 00:11:58 +03:00
|
|
|
"github.com/motiejus/code/undocker/internal/cmd"
|
2021-05-24 00:11:58 +03:00
|
|
|
"github.com/motiejus/code/undocker/rootfs"
|
|
|
|
"go.uber.org/multierr"
|
|
|
|
)
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
type (
|
|
|
|
rootfsFactory func(io.ReadSeeker) io.WriterTo
|
2021-05-24 00:11:58 +03:00
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
// Command is "rootfs" command
|
|
|
|
Command struct {
|
|
|
|
cmd.BaseCommand
|
2021-05-24 00:11:58 +03:00
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
PositionalArgs struct {
|
|
|
|
Infile goflags.Filename `long:"infile" description:"Input tarball"`
|
|
|
|
Outfile string `long:"outfile" description:"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
|
|
|
rootfsNew rootfsFactory
|
|
|
|
}
|
|
|
|
)
|
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
|
|
|
if c.rootfsNew == nil {
|
2021-05-24 00:11:58 +03:00
|
|
|
c.init()
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
2021-05-24 00:11:58 +03:00
|
|
|
|
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() { err = multierr.Append(err, rd.Close()) }()
|
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() { err = multierr.Append(err, outf.Close()) }()
|
|
|
|
out = outf
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := c.rootfsNew(rd).WriteTo(out); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
2021-05-24 00:11:58 +03:00
|
|
|
|
|
|
|
// 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.rootfsNew = func(r io.ReadSeeker) io.WriterTo {
|
|
|
|
return rootfs.New(r)
|
|
|
|
}
|
|
|
|
}
|