2018-11-09 09:10:41 -05:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/context/store"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newImportCommand(dockerCli command.Cli) *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "import CONTEXT FILE|-",
|
2019-05-14 16:14:39 -04:00
|
|
|
Short: "Import a context from a tar or zip file",
|
2018-11-09 09:10:41 -05:00
|
|
|
Args: cli.ExactArgs(2),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2019-01-21 03:37:20 -05:00
|
|
|
return RunImport(dockerCli, args[0], args[1])
|
2018-11-09 09:10:41 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2019-01-21 03:37:20 -05:00
|
|
|
// RunImport imports a Docker context
|
|
|
|
func RunImport(dockerCli command.Cli, name string, source string) error {
|
2018-11-09 09:10:41 -05:00
|
|
|
if err := checkContextNameForCreation(dockerCli.ContextStore(), name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-14 16:14:39 -04:00
|
|
|
|
2018-11-09 09:10:41 -05:00
|
|
|
var reader io.Reader
|
|
|
|
if source == "-" {
|
|
|
|
reader = dockerCli.In()
|
|
|
|
} else {
|
|
|
|
f, err := os.Open(source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
reader = f
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := store.Import(name, dockerCli.ContextStore(), reader); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-14 16:14:39 -04:00
|
|
|
|
2018-11-09 09:10:41 -05:00
|
|
|
fmt.Fprintln(dockerCli.Out(), name)
|
|
|
|
fmt.Fprintf(dockerCli.Err(), "Successfully imported context %q\n", name)
|
|
|
|
return nil
|
|
|
|
}
|