2016-09-08 13:11:39 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
"os"
|
2022-04-12 11:16:02 -04:00
|
|
|
"strings"
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2017-05-15 08:45:19 -04:00
|
|
|
dockeropts "github.com/docker/cli/opts"
|
2024-01-24 08:32:07 -05:00
|
|
|
"github.com/docker/docker/api/types/image"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type importOptions struct {
|
|
|
|
source string
|
|
|
|
reference string
|
|
|
|
changes dockeropts.ListOpts
|
|
|
|
message string
|
2018-09-13 13:56:01 -04:00
|
|
|
platform string
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewImportCommand creates a new `docker import` command
|
2017-03-30 20:21:14 -04:00
|
|
|
func NewImportCommand(dockerCli command.Cli) *cobra.Command {
|
2017-05-15 08:45:19 -04:00
|
|
|
var options importOptions
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
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 {
|
2017-05-15 08:45:19 -04:00
|
|
|
options.source = args[0]
|
2016-09-08 13:11:39 -04:00
|
|
|
if len(args) > 1 {
|
2017-05-15 08:45:19 -04:00
|
|
|
options.reference = args[1]
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2023-09-09 18:27:44 -04:00
|
|
|
return runImport(cmd.Context(), dockerCli, options)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
cli: use custom annotation for aliases
Cobra allows for aliases to be defined for a command, but only allows these
to be defined at the same level (for example, `docker image ls` as alias for
`docker image list`). Our CLI has some commands that are available both as a
top-level shorthand as well as `docker <object> <verb>` subcommands. For example,
`docker ps` is a shorthand for `docker container ps` / `docker container ls`.
This patch introduces a custom "aliases" annotation that can be used to print
all available aliases for a command. While this requires these aliases to be
defined manually, in practice the list of aliases rarely changes, so maintenance
should be minimal.
As a convention, we could consider the first command in this list to be the
canonical command, so that we can use this information to add redirects in
our documentation in future.
Before this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
...
With this patch:
docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker images
Options:
-a, --all Show all images (default hides intermediate images)
...
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-06-28 04:52:25 -04:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"aliases": "docker image import, docker import",
|
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
options.changes = dockeropts.NewListOpts(nil)
|
|
|
|
flags.VarP(&options.changes, "change", "c", "Apply Dockerfile instruction to the created image")
|
|
|
|
flags.StringVarP(&options.message, "message", "m", "", "Set commit message for imported image")
|
2018-09-13 13:56:01 -04:00
|
|
|
command.AddPlatformFlag(flags, &options.platform)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2023-09-09 18:27:44 -04:00
|
|
|
func runImport(ctx context.Context, dockerCli command.Cli, options importOptions) error {
|
2024-06-09 07:54:37 -04:00
|
|
|
var source image.ImportSource
|
2022-04-12 11:16:02 -04:00
|
|
|
switch {
|
|
|
|
case options.source == "-":
|
|
|
|
// import from STDIN
|
2024-06-09 07:54:37 -04:00
|
|
|
source = image.ImportSource{
|
2022-04-12 11:16:02 -04:00
|
|
|
Source: dockerCli.In(),
|
|
|
|
SourceName: options.source,
|
|
|
|
}
|
|
|
|
case strings.HasPrefix(options.source, "https://"), strings.HasPrefix(options.source, "http://"):
|
|
|
|
// import from a remote source (handled by the daemon)
|
2024-06-09 07:54:37 -04:00
|
|
|
source = image.ImportSource{
|
2022-04-12 11:16:02 -04:00
|
|
|
SourceName: options.source,
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// import from a local file
|
2017-05-15 08:45:19 -04:00
|
|
|
file, err := os.Open(options.source)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2024-06-09 07:54:37 -04:00
|
|
|
source = image.ImportSource{
|
2022-04-12 11:16:02 -04:00
|
|
|
Source: file,
|
|
|
|
SourceName: "-",
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2024-01-24 08:32:07 -05:00
|
|
|
responseBody, err := dockerCli.Client().ImageImport(ctx, source, options.reference, image.ImportOptions{
|
2018-09-13 13:56:01 -04:00
|
|
|
Message: options.message,
|
|
|
|
Changes: options.changes.GetAll(),
|
|
|
|
Platform: options.platform,
|
2022-04-12 11:16:02 -04:00
|
|
|
})
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer responseBody.Close()
|
|
|
|
|
|
|
|
return jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil)
|
|
|
|
}
|