github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/command/context/import.go (about) 1 package context 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 8 "github.com/docker/cli/cli" 9 "github.com/docker/cli/cli/command" 10 "github.com/docker/cli/cli/context/store" 11 "github.com/spf13/cobra" 12 ) 13 14 func newImportCommand(dockerCli command.Cli) *cobra.Command { 15 cmd := &cobra.Command{ 16 Use: "import CONTEXT FILE|-", 17 Short: "Import a context from a tar or zip file", 18 Args: cli.ExactArgs(2), 19 RunE: func(cmd *cobra.Command, args []string) error { 20 return RunImport(dockerCli, args[0], args[1]) 21 }, 22 } 23 return cmd 24 } 25 26 // RunImport imports a Docker context 27 func RunImport(dockerCli command.Cli, name string, source string) error { 28 if err := checkContextNameForCreation(dockerCli.ContextStore(), name); err != nil { 29 return err 30 } 31 32 var reader io.Reader 33 if source == "-" { 34 reader = dockerCli.In() 35 } else { 36 f, err := os.Open(source) 37 if err != nil { 38 return err 39 } 40 defer f.Close() 41 reader = f 42 } 43 44 if err := store.Import(name, dockerCli.ContextStore(), reader); err != nil { 45 return err 46 } 47 48 fmt.Fprintln(dockerCli.Out(), name) 49 fmt.Fprintf(dockerCli.Err(), "Successfully imported context %q\n", name) 50 return nil 51 }