wip leave only rootfs

This commit is contained in:
2021-05-24 00:11:58 +03:00
parent b0cb6bb850
commit 9665574308
3 changed files with 170 additions and 17 deletions

76
main.go
View File

@@ -1,28 +1,78 @@
package main
import (
"errors"
"fmt"
"io"
"os"
"git.sr.ht/~motiejus/code/undocker/rootfs"
goflags "github.com/jessevdk/go-flags"
"git.sr.ht/~motiejus/code/undocker/internal/cmdlxcconfig"
"git.sr.ht/~motiejus/code/undocker/internal/cmdmanpage"
"git.sr.ht/~motiejus/code/undocker/internal/cmdrootfs"
)
const _description = "Flatten a docker container image to a tarball"
func main() {
parser := goflags.NewParser(nil, goflags.Default)
rootfs := cmdrootfs.NewCommand()
lxcconfig := cmdlxcconfig.NewCommand()
manpage := cmdmanpage.NewCommand(parser)
parser.AddCommand("rootfs", rootfs.ShortDesc(), rootfs.LongDesc(), rootfs)
parser.AddCommand("lxcconfig", lxcconfig.ShortDesc(), lxcconfig.LongDesc(), lxcconfig)
m, _ := parser.AddCommand("man-page", "", "", manpage)
m.Hidden = true
parser := goflags.NewParser(newCommand(), goflags.Default)
_, err := parser.Parse()
if err != nil {
os.Exit(1)
}
os.Exit(0)
}
// 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)
}