undocker/main.go

73 lines
1.4 KiB
Go
Raw Normal View History

2021-05-24 00:11:57 +03:00
package main
import (
2021-05-24 00:11:57 +03:00
"errors"
2021-05-24 00:11:57 +03:00
"os"
goflags "github.com/jessevdk/go-flags"
2021-05-24 00:11:57 +03:00
"github.com/motiejus/code/undocker/rootfs"
2021-05-24 00:11:58 +03:00
"go.uber.org/multierr"
2021-05-24 00:11:57 +03:00
)
2021-05-24 00:11:57 +03:00
type (
params struct {
RootFS cmdRootFS `command:"rootfs" description:"Unpack a docker container image to a single filesystem tarball"`
Manifest cmdManifest `command:"manifest"`
}
cmdManifest struct{} // stub
)
2021-05-24 00:11:57 +03:00
func main() {
if err := run(os.Args); err != nil {
os.Exit(1)
}
os.Exit(0)
}
func run(args []string) error {
2021-05-24 00:11:57 +03:00
var opts params
if _, err := goflags.ParseArgs(&opts, args[1:]); err != nil {
2021-05-24 00:11:57 +03:00
return err
}
return nil
}
2021-05-24 00:11:57 +03:00
type cmdRootFS struct {
PositionalArgs struct {
Infile goflags.Filename `long:"infile" description:"Input tarball"`
2021-05-24 00:11:58 +03:00
Outfile string `long:"outfile" description:"Output path, stdout is '-'"`
2021-05-24 00:11:57 +03:00
} `positional-args:"yes" required:"yes"`
}
2021-05-24 00:11:58 +03:00
func (r *cmdRootFS) Execute(args []string) (err error) {
2021-05-24 00:11:57 +03:00
if len(args) != 0 {
return errors.New("too many args")
}
in, err := os.Open(string(r.PositionalArgs.Infile))
if err != nil {
return err
}
2021-05-24 00:11:58 +03:00
defer func() {
err = multierr.Append(err, in.Close())
}()
2021-05-24 00:11:57 +03:00
2021-05-24 00:11:58 +03:00
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
}
2021-05-24 00:11:57 +03:00
}
2021-05-24 00:11:58 +03:00
defer func() {
err = multierr.Append(err, out.Close())
}()
2021-05-24 00:11:57 +03:00
return rootfs.RootFS(in, out)
2021-05-24 00:11:57 +03:00
}