2016-09-08 13:11:39 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2017-01-11 16:54:52 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
|
|
|
"github.com/docker/docker/registry"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewPushCommand creates a new `docker push` command
|
2017-03-30 20:21:14 -04:00
|
|
|
func NewPushCommand(dockerCli command.Cli) *cobra.Command {
|
2016-09-08 13:11:39 -04:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "push [OPTIONS] NAME[:TAG]",
|
|
|
|
Short: "Push an image or a repository to a registry",
|
|
|
|
Args: cli.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runPush(dockerCli, args[0])
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
2017-01-17 09:46:07 -05:00
|
|
|
command.AddTrustSigningFlags(flags)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-03-30 20:21:14 -04:00
|
|
|
func runPush(dockerCli command.Cli, remote string) error {
|
2017-01-11 16:54:52 -05:00
|
|
|
ref, err := reference.ParseNormalizedNamed(remote)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve the Repository name from fqn to RepositoryInfo
|
|
|
|
repoInfo, err := registry.ParseRepositoryInfo(ref)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// Resolve the Auth config relevant for this server
|
2016-09-09 15:38:00 -04:00
|
|
|
authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
|
|
|
|
requestPrivilege := command.RegistryAuthenticationPrivilegedFunc(dockerCli, repoInfo.Index, "push")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
if command.IsTrusted() {
|
2017-08-24 18:42:21 -04:00
|
|
|
return TrustedPush(ctx, dockerCli, repoInfo, ref, authConfig, requestPrivilege)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2017-01-11 16:54:52 -05:00
|
|
|
responseBody, err := imagePushPrivileged(ctx, dockerCli, authConfig, ref, requestPrivilege)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer responseBody.Close()
|
|
|
|
return jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil)
|
|
|
|
}
|