mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #23412 from vdemeester/migrate-load-and-save-to-cobra
Use spf13/cobra for docker load and save
This commit is contained in:
commit
a05536ff42
7 changed files with 126 additions and 96 deletions
|
@ -8,13 +8,11 @@ func (cli *DockerCli) Command(name string) func(...string) error {
|
||||||
"exec": cli.CmdExec,
|
"exec": cli.CmdExec,
|
||||||
"info": cli.CmdInfo,
|
"info": cli.CmdInfo,
|
||||||
"inspect": cli.CmdInspect,
|
"inspect": cli.CmdInspect,
|
||||||
"load": cli.CmdLoad,
|
|
||||||
"login": cli.CmdLogin,
|
"login": cli.CmdLogin,
|
||||||
"logout": cli.CmdLogout,
|
"logout": cli.CmdLogout,
|
||||||
"ps": cli.CmdPs,
|
"ps": cli.CmdPs,
|
||||||
"pull": cli.CmdPull,
|
"pull": cli.CmdPull,
|
||||||
"push": cli.CmdPush,
|
"push": cli.CmdPush,
|
||||||
"save": cli.CmdSave,
|
|
||||||
"update": cli.CmdUpdate,
|
"update": cli.CmdUpdate,
|
||||||
}[name]
|
}[name]
|
||||||
}
|
}
|
||||||
|
|
67
api/client/image/load.go
Normal file
67
api/client/image/load.go
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
package image
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
|
"github.com/docker/docker/api/client"
|
||||||
|
"github.com/docker/docker/cli"
|
||||||
|
"github.com/docker/docker/pkg/jsonmessage"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
type loadOptions struct {
|
||||||
|
input string
|
||||||
|
quiet bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewLoadCommand creates a new `docker load` command
|
||||||
|
func NewLoadCommand(dockerCli *client.DockerCli) *cobra.Command {
|
||||||
|
var opts loadOptions
|
||||||
|
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "load [OPTIONS]",
|
||||||
|
Short: "Load an image from a tar archive or STDIN",
|
||||||
|
Args: cli.NoArgs,
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
return runLoad(dockerCli, opts)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
flags := cmd.Flags()
|
||||||
|
|
||||||
|
flags.StringVarP(&opts.input, "input", "i", "", "Read from tar archive file, instead of STDIN")
|
||||||
|
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Suppress the load output")
|
||||||
|
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
func runLoad(dockerCli *client.DockerCli, opts loadOptions) error {
|
||||||
|
|
||||||
|
var input io.Reader = dockerCli.In()
|
||||||
|
if opts.input != "" {
|
||||||
|
file, err := os.Open(opts.input)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
input = file
|
||||||
|
}
|
||||||
|
if !dockerCli.IsTerminalOut() {
|
||||||
|
opts.quiet = true
|
||||||
|
}
|
||||||
|
response, err := dockerCli.Client().ImageLoad(context.Background(), input, opts.quiet)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer response.Body.Close()
|
||||||
|
|
||||||
|
if response.Body != nil && response.JSON {
|
||||||
|
return jsonmessage.DisplayJSONMessagesStream(response.Body, dockerCli.Out(), dockerCli.OutFd(), dockerCli.IsTerminalOut(), nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = io.Copy(dockerCli.Out(), response.Body)
|
||||||
|
return err
|
||||||
|
}
|
57
api/client/image/save.go
Normal file
57
api/client/image/save.go
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
package image
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
|
"github.com/docker/docker/api/client"
|
||||||
|
"github.com/docker/docker/cli"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
type saveOptions struct {
|
||||||
|
images []string
|
||||||
|
output string
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewSaveCommand creates a new `docker save` command
|
||||||
|
func NewSaveCommand(dockerCli *client.DockerCli) *cobra.Command {
|
||||||
|
var opts saveOptions
|
||||||
|
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "save [OPTIONS] IMAGE [IMAGE...]",
|
||||||
|
Short: "Save one or more images to a tar archive (streamed to STDOUT by default)",
|
||||||
|
Args: cli.RequiresMinArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
opts.images = args
|
||||||
|
return runSave(dockerCli, opts)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
flags := cmd.Flags()
|
||||||
|
|
||||||
|
flags.StringVarP(&opts.output, "output", "o", "", "Write to a file, instead of STDOUT")
|
||||||
|
|
||||||
|
return cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
func runSave(dockerCli *client.DockerCli, opts saveOptions) error {
|
||||||
|
if opts.output == "" && dockerCli.IsTerminalOut() {
|
||||||
|
return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
|
||||||
|
}
|
||||||
|
|
||||||
|
responseBody, err := dockerCli.Client().ImageSave(context.Background(), opts.images)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer responseBody.Close()
|
||||||
|
|
||||||
|
if opts.output == "" {
|
||||||
|
_, err := io.Copy(dockerCli.Out(), responseBody)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.CopyToFile(opts.output, responseBody)
|
||||||
|
}
|
|
@ -1,50 +0,0 @@
|
||||||
package client
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
|
||||||
|
|
||||||
Cli "github.com/docker/docker/cli"
|
|
||||||
"github.com/docker/docker/pkg/jsonmessage"
|
|
||||||
flag "github.com/docker/docker/pkg/mflag"
|
|
||||||
)
|
|
||||||
|
|
||||||
// CmdLoad loads an image from a tar archive.
|
|
||||||
//
|
|
||||||
// The tar archive is read from STDIN by default, or from a tar archive file.
|
|
||||||
//
|
|
||||||
// Usage: docker load [OPTIONS]
|
|
||||||
func (cli *DockerCli) CmdLoad(args ...string) error {
|
|
||||||
cmd := Cli.Subcmd("load", nil, Cli.DockerCommands["load"].Description, true)
|
|
||||||
infile := cmd.String([]string{"i", "-input"}, "", "Read from a tar archive file, instead of STDIN")
|
|
||||||
quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Suppress the load output")
|
|
||||||
cmd.Require(flag.Exact, 0)
|
|
||||||
cmd.ParseFlags(args, true)
|
|
||||||
|
|
||||||
var input io.Reader = cli.in
|
|
||||||
if *infile != "" {
|
|
||||||
file, err := os.Open(*infile)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
input = file
|
|
||||||
}
|
|
||||||
if !cli.isTerminalOut {
|
|
||||||
*quiet = true
|
|
||||||
}
|
|
||||||
response, err := cli.client.ImageLoad(context.Background(), input, *quiet)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer response.Body.Close()
|
|
||||||
|
|
||||||
if response.Body != nil && response.JSON {
|
|
||||||
return jsonmessage.DisplayJSONMessagesStream(response.Body, cli.out, cli.outFd, cli.isTerminalOut, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = io.Copy(cli.out, response.Body)
|
|
||||||
return err
|
|
||||||
}
|
|
|
@ -1,42 +0,0 @@
|
||||||
package client
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
|
||||||
|
|
||||||
Cli "github.com/docker/docker/cli"
|
|
||||||
flag "github.com/docker/docker/pkg/mflag"
|
|
||||||
)
|
|
||||||
|
|
||||||
// CmdSave saves one or more images to a tar archive.
|
|
||||||
//
|
|
||||||
// The tar archive is written to STDOUT by default, or written to a file.
|
|
||||||
//
|
|
||||||
// Usage: docker save [OPTIONS] IMAGE [IMAGE...]
|
|
||||||
func (cli *DockerCli) CmdSave(args ...string) error {
|
|
||||||
cmd := Cli.Subcmd("save", []string{"IMAGE [IMAGE...]"}, Cli.DockerCommands["save"].Description+" (streamed to STDOUT by default)", true)
|
|
||||||
outfile := cmd.String([]string{"o", "-output"}, "", "Write to a file, instead of STDOUT")
|
|
||||||
cmd.Require(flag.Min, 1)
|
|
||||||
|
|
||||||
cmd.ParseFlags(args, true)
|
|
||||||
|
|
||||||
if *outfile == "" && cli.isTerminalOut {
|
|
||||||
return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
|
|
||||||
}
|
|
||||||
|
|
||||||
responseBody, err := cli.client.ImageSave(context.Background(), cmd.Args())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer responseBody.Close()
|
|
||||||
|
|
||||||
if *outfile == "" {
|
|
||||||
_, err := io.Copy(cli.out, responseBody)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return CopyToFile(*outfile, responseBody)
|
|
||||||
|
|
||||||
}
|
|
|
@ -56,7 +56,9 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
|
||||||
image.NewBuildCommand(dockerCli),
|
image.NewBuildCommand(dockerCli),
|
||||||
image.NewHistoryCommand(dockerCli),
|
image.NewHistoryCommand(dockerCli),
|
||||||
image.NewImagesCommand(dockerCli),
|
image.NewImagesCommand(dockerCli),
|
||||||
|
image.NewLoadCommand(dockerCli),
|
||||||
image.NewRemoveCommand(dockerCli),
|
image.NewRemoveCommand(dockerCli),
|
||||||
|
image.NewSaveCommand(dockerCli),
|
||||||
image.NewSearchCommand(dockerCli),
|
image.NewSearchCommand(dockerCli),
|
||||||
image.NewImportCommand(dockerCli),
|
image.NewImportCommand(dockerCli),
|
||||||
image.NewTagCommand(dockerCli),
|
image.NewTagCommand(dockerCli),
|
||||||
|
|
|
@ -13,13 +13,11 @@ var DockerCommandUsage = []Command{
|
||||||
{"exec", "Run a command in a running container"},
|
{"exec", "Run a command in a running container"},
|
||||||
{"info", "Display system-wide information"},
|
{"info", "Display system-wide information"},
|
||||||
{"inspect", "Return low-level information on a container or image"},
|
{"inspect", "Return low-level information on a container or image"},
|
||||||
{"load", "Load an image from a tar archive or STDIN"},
|
|
||||||
{"login", "Log in to a Docker registry"},
|
{"login", "Log in to a Docker registry"},
|
||||||
{"logout", "Log out from a Docker registry"},
|
{"logout", "Log out from a Docker registry"},
|
||||||
{"ps", "List containers"},
|
{"ps", "List containers"},
|
||||||
{"pull", "Pull an image or a repository from a registry"},
|
{"pull", "Pull an image or a repository from a registry"},
|
||||||
{"push", "Push an image or a repository to a registry"},
|
{"push", "Push an image or a repository to a registry"},
|
||||||
{"save", "Save one or more images to a tar archive"},
|
|
||||||
{"update", "Update configuration of one or more containers"},
|
{"update", "Update configuration of one or more containers"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue