2015-03-24 23:57:23 -04:00
package client
import (
"fmt"
"io"
"net/url"
2015-03-29 13:42:11 -04:00
"os"
2015-03-24 23:57:23 -04:00
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"
flag "github.com/docker/docker/pkg/mflag"
"github.com/docker/docker/pkg/parsers"
2015-03-29 13:42:11 -04:00
"github.com/docker/docker/pkg/urlutil"
2015-03-24 23:57:23 -04:00
"github.com/docker/docker/registry"
)
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-05-05 00:18:28 -04:00
cmd := Cli . Subcmd ( "import" , [ ] string { "file|URL|- [REPOSITORY[:TAG]]" } , "Create an empty filesystem image and import the contents of the\ntarball (.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz) into it, then\noptionally tag it." , 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 (
v = url . Values { }
src = cmd . Arg ( 0 )
repository = cmd . Arg ( 1 )
)
v . Set ( "fromSrc" , src )
v . Set ( "repo" , repository )
2015-08-20 00:01:50 -04:00
v . Set ( "message" , * message )
2015-03-24 23:57:23 -04:00
for _ , change := range flChanges . GetAll ( ) {
v . Add ( "changes" , change )
}
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-03-24 23:57:23 -04:00
v . Set ( "tag" , cmd . Arg ( 2 ) )
}
if repository != "" {
//Check if the given image name can be resolved
repo , _ := parsers . ParseRepositoryTag ( repository )
if err := registry . ValidateRepositoryName ( repo ) ; err != nil {
return err
}
}
var in io . Reader
if src == "-" {
in = cli . in
2015-03-29 13:42:11 -04:00
} else if ! urlutil . IsURL ( src ) {
v . Set ( "fromSrc" , "-" )
file , err := os . Open ( src )
if err != nil {
return err
}
defer file . Close ( )
in = file
2015-03-24 23:57:23 -04:00
}
2015-05-01 14:23:44 -04:00
sopts := & streamOpts {
rawTerminal : true ,
in : in ,
out : cli . out ,
}
2015-06-04 09:30:14 -04:00
_ , err := cli . stream ( "POST" , "/images/create?" + v . Encode ( ) , sopts )
return err
2015-03-24 23:57:23 -04:00
}