Skip to content

Commit

Permalink
Update client code with api changes
Browse files Browse the repository at this point in the history
Using new methods from engine-api, that make it clearer which element is
required when consuming the API.

Signed-off-by: Vincent Demeester <[email protected]>
  • Loading branch information
vdemeester committed Apr 15, 2016
1 parent 9802d7d commit b9c94b7
Show file tree
Hide file tree
Showing 26 changed files with 173 additions and 237 deletions.
19 changes: 10 additions & 9 deletions api/client/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ func (cli *DockerCli) CmdAttach(args ...string) error {
cli.configFile.DetachKeys = *detachKeys
}

container := cmd.Arg(0)

options := types.ContainerAttachOptions{
ContainerID: cmd.Arg(0),
Stream: true,
Stdin: !*noStdin && c.Config.OpenStdin,
Stdout: true,
Stderr: true,
DetachKeys: cli.configFile.DetachKeys,
Stream: true,
Stdin: !*noStdin && c.Config.OpenStdin,
Stdout: true,
Stderr: true,
DetachKeys: cli.configFile.DetachKeys,
}

var in io.ReadCloser
Expand All @@ -63,11 +64,11 @@ func (cli *DockerCli) CmdAttach(args ...string) error {
}

if *proxy && !c.Config.Tty {
sigc := cli.forwardAllSignals(options.ContainerID)
sigc := cli.forwardAllSignals(container)
defer signal.StopCatch(sigc)
}

resp, errAttach := cli.client.ContainerAttach(context.Background(), options)
resp, errAttach := cli.client.ContainerAttach(context.Background(), container, options)
if errAttach != nil && errAttach != httputil.ErrPersistEOF {
// ContainerAttach returns an ErrPersistEOF (connection closed)
// means server met an error and put it in Hijacked connection
Expand Down Expand Up @@ -98,7 +99,7 @@ func (cli *DockerCli) CmdAttach(args ...string) error {
return errAttach
}

_, status, err := getExitCode(cli, options.ContainerID)
_, status, err := getExitCode(cli, container)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions api/client/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
}

options := types.ImageBuildOptions{
Context: body,
Memory: memory,
MemorySwap: memorySwap,
Tags: flTags.GetAll(),
Expand All @@ -236,7 +235,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
Labels: runconfigopts.ConvertKVStringsToMap(flLabels.GetAll()),
}

response, err := cli.client.ImageBuild(context.Background(), options)
response, err := cli.client.ImageBuild(context.Background(), body, options)
if err != nil {
return err
}
Expand Down
41 changes: 9 additions & 32 deletions api/client/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package client

import (
"encoding/json"
"errors"
"fmt"

"golang.org/x/net/context"

Cli "github.com/docker/docker/cli"
"github.com/docker/docker/opts"
flag "github.com/docker/docker/pkg/mflag"
"github.com/docker/docker/reference"
"github.com/docker/engine-api/types"
"github.com/docker/engine-api/types/container"
)
Expand All @@ -33,29 +31,10 @@ func (cli *DockerCli) CmdCommit(args ...string) error {
cmd.ParseFlags(args, true)

var (
name = cmd.Arg(0)
repositoryAndTag = cmd.Arg(1)
repositoryName string
tag string
name = cmd.Arg(0)
reference = cmd.Arg(1)
)

//Check if the given image name can be resolved
if repositoryAndTag != "" {
ref, err := reference.ParseNamed(repositoryAndTag)
if err != nil {
return err
}

repositoryName = ref.Name()

switch x := ref.(type) {
case reference.Canonical:
return errors.New("cannot commit to digest reference")
case reference.NamedTagged:
tag = x.Tag()
}
}

var config *container.Config
if *flConfig != "" {
config = &container.Config{}
Expand All @@ -65,17 +44,15 @@ func (cli *DockerCli) CmdCommit(args ...string) error {
}

options := types.ContainerCommitOptions{
ContainerID: name,
RepositoryName: repositoryName,
Tag: tag,
Comment: *flComment,
Author: *flAuthor,
Changes: flChanges.GetAll(),
Pause: *flPause,
Config: config,
Reference: reference,
Comment: *flComment,
Author: *flAuthor,
Changes: flChanges.GetAll(),
Pause: *flPause,
Config: config,
}

response, err := cli.client.ContainerCommit(context.Background(), options)
response, err := cli.client.ContainerCommit(context.Background(), name, options)
if err != nil {
return err
}
Expand Down
13 changes: 2 additions & 11 deletions api/client/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

Cli "github.com/docker/docker/cli"
"github.com/docker/docker/pkg/jsonmessage"
// FIXME migrate to docker/distribution/reference
"github.com/docker/docker/reference"
"github.com/docker/docker/registry"
runconfigopts "github.com/docker/docker/runconfig/opts"
Expand All @@ -24,14 +25,6 @@ func (cli *DockerCli) pullImage(image string, out io.Writer) error {
return err
}

var tag string
switch x := reference.WithDefaultTag(ref).(type) {
case reference.Canonical:
tag = x.Digest().String()
case reference.NamedTagged:
tag = x.Tag()
}

// Resolve the Repository name from fqn to RepositoryInfo
repoInfo, err := registry.ParseRepositoryInfo(ref)
if err != nil {
Expand All @@ -45,12 +38,10 @@ func (cli *DockerCli) pullImage(image string, out io.Writer) error {
}

options := types.ImageCreateOptions{
Parent: ref.Name(),
Tag: tag,
RegistryAuth: encodedAuth,
}

responseBody, err := cli.client.ImageCreate(context.Background(), options)
responseBody, err := cli.client.ImageCreate(context.Background(), image, options)
if err != nil {
return err
}
Expand Down
8 changes: 3 additions & 5 deletions api/client/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ func (cli *DockerCli) CmdExec(args ...string) error {
detachKeys := cmd.String([]string{"-detach-keys"}, "", "Override the key sequence for detaching a container")

execConfig, err := ParseExec(cmd, args)
container := cmd.Arg(0)
// just in case the ParseExec does not exit
if execConfig.Container == "" || err != nil {
if container == "" || err != nil {
return Cli.StatusError{StatusCode: 1}
}

Expand All @@ -33,7 +34,7 @@ func (cli *DockerCli) CmdExec(args ...string) error {
// Send client escape keys
execConfig.DetachKeys = cli.configFile.DetachKeys

response, err := cli.client.ContainerExecCreate(context.Background(), *execConfig)
response, err := cli.client.ContainerExecCreate(context.Background(), container, *execConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -128,13 +129,11 @@ func ParseExec(cmd *flag.FlagSet, args []string) (*types.ExecConfig, error) {
flUser = cmd.String([]string{"u", "-user"}, "", "Username or UID (format: <name|uid>[:<group|gid>])")
flPrivileged = cmd.Bool([]string{"-privileged"}, false, "Give extended privileges to the command")
execCmd []string
container string
)
cmd.Require(flag.Min, 2)
if err := cmd.ParseFlags(args, true); err != nil {
return nil, err
}
container = cmd.Arg(0)
parsedArgs := cmd.Args()
execCmd = parsedArgs[1:]

Expand All @@ -143,7 +142,6 @@ func ParseExec(cmd *flag.FlagSet, args []string) (*types.ExecConfig, error) {
Privileged: *flPrivileged,
Tty: *flTty,
Cmd: execCmd,
Container: container,
Detach: *flDetach,
}

Expand Down
8 changes: 0 additions & 8 deletions api/client/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ func TestParseExec(t *testing.T) {
&arguments{
[]string{"container", "command"},
}: {
Container: "container",
Cmd: []string{"command"},
AttachStdout: true,
AttachStderr: true,
},
&arguments{
[]string{"container", "command1", "command2"},
}: {
Container: "container",
Cmd: []string{"command1", "command2"},
AttachStdout: true,
AttachStderr: true,
Expand All @@ -44,7 +42,6 @@ func TestParseExec(t *testing.T) {
AttachStdout: true,
AttachStderr: true,
Tty: true,
Container: "container",
Cmd: []string{"command"},
},
&arguments{
Expand All @@ -54,7 +51,6 @@ func TestParseExec(t *testing.T) {
AttachStdout: false,
AttachStderr: false,
Detach: true,
Container: "container",
Cmd: []string{"command"},
},
&arguments{
Expand All @@ -65,7 +61,6 @@ func TestParseExec(t *testing.T) {
AttachStderr: false,
Detach: true,
Tty: true,
Container: "container",
Cmd: []string{"command"},
},
}
Expand Down Expand Up @@ -103,9 +98,6 @@ func compareExecConfig(config1 *types.ExecConfig, config2 *types.ExecConfig) boo
if config1.AttachStdout != config2.AttachStdout {
return false
}
if config1.Container != config2.Container {
return false
}
if config1.Detach != config2.Detach {
return false
}
Expand Down
37 changes: 16 additions & 21 deletions api/client/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/docker/docker/pkg/jsonmessage"
flag "github.com/docker/docker/pkg/mflag"
"github.com/docker/docker/pkg/urlutil"
"github.com/docker/docker/reference"
"github.com/docker/engine-api/types"
)

Expand All @@ -31,26 +30,20 @@ func (cli *DockerCli) CmdImport(args ...string) error {
cmd.ParseFlags(args, true)

var (
in io.Reader
tag string
src = cmd.Arg(0)
srcName = src
repository = cmd.Arg(1)
changes = flChanges.GetAll()
in io.Reader
tag string
src = cmd.Arg(0)
srcName = src
ref = cmd.Arg(1)
changes = flChanges.GetAll()
)

if cmd.NArg() == 3 {
// FIXME(vdemeester) Which version has this been deprecated ? should we remove it ?
fmt.Fprintf(cli.err, "[DEPRECATED] The format 'file|URL|- [REPOSITORY [TAG]]' has been deprecated. Please use file|URL|- [REPOSITORY[:TAG]]\n")
tag = cmd.Arg(2)
}

if repository != "" {
//Check if the given image name can be resolved
if _, err := reference.ParseNamed(repository); err != nil {
return err
}
}

if src == "-" {
in = cli.in
} else if !urlutil.IsURL(src) {
Expand All @@ -63,16 +56,18 @@ func (cli *DockerCli) CmdImport(args ...string) error {
in = file
}

source := types.ImageImportSource{
Source: in,
SourceName: srcName,
}

options := types.ImageImportOptions{
Source: in,
SourceName: srcName,
RepositoryName: repository,
Message: *message,
Tag: tag,
Changes: changes,
Message: *message,
Tag: tag,
Changes: changes,
}

responseBody, err := cli.client.ImageImport(context.Background(), options)
responseBody, err := cli.client.ImageImport(context.Background(), source, ref, options)
if err != nil {
return err
}
Expand Down
15 changes: 7 additions & 8 deletions api/client/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,14 @@ func (cli *DockerCli) CmdLogs(args ...string) error {
}

options := types.ContainerLogsOptions{
ContainerID: name,
ShowStdout: true,
ShowStderr: true,
Since: *since,
Timestamps: *times,
Follow: *follow,
Tail: *tail,
ShowStdout: true,
ShowStderr: true,
Since: *since,
Timestamps: *times,
Follow: *follow,
Tail: *tail,
}
responseBody, err := cli.client.ContainerLogs(context.Background(), options)
responseBody, err := cli.client.ContainerLogs(context.Background(), name, options)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions api/client/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ func (cli *DockerCli) CmdNetworkCreate(args ...string) error {

// Construct network create request body
nc := types.NetworkCreate{
Name: cmd.Arg(0),
Driver: driver,
IPAM: network.IPAM{Driver: *flIpamDriver, Config: ipamCfg, Options: flIpamOpt.GetAll()},
Options: flOpts.GetAll(),
Expand All @@ -87,7 +86,7 @@ func (cli *DockerCli) CmdNetworkCreate(args ...string) error {
Labels: runconfigopts.ConvertKVStringsToMap(flLabels.GetAll()),
}

resp, err := cli.client.NetworkCreate(context.Background(), nc)
resp, err := cli.client.NetworkCreate(context.Background(), cmd.Arg(0), nc)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit b9c94b7

Please sign in to comment.