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

Fix stack output stream #298

Merged
merged 2 commits into from
Jul 10, 2017
Merged
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
7 changes: 1 addition & 6 deletions cli/command/node/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/cli/command/idresolver"
"github.com/docker/cli/cli/command/task"
"github.com/docker/cli/opts"
Expand Down Expand Up @@ -88,11 +87,7 @@ func runPs(dockerCli command.Cli, options psOptions) error {

format := options.format
if len(format) == 0 {
if dockerCli.ConfigFile() != nil && len(dockerCli.ConfigFile().TasksFormat) > 0 && !options.quiet {
format = dockerCli.ConfigFile().TasksFormat
} else {
format = formatter.TableFormatKey
}
format = task.DefaultFormat(dockerCli.ConfigFile(), options.quiet)
}

if len(errs) == 0 || len(tasks) != 0 {
Expand Down
7 changes: 1 addition & 6 deletions cli/command/service/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/cli/command/idresolver"
"github.com/docker/cli/cli/command/node"
"github.com/docker/cli/cli/command/task"
Expand Down Expand Up @@ -65,11 +64,7 @@ func runPS(dockerCli command.Cli, options psOptions) error {

format := options.format
if len(format) == 0 {
if len(dockerCli.ConfigFile().TasksFormat) > 0 && !options.quiet {
format = dockerCli.ConfigFile().TasksFormat
} else {
format = formatter.TableFormatKey
}
format = task.DefaultFormat(dockerCli.ConfigFile(), options.quiet)
}
if err := task.Print(ctx, dockerCli, tasks, idresolver.New(client, options.noResolve), !options.noTrunc, options.quiet, format); err != nil {
return err
Expand Down
9 changes: 2 additions & 7 deletions cli/command/stack/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/cli/command/idresolver"
"github.com/docker/cli/cli/command/task"
"github.com/docker/cli/opts"
Expand Down Expand Up @@ -58,17 +57,13 @@ func runPS(dockerCli command.Cli, options psOptions) error {
}

if len(tasks) == 0 {
fmt.Fprintf(dockerCli.Out(), "Nothing found in stack: %s\n", namespace)
fmt.Fprintf(dockerCli.Err(), "Nothing found in stack: %s\n", namespace)
return nil
}

format := options.format
if len(format) == 0 {
if len(dockerCli.ConfigFile().TasksFormat) > 0 && !options.quiet {
format = dockerCli.ConfigFile().TasksFormat
} else {
format = formatter.TableFormatKey
}
format = task.DefaultFormat(dockerCli.ConfigFile(), options.quiet)
}

return task.Print(ctx, dockerCli, tasks, idresolver.New(client, options.noResolve), !options.noTrunc, options.quiet, format)
Expand Down
13 changes: 9 additions & 4 deletions cli/command/stack/ps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,20 @@ func TestStackPsErrors(t *testing.T) {
}

func TestStackPsEmptyStack(t *testing.T) {
buf := new(bytes.Buffer)
cmd := newPsCommand(test.NewFakeCli(&fakeClient{
out := new(bytes.Buffer)
stderr := new(bytes.Buffer)
fakeCli := test.NewFakeCli(&fakeClient{
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{}, nil
},
}, buf))
}, out)
fakeCli.SetErr(stderr)
cmd := newPsCommand(fakeCli)
cmd.SetArgs([]string{"foo"})

assert.NoError(t, cmd.Execute())
testutil.EqualNormalizedString(t, testutil.RemoveSpace, buf.String(), "Nothing found in stack: foo")
assert.Equal(t, "", out.String())
assert.Equal(t, "Nothing found in stack: foo\n", stderr.String())
}

func TestStackPsWithQuietOption(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cli/command/stack/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func runRemove(dockerCli command.Cli, opts removeOptions) error {
}

if len(services)+len(networks)+len(secrets)+len(configs) == 0 {
fmt.Fprintf(dockerCli.Out(), "Nothing found in stack: %s\n", namespace)
fmt.Fprintf(dockerCli.Err(), "Nothing found in stack: %s\n", namespace)
continue
}

Expand Down
22 changes: 13 additions & 9 deletions cli/command/stack/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ func TestRemoveStack(t *testing.T) {
assert.Equal(t, allConfigIDs, cli.removedConfigs)
}

func TestSkipEmptyStack(t *testing.T) {
buf := new(bytes.Buffer)
func TestRemoveStackSkipEmpty(t *testing.T) {
out := new(bytes.Buffer)
stderr := new(bytes.Buffer)
allServices := []string{objectName("bar", "service1"), objectName("bar", "service2")}
allServiceIDs := buildObjectIDs(allServices)

Expand All @@ -70,21 +71,24 @@ func TestSkipEmptyStack(t *testing.T) {
allConfigs := []string{objectName("bar", "config1")}
allConfigIDs := buildObjectIDs(allConfigs)

cli := &fakeClient{
fakeClient := &fakeClient{
services: allServices,
networks: allNetworks,
secrets: allSecrets,
configs: allConfigs,
}
cmd := newRemoveCommand(test.NewFakeCli(cli, buf))
fakeCli := test.NewFakeCli(fakeClient, out)
fakeCli.SetErr(stderr)
cmd := newRemoveCommand(fakeCli)
cmd.SetArgs([]string{"foo", "bar"})

assert.NoError(t, cmd.Execute())
assert.Contains(t, buf.String(), "Nothing found in stack: foo")
assert.Equal(t, allServiceIDs, cli.removedServices)
assert.Equal(t, allNetworkIDs, cli.removedNetworks)
assert.Equal(t, allSecretIDs, cli.removedSecrets)
assert.Equal(t, allConfigIDs, cli.removedConfigs)
assert.Equal(t, "", out.String())
assert.Contains(t, stderr.String(), "Nothing found in stack: foo\n")
assert.Equal(t, allServiceIDs, fakeClient.removedServices)
assert.Equal(t, allNetworkIDs, fakeClient.removedNetworks)
assert.Equal(t, allSecretIDs, fakeClient.removedSecrets)
assert.Equal(t, allConfigIDs, fakeClient.removedConfigs)
}

func TestRemoveContinueAfterError(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions cli/command/stack/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ func runServices(dockerCli command.Cli, options servicesOptions) error {
return err
}

out := dockerCli.Out()

// if no services in this stack, print message and exit 0
if len(services) == 0 {
fmt.Fprintf(out, "Nothing found in stack: %s\n", options.namespace)
fmt.Fprintf(dockerCli.Err(), "Nothing found in stack: %s\n", options.namespace)
return nil
}

Expand Down
20 changes: 11 additions & 9 deletions cli/command/stack/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,19 @@ func TestStackServicesErrors(t *testing.T) {
}

func TestStackServicesEmptyServiceList(t *testing.T) {
buf := new(bytes.Buffer)
cmd := newServicesCommand(
test.NewFakeCli(&fakeClient{
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
return []swarm.Service{}, nil
},
}, buf),
)
out := new(bytes.Buffer)
stderr := new(bytes.Buffer)
fakeCli := test.NewFakeCli(&fakeClient{
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
return []swarm.Service{}, nil
},
}, out)
fakeCli.SetErr(stderr)
cmd := newServicesCommand(fakeCli)
cmd.SetArgs([]string{"foo"})
assert.NoError(t, cmd.Execute())
testutil.EqualNormalizedString(t, testutil.RemoveSpace, buf.String(), "Nothing found in stack: foo")
assert.Equal(t, "", out.String())
assert.Equal(t, "Nothing found in stack: foo\n", stderr.String())
}

func TestStackServicesWithQuietOption(t *testing.T) {
Expand Down
13 changes: 11 additions & 2 deletions cli/command/task/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"fmt"
"sort"

"golang.org/x/net/context"

"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/cli/command/idresolver"
"github.com/docker/cli/cli/config/configfile"
"github.com/docker/docker/api/types/swarm"
"golang.org/x/net/context"
)

type tasksBySlot []swarm.Task
Expand Down Expand Up @@ -82,3 +82,12 @@ func Print(ctx context.Context, dockerCli command.Cli, tasks []swarm.Task, resol

return formatter.TaskWrite(tasksCtx, tasks, names, nodes)
}

// DefaultFormat returns the default format from the config file, or table
// format if nothing is set in the config.
func DefaultFormat(configFile *configfile.ConfigFile, quiet bool) string {
if len(configFile.TasksFormat) > 0 && !quiet {
return configFile.TasksFormat
}
return formatter.TableFormatKey
}