2021-05-24 00:11:57 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-05-24 00:11:58 +03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-05-24 00:11:57 +03:00
|
|
|
"os"
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
"git.sr.ht/~motiejus/code/undocker/rootfs"
|
2021-05-24 00:11:57 +03:00
|
|
|
goflags "github.com/jessevdk/go-flags"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
func main() {
|
|
|
|
parser := goflags.NewParser(newCommand(), goflags.Default)
|
2021-05-24 00:11:58 +03:00
|
|
|
_, err := parser.Parse()
|
2021-05-24 00:11:58 +03:00
|
|
|
if err != nil {
|
2021-05-24 00:11:57 +03:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2021-05-24 00:11:58 +03:00
|
|
|
|
|
|
|
// command implements go-flags.Command
|
|
|
|
type command struct {
|
|
|
|
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 '-'"`
|
|
|
|
} `positional-args:"yes" required:"yes"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// newCommand creates a new Command struct
|
|
|
|
func newCommand() *command {
|
|
|
|
return &command{
|
|
|
|
flattener: rootfs.Flatten,
|
|
|
|
Stdout: os.Stdout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute executes rootfs Command
|
|
|
|
func (c *command) Execute(args []string) (err error) {
|
|
|
|
if len(args) != 0 {
|
|
|
|
return errors.New("too many args")
|
|
|
|
}
|
|
|
|
|
|
|
|
rd, err := os.Open(string(c.PositionalArgs.Infile))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err1 := rd.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var out io.Writer
|
|
|
|
if fname := string(c.PositionalArgs.Outfile); fname == "-" {
|
|
|
|
out = c.Stdout
|
|
|
|
} else {
|
|
|
|
outf, err := os.Create(fname)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create: %w", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err1 := outf.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
out = outf
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.flattener(rd, out)
|
|
|
|
}
|