2015-03-25 03:57:23 +00:00
package client
import (
"fmt"
"io"
2015-03-29 17:42:11 +00:00
"os"
2015-03-25 03:57:23 +00:00
2016-02-03 23:41:26 +00:00
"golang.org/x/net/context"
2015-05-05 04:18:28 +00:00
Cli "github.com/docker/docker/cli"
2015-03-25 03:57:23 +00:00
"github.com/docker/docker/opts"
2015-12-03 22:11:37 +00:00
"github.com/docker/docker/pkg/jsonmessage"
2015-03-25 03:57:23 +00:00
flag "github.com/docker/docker/pkg/mflag"
2015-03-29 17:42:11 +00:00
"github.com/docker/docker/pkg/urlutil"
2015-12-04 21:55:15 +00:00
"github.com/docker/docker/reference"
2016-01-05 00:05:26 +00:00
"github.com/docker/engine-api/types"
2015-03-25 03:57:23 +00:00
)
2015-03-25 17:34:41 +00:00
// CmdImport creates an empty filesystem image, imports the contents of the tarball into the image, and optionally tags the image.
//
2015-03-29 17:42:11 +00:00
// The URL argument is the address of a tarball (.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz) file or a path to local file relative to docker client. If the URL is '-', then the tar file is read from STDIN.
2015-03-25 17:34:41 +00:00
//
2015-03-29 17:42:11 +00:00
// Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
2015-03-25 03:57:23 +00:00
func ( cli * DockerCli ) CmdImport ( args ... string ) error {
2015-10-08 12:46:21 +00:00
cmd := Cli . Subcmd ( "import" , [ ] string { "file|URL|- [REPOSITORY[:TAG]]" } , Cli . DockerCommands [ "import" ] . Description , true )
2015-03-25 03:57:23 +00:00
flChanges := opts . NewListOpts ( nil )
cmd . Var ( & flChanges , [ ] string { "c" , "-change" } , "Apply Dockerfile instruction to the created image" )
2015-08-20 04:01:50 +00:00
message := cmd . String ( [ ] string { "m" , "-message" } , "" , "Set commit message for imported image" )
2015-03-25 03:57:23 +00:00
cmd . Require ( flag . Min , 1 )
2015-03-29 01:22:46 +00:00
cmd . ParseFlags ( args , true )
2015-03-25 03:57:23 +00:00
var (
2015-12-03 22:11:37 +00:00
in io . Reader
tag string
2015-03-25 03:57:23 +00:00
src = cmd . Arg ( 0 )
2015-12-03 22:11:37 +00:00
srcName = src
2015-03-25 03:57:23 +00:00
repository = cmd . Arg ( 1 )
2015-12-03 22:11:37 +00:00
changes = flChanges . GetAll ( )
2015-03-25 03:57:23 +00:00
)
if cmd . NArg ( ) == 3 {
2015-03-29 17:42:11 +00:00
fmt . Fprintf ( cli . err , "[DEPRECATED] The format 'file|URL|- [REPOSITORY [TAG]]' has been deprecated. Please use file|URL|- [REPOSITORY[:TAG]]\n" )
2015-12-03 22:11:37 +00:00
tag = cmd . Arg ( 2 )
2015-03-25 03:57:23 +00:00
}
if repository != "" {
//Check if the given image name can be resolved
2015-12-11 19:00:13 +00:00
if _ , err := reference . ParseNamed ( repository ) ; err != nil {
2015-03-25 03:57:23 +00:00
return err
}
}
if src == "-" {
in = cli . in
2015-03-29 17:42:11 +00:00
} else if ! urlutil . IsURL ( src ) {
2015-12-03 22:11:37 +00:00
srcName = "-"
2015-03-29 17:42:11 +00:00
file , err := os . Open ( src )
if err != nil {
return err
}
defer file . Close ( )
in = file
2015-03-25 03:57:23 +00:00
}
2015-12-04 22:02:06 +00:00
options := types . ImageImportOptions {
2015-12-03 22:11:37 +00:00
Source : in ,
SourceName : srcName ,
RepositoryName : repository ,
Message : * message ,
Tag : tag ,
Changes : changes ,
}
2016-02-03 23:41:26 +00:00
responseBody , err := cli . client . ImageImport ( context . Background ( ) , options )
2015-12-03 22:11:37 +00:00
if err != nil {
return err
2015-05-01 18:23:44 +00:00
}
2015-12-03 22:11:37 +00:00
defer responseBody . Close ( )
2015-05-01 18:23:44 +00:00
2015-12-21 23:02:44 +00:00
return jsonmessage . DisplayJSONMessagesStream ( responseBody , cli . out , cli . outFd , cli . isTerminalOut , nil )
2015-03-25 03:57:23 +00:00
}