2021-05-24 00:11:58 +03:00
|
|
|
// Package main is a simple command-line application on top of rootfs.Flatten.
|
2021-05-24 00:11:57 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-08-29 16:55:32 +03:00
|
|
|
"flag"
|
2021-05-24 00:11:58 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-05-24 00:11:57 +03:00
|
|
|
"os"
|
2021-05-24 00:11:58 +03:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2021-05-24 00:11:57 +03:00
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
"git.sr.ht/~motiejus/undocker/rootfs"
|
2021-05-24 00:11:57 +03:00
|
|
|
)
|
|
|
|
|
2021-08-24 07:02:12 +03:00
|
|
|
var Version = "unknown"
|
|
|
|
var VersionHash = "unknown"
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
const _usage = `Usage:
|
2021-08-29 16:55:32 +03:00
|
|
|
%s [OPTION]... <infile> <outfile>
|
2021-05-24 00:11:58 +03:00
|
|
|
|
|
|
|
Flatten a Docker container image to a root file system.
|
|
|
|
|
|
|
|
Arguments:
|
2021-08-29 16:55:32 +03:00
|
|
|
<infile> Input Docker container. Tarball.
|
|
|
|
<outfile> Output tarball, the root file system. '-' is stdout.
|
|
|
|
|
|
|
|
Options:
|
|
|
|
--prefix=<prefix> prefix all destination files with a given string.
|
2021-08-24 07:02:12 +03:00
|
|
|
|
|
|
|
undocker %s (%s)
|
|
|
|
Built with %s
|
2021-05-24 00:11:58 +03:00
|
|
|
`
|
2021-05-24 00:11:58 +03:00
|
|
|
|
2021-08-29 16:55:32 +03:00
|
|
|
func usage(pre string, out io.Writer) {
|
|
|
|
fmt.Fprintf(out, pre+_usage,
|
|
|
|
filepath.Base(os.Args[0]),
|
|
|
|
Version,
|
|
|
|
VersionHash,
|
|
|
|
runtime.Version(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func usageErr(pre string) {
|
|
|
|
usage(pre, os.Stderr)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
func main() {
|
2021-05-24 00:11:58 +03:00
|
|
|
runtime.GOMAXPROCS(1) // no need to create that many threads
|
|
|
|
|
2021-08-29 16:55:32 +03:00
|
|
|
var filePrefix string
|
|
|
|
fs := flag.NewFlagSet("undocker", flag.ExitOnError)
|
|
|
|
fs.Usage = func() { usageErr("") }
|
|
|
|
fs.StringVar(&filePrefix, "prefix", "", "prefix files in the tarball")
|
|
|
|
|
|
|
|
if len(os.Args) == 1 {
|
|
|
|
usageErr("")
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = fs.Parse(os.Args[1:]) // ExitOnError captures it
|
|
|
|
|
|
|
|
args := fs.Args()
|
|
|
|
if len(args) != 2 {
|
|
|
|
usageErr("invalid number of arguments\n")
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
c := &command{flattener: rootfs.Flatten, Stdout: os.Stdout}
|
2021-08-29 16:55:32 +03:00
|
|
|
if err := c.execute(args[0], args[1], filePrefix); err != nil {
|
2021-08-19 16:32:22 +03:00
|
|
|
fmt.Printf("Error: %v\n", err)
|
2021-05-24 00:11:57 +03:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2021-05-24 00:11:58 +03:00
|
|
|
|
|
|
|
type command struct {
|
2021-08-29 16:55:32 +03:00
|
|
|
flattener func(io.ReadSeeker, io.Writer, ...rootfs.Option) error
|
2021-05-24 00:11:58 +03:00
|
|
|
Stdout io.Writer
|
|
|
|
}
|
|
|
|
|
2021-08-29 16:55:32 +03:00
|
|
|
func (c *command) execute(infile, outfile, filePrefix string) (_err error) {
|
2021-05-24 00:11:58 +03:00
|
|
|
rd, err := os.Open(infile)
|
2021-05-24 00:11:58 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
2021-08-25 08:49:30 +03:00
|
|
|
err := rd.Close()
|
|
|
|
if _err == nil {
|
|
|
|
_err = err
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var out io.Writer
|
2021-05-24 00:11:58 +03:00
|
|
|
if outfile == "-" {
|
2021-05-24 00:11:58 +03:00
|
|
|
out = c.Stdout
|
|
|
|
} else {
|
2021-05-24 00:11:58 +03:00
|
|
|
outf, err := os.Create(outfile)
|
2021-05-24 00:11:58 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("create: %w", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2021-08-25 08:49:30 +03:00
|
|
|
err := outf.Close()
|
|
|
|
if _err != nil {
|
|
|
|
os.Remove(outfile)
|
|
|
|
} else {
|
|
|
|
_err = err
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
out = outf
|
|
|
|
}
|
|
|
|
|
2021-08-29 16:55:32 +03:00
|
|
|
return c.flattener(rd, out, rootfs.WithFilePrefix(filePrefix))
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|