2016-06-10 06:07:28 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2017-01-11 16:54:52 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-06-10 06:07:28 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2016-06-10 06:07:28 -04:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
|
|
|
"github.com/docker/docker/registry"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewPushCommand creates a new `docker push` command
|
2016-09-08 13:11:39 -04:00
|
|
|
func NewPushCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-06-10 06:07:28 -04:00
|
|
|
cmd := &cobra.Command{
|
2016-06-19 04:26:23 -04:00
|
|
|
Use: "push [OPTIONS] NAME[:TAG]",
|
2016-06-10 06:07:28 -04:00
|
|
|
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-06-10 06:07:28 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runPush(dockerCli *command.DockerCli, remote string) error {
|
2017-01-11 16:54:52 -05:00
|
|
|
ref, err := reference.ParseNormalizedNamed(remote)
|
2016-06-10 06:07:28 -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-06-10 06:07:28 -04:00
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
if command.IsTrusted() {
|
2016-08-29 14:45:29 -04:00
|
|
|
return trustedPush(ctx, dockerCli, repoInfo, ref, authConfig, requestPrivilege)
|
2016-06-10 06:07:28 -04:00
|
|
|
}
|
|
|
|
|
2017-01-11 16:54:52 -05:00
|
|
|
responseBody, err := imagePushPrivileged(ctx, dockerCli, authConfig, ref, requestPrivilege)
|
2016-06-10 06:07:28 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer responseBody.Close()
|
2016-08-29 11:11:29 -04:00
|
|
|
return jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil)
|
2016-06-10 06:07:28 -04:00
|
|
|
}
|