2015-03-24 23:57:23 -04:00
package client
import (
"fmt"
"io"
2015-03-29 13:42:11 -04:00
"os"
2015-03-24 23:57:23 -04:00
2015-12-04 17:02:06 -05:00
"github.com/docker/docker/api/types"
2015-05-05 00:18:28 -04:00
Cli "github.com/docker/docker/cli"
2015-03-24 23:57:23 -04:00
"github.com/docker/docker/opts"
2015-12-03 17:11:37 -05:00
"github.com/docker/docker/pkg/jsonmessage"
2015-03-24 23:57:23 -04:00
flag "github.com/docker/docker/pkg/mflag"
2015-03-29 13:42:11 -04:00
"github.com/docker/docker/pkg/urlutil"
2015-12-04 16:55:15 -05:00
"github.com/docker/docker/reference"
2015-03-24 23:57:23 -04:00
)
2015-03-25 13:34:41 -04:00
// CmdImport creates an empty filesystem image, imports the contents of the tarball into the image, and optionally tags the image.
//
2015-03-29 13:42:11 -04: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 13:34:41 -04:00
//
2015-03-29 13:42:11 -04:00
// Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
2015-03-24 23:57:23 -04:00
func ( cli * DockerCli ) CmdImport ( args ... string ) error {
2015-10-08 08:46:21 -04:00
cmd := Cli . Subcmd ( "import" , [ ] string { "file|URL|- [REPOSITORY[:TAG]]" } , Cli . DockerCommands [ "import" ] . Description , true )
2015-03-24 23:57:23 -04:00
flChanges := opts . NewListOpts ( nil )
cmd . Var ( & flChanges , [ ] string { "c" , "-change" } , "Apply Dockerfile instruction to the created image" )
2015-08-20 00:01:50 -04:00
message := cmd . String ( [ ] string { "m" , "-message" } , "" , "Set commit message for imported image" )
2015-03-24 23:57:23 -04:00
cmd . Require ( flag . Min , 1 )
2015-03-28 21:22:46 -04:00
cmd . ParseFlags ( args , true )
2015-03-24 23:57:23 -04:00
var (
2015-12-03 17:11:37 -05:00
in io . Reader
tag string
2015-03-24 23:57:23 -04:00
src = cmd . Arg ( 0 )
2015-12-03 17:11:37 -05:00
srcName = src
2015-03-24 23:57:23 -04:00
repository = cmd . Arg ( 1 )
2015-12-03 17:11:37 -05:00
changes = flChanges . GetAll ( )
2015-03-24 23:57:23 -04:00
)
if cmd . NArg ( ) == 3 {
2015-03-29 13:42:11 -04: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 17:11:37 -05:00
tag = cmd . Arg ( 2 )
2015-03-24 23:57:23 -04:00
}
if repository != "" {
//Check if the given image name can be resolved
2015-12-11 14:00:13 -05:00
if _ , err := reference . ParseNamed ( repository ) ; err != nil {
2015-03-24 23:57:23 -04:00
return err
}
}
if src == "-" {
in = cli . in
2015-03-29 13:42:11 -04:00
} else if ! urlutil . IsURL ( src ) {
2015-12-03 17:11:37 -05:00
srcName = "-"
2015-03-29 13:42:11 -04:00
file , err := os . Open ( src )
if err != nil {
return err
}
defer file . Close ( )
in = file
2015-03-24 23:57:23 -04:00
}
2015-12-04 17:02:06 -05:00
options := types . ImageImportOptions {
2015-12-03 17:11:37 -05:00
Source : in ,
SourceName : srcName ,
RepositoryName : repository ,
Message : * message ,
Tag : tag ,
Changes : changes ,
}
2015-12-04 17:02:06 -05:00
responseBody , err := cli . client . ImageImport ( options )
2015-12-03 17:11:37 -05:00
if err != nil {
return err
2015-05-01 14:23:44 -04:00
}
2015-12-03 17:11:37 -05:00
defer responseBody . Close ( )
2015-05-01 14:23:44 -04:00
2015-12-03 17:11:37 -05:00
return jsonmessage . DisplayJSONMessagesStream ( responseBody , cli . out , cli . outFd , cli . isTerminalOut )
2015-03-24 23:57:23 -04:00
}