2021-05-24 00:11:58 +03:00
|
|
|
package cmdlxcconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2021-05-24 00:11:58 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-05-24 00:11:58 +03:00
|
|
|
"os"
|
|
|
|
|
|
|
|
goflags "github.com/jessevdk/go-flags"
|
|
|
|
"github.com/motiejus/code/undocker/lxcconfig"
|
|
|
|
)
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
const _description = "Create an LXC-compatible container configuration"
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
type (
|
2021-05-24 00:11:58 +03:00
|
|
|
// Command is an implementation of go-flags.Command
|
2021-05-24 00:11:58 +03:00
|
|
|
Command struct {
|
2021-05-24 00:11:58 +03:00
|
|
|
configer func(io.ReadSeeker, io.Writer) error
|
|
|
|
Stdout io.Writer
|
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
|
|
|
func NewCommand() *Command {
|
|
|
|
return &Command{
|
2021-05-24 00:11:58 +03:00
|
|
|
configer: lxcconfig.LXCConfig,
|
|
|
|
Stdout: os.Stdout,
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
func (*Command) ShortDesc() string { return _description }
|
|
|
|
func (*Command) LongDesc() string { return _description }
|
2021-05-24 00:11:58 +03:00
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
// Execute executes lxcconfig 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
|
|
|
|
}
|
2021-05-24 00:11:58 +03:00
|
|
|
defer func() {
|
|
|
|
err1 := rd.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
}()
|
2021-05-24 00:11:58 +03:00
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
var out io.Writer
|
2021-05-24 00:11:58 +03:00
|
|
|
outf := string(c.PositionalArgs.Outfile)
|
2021-05-24 00:11:58 +03:00
|
|
|
if fname := string(c.PositionalArgs.Outfile); fname == "-" {
|
|
|
|
out = c.Stdout
|
2021-05-24 00:11:58 +03:00
|
|
|
} else {
|
2021-05-24 00:11:58 +03:00
|
|
|
outf, err := os.Create(outf)
|
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() {
|
|
|
|
err1 := outf.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
}()
|
2021-05-24 00:11:58 +03:00
|
|
|
out = outf
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
return c.configer(rd, out)
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|