Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce cyclomatic comeplxity threshold #61

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions cli/command/container/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,7 @@ func runAttach(dockerCli command.Cli, opts *attachOptions) error {
}

if c.Config.Tty && dockerCli.Out().IsTerminal() {
height, width := dockerCli.Out().GetTtySize()
// To handle the case where a user repeatedly attaches/detaches without resizing their
// terminal, the only way to get the shell prompt to display for attaches 2+ is to artificially
// resize it, then go back to normal. Without this, every attach after the first will
// require the user to manually resize or hit enter.
resizeTtyTo(ctx, client, opts.container, height+1, width+1, false)

// After the above resizing occurs, the call to MonitorTtySize below will handle resetting back
// to the actual size.
if err := MonitorTtySize(ctx, dockerCli, opts.container, false); err != nil {
logrus.Debugf("Error monitoring TTY size: %s", err)
}
resizeTTY(ctx, dockerCli, opts.container)
}

streamer := hijackedIOStreamer{
Expand All @@ -151,14 +140,36 @@ func runAttach(dockerCli command.Cli, opts *attachOptions) error {
if errAttach != nil {
return errAttach
}
return getExitStatus(ctx, dockerCli.Client(), opts.container)
}

func resizeTTY(ctx context.Context, dockerCli command.Cli, containerID string) {
height, width := dockerCli.Out().GetTtySize()
// To handle the case where a user repeatedly attaches/detaches without resizing their
// terminal, the only way to get the shell prompt to display for attaches 2+ is to artificially
// resize it, then go back to normal. Without this, every attach after the first will
// require the user to manually resize or hit enter.
resizeTtyTo(ctx, dockerCli.Client(), containerID, height+1, width+1, false)

_, status, err := getExitCode(ctx, dockerCli, opts.container)
// After the above resizing occurs, the call to MonitorTtySize below will handle resetting back
// to the actual size.
if err := MonitorTtySize(ctx, dockerCli, containerID, false); err != nil {
logrus.Debugf("Error monitoring TTY size: %s", err)
}
}

func getExitStatus(ctx context.Context, apiclient client.ContainerAPIClient, containerID string) error {
container, err := apiclient.ContainerInspect(ctx, containerID)
if err != nil {
return err
// If we can't connect, then the daemon probably died.
if !client.IsErrConnectionFailed(err) {
return err
}
return cli.StatusError{StatusCode: -1}
}
status := container.State.ExitCode
if status != 0 {
return cli.StatusError{StatusCode: status}
}

return nil
}
136 changes: 136 additions & 0 deletions cli/command/container/client_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package container

import (
"io"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"golang.org/x/net/context"
)

type fakeClient struct {
client.Client
containerInspectFunc func(string) (types.ContainerJSON, error)
execInspectFunc func(ctx context.Context, execID string) (types.ContainerExecInspect, error)
}

func (cli *fakeClient) ContainerInspect(_ context.Context, containerID string) (types.ContainerJSON, error) {
Expand All @@ -17,3 +24,132 @@ func (cli *fakeClient) ContainerInspect(_ context.Context, containerID string) (
}
return types.ContainerJSON{}, nil
}

func (cli *fakeClient) ContainerAttach(ctx context.Context, container string, options types.ContainerAttachOptions) (types.HijackedResponse, error) {
return types.HijackedResponse{}, nil
}

func (cli *fakeClient) ContainerCommit(ctx context.Context, container string, options types.ContainerCommitOptions) (types.IDResponse, error) {
return types.IDResponse{}, nil
}

func (cli *fakeClient) ContainerCreate(
ctx context.Context,
config *container.Config,
hostConfig *container.HostConfig,
networkingConfig *network.NetworkingConfig,
containerName string,
) (container.ContainerCreateCreatedBody, error) {
return container.ContainerCreateCreatedBody{}, nil
}

func (cli *fakeClient) ContainerDiff(ctx context.Context, container string) ([]container.ContainerChangeResponseItem, error) {
return nil, nil
}

func (cli *fakeClient) ContainerExecAttach(ctx context.Context, execID string, config types.ExecConfig) (types.HijackedResponse, error) {
return types.HijackedResponse{}, nil
}

func (cli *fakeClient) ContainerExecCreate(ctx context.Context, container string, config types.ExecConfig) (types.IDResponse, error) {
return types.IDResponse{}, nil
}

func (cli *fakeClient) ContainerExecInspect(ctx context.Context, execID string) (types.ContainerExecInspect, error) {
if cli.execInspectFunc != nil {
return cli.execInspectFunc(ctx, execID)
}
return types.ContainerExecInspect{}, nil
}

func (cli *fakeClient) ContainerExecResize(ctx context.Context, execID string, options types.ResizeOptions) error {
return nil
}

func (cli *fakeClient) ContainerExecStart(ctx context.Context, execID string, config types.ExecStartCheck) error {
return nil
}

func (cli *fakeClient) ContainerExport(ctx context.Context, container string) (io.ReadCloser, error) {
return nil, nil
}

func (cli *fakeClient) ContainerInspectWithRaw(ctx context.Context, container string, getSize bool) (types.ContainerJSON, []byte, error) {
return types.ContainerJSON{}, nil, nil
}

func (cli *fakeClient) ContainerKill(ctx context.Context, container, signal string) error {
return nil
}

func (cli *fakeClient) ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error) {
return nil, nil
}

func (cli *fakeClient) ContainerLogs(ctx context.Context, container string, options types.ContainerLogsOptions) (io.ReadCloser, error) {
return nil, nil
}

func (cli *fakeClient) ContainerPause(ctx context.Context, container string) error {
return nil
}

func (cli *fakeClient) ContainerRemove(ctx context.Context, container string, options types.ContainerRemoveOptions) error {
return nil
}

func (cli *fakeClient) ContainerRename(ctx context.Context, container, newContainerName string) error {
return nil
}

func (cli *fakeClient) ContainerResize(ctx context.Context, container string, options types.ResizeOptions) error {
return nil
}

func (cli *fakeClient) ContainerRestart(ctx context.Context, container string, timeout *time.Duration) error {
return nil
}

func (cli *fakeClient) ContainerStatPath(ctx context.Context, container, path string) (types.ContainerPathStat, error) {
return types.ContainerPathStat{}, nil
}

func (cli *fakeClient) ContainerStats(ctx context.Context, container string, stream bool) (types.ContainerStats, error) {
return types.ContainerStats{}, nil
}

func (cli *fakeClient) ContainerStart(ctx context.Context, container string, options types.ContainerStartOptions) error {
return nil
}

func (cli *fakeClient) ContainerStop(ctx context.Context, container string, timeout *time.Duration) error {
return nil
}

func (cli *fakeClient) ContainerTop(ctx context.Context, containerID string, arguments []string) (container.ContainerTopOKBody, error) {
return container.ContainerTopOKBody{}, nil
}

func (cli *fakeClient) ContainerUnpause(ctx context.Context, container string) error {
return nil
}

func (cli *fakeClient) ContainerUpdate(ctx context.Context, containerID string, updateConfig container.UpdateConfig) (container.ContainerUpdateOKBody, error) {
return container.ContainerUpdateOKBody{}, nil
}

func (cli *fakeClient) ContainerWait(ctx context.Context, container string, condition container.WaitCondition) (<-chan container.ContainerWaitOKBody, <-chan error) {
return nil, nil
}

func (cli *fakeClient) CopyFromContainer(ctx context.Context, container, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) {
return nil, types.ContainerPathStat{}, nil
}

func (cli *fakeClient) CopyToContainer(ctx context.Context, container, path string, content io.Reader, options types.CopyToContainerOptions) error {
return nil
}

func (cli *fakeClient) ContainersPrune(ctx context.Context, pruneFilters filters.Args) (types.ContainersPruneReport, error) {
return types.ContainersPruneReport{}, nil
}
36 changes: 18 additions & 18 deletions cli/command/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ type cidFile struct {
}

func (cid *cidFile) Close() error {
if cid.file == nil {
return nil
}
cid.file.Close()

if cid.written {
Expand All @@ -126,6 +129,9 @@ func (cid *cidFile) Close() error {
}

func (cid *cidFile) Write(id string) error {
if cid.file == nil {
return nil
}
if _, err := cid.file.Write([]byte(id)); err != nil {
return errors.Errorf("Failed to write the container ID to the file: %s", err)
}
Expand All @@ -134,6 +140,9 @@ func (cid *cidFile) Write(id string) error {
}

func newCIDFile(path string) (*cidFile, error) {
if path == "" {
return &cidFile{}, nil
}
if _, err := os.Stat(path); err == nil {
return nil, errors.Errorf("Container ID file found, make sure the other container isn't running or delete %s", path)
}
Expand All @@ -153,19 +162,15 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerConfig
stderr := dockerCli.Err()

var (
containerIDFile *cidFile
trustedRef reference.Canonical
namedRef reference.Named
trustedRef reference.Canonical
namedRef reference.Named
)

cidfile := hostConfig.ContainerIDFile
if cidfile != "" {
var err error
if containerIDFile, err = newCIDFile(cidfile); err != nil {
return nil, err
}
defer containerIDFile.Close()
containerIDFile, err := newCIDFile(hostConfig.ContainerIDFile)
if err != nil {
return nil, err
}
defer containerIDFile.Close()

ref, err := reference.ParseAnyReference(config.Image)
if err != nil {
Expand Down Expand Up @@ -207,18 +212,13 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerConfig
if retryErr != nil {
return nil, retryErr
}
} else {
return nil, err
}
return nil, err
}

for _, warning := range response.Warnings {
fmt.Fprintf(stderr, "WARNING: %s\n", warning)
}
if containerIDFile != nil {
if err = containerIDFile.Write(response.ID); err != nil {
return nil, err
}
}
return &response, nil
err = containerIDFile.Write(response.ID)
return &response, err
}
Loading