forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request moby#23269 from vdemeester/migrate-import-to-cobra
Use spf13/cobra for docker import
- Loading branch information
Showing
9 changed files
with
107 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package image | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
"golang.org/x/net/context" | ||
|
||
"github.com/docker/docker/api/client" | ||
"github.com/docker/docker/cli" | ||
"github.com/docker/docker/pkg/jsonmessage" | ||
"github.com/docker/docker/pkg/urlutil" | ||
"github.com/docker/engine-api/types" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type importOptions struct { | ||
source string | ||
reference string | ||
changes []string | ||
message string | ||
} | ||
|
||
// NewImportCommand creates a new `docker import` command | ||
func NewImportCommand(dockerCli *client.DockerCli) *cobra.Command { | ||
var opts importOptions | ||
|
||
cmd := &cobra.Command{ | ||
Use: "import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]", | ||
Short: "Import the contents from a tarball to create a filesystem image", | ||
Args: cli.RequiresMinArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
opts.source = args[0] | ||
if len(args) > 1 { | ||
opts.reference = args[1] | ||
} | ||
return runImport(dockerCli, opts) | ||
}, | ||
} | ||
|
||
flags := cmd.Flags() | ||
|
||
flags.StringSliceVarP(&opts.changes, "change", "c", []string{}, "Apply Dockerfile instruction to the created image") | ||
flags.StringVarP(&opts.message, "message", "m", "", "Set commit message for imported image") | ||
|
||
return cmd | ||
} | ||
|
||
func runImport(dockerCli *client.DockerCli, opts importOptions) error { | ||
var ( | ||
in io.Reader | ||
srcName = opts.source | ||
) | ||
|
||
if opts.source == "-" { | ||
in = dockerCli.In() | ||
} else if !urlutil.IsURL(opts.source) { | ||
srcName = "-" | ||
file, err := os.Open(opts.source) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
in = file | ||
} | ||
|
||
source := types.ImageImportSource{ | ||
Source: in, | ||
SourceName: srcName, | ||
} | ||
|
||
options := types.ImageImportOptions{ | ||
Message: opts.message, | ||
Changes: opts.changes, | ||
} | ||
|
||
clnt := dockerCli.Client() | ||
|
||
responseBody, err := clnt.ImageImport(context.Background(), source, opts.reference, options) | ||
if err != nil { | ||
return err | ||
} | ||
defer responseBody.Close() | ||
|
||
return jsonmessage.DisplayJSONMessagesStream(responseBody, dockerCli.Out(), dockerCli.OutFd(), dockerCli.IsTerminalOut(), nil) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters