Merge pull request #298 from dnephin/fix-stack-out-stream

Fix stack output stream
This commit is contained in:
Vincent Demeester 2017-07-10 09:23:23 +02:00 committed by GitHub
commit af6c0896f5
9 changed files with 50 additions and 47 deletions

View File

@ -5,7 +5,6 @@ import (
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
"github.com/docker/cli/cli/command" "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/idresolver"
"github.com/docker/cli/cli/command/task" "github.com/docker/cli/cli/command/task"
"github.com/docker/cli/opts" "github.com/docker/cli/opts"
@ -88,11 +87,7 @@ func runPs(dockerCli command.Cli, options psOptions) error {
format := options.format format := options.format
if len(format) == 0 { if len(format) == 0 {
if dockerCli.ConfigFile() != nil && len(dockerCli.ConfigFile().TasksFormat) > 0 && !options.quiet { format = task.DefaultFormat(dockerCli.ConfigFile(), options.quiet)
format = dockerCli.ConfigFile().TasksFormat
} else {
format = formatter.TableFormatKey
}
} }
if len(errs) == 0 || len(tasks) != 0 { if len(errs) == 0 || len(tasks) != 0 {

View File

@ -5,7 +5,6 @@ import (
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
"github.com/docker/cli/cli/command" "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/idresolver"
"github.com/docker/cli/cli/command/node" "github.com/docker/cli/cli/command/node"
"github.com/docker/cli/cli/command/task" "github.com/docker/cli/cli/command/task"
@ -65,11 +64,7 @@ func runPS(dockerCli command.Cli, options psOptions) error {
format := options.format format := options.format
if len(format) == 0 { if len(format) == 0 {
if len(dockerCli.ConfigFile().TasksFormat) > 0 && !options.quiet { format = task.DefaultFormat(dockerCli.ConfigFile(), options.quiet)
format = dockerCli.ConfigFile().TasksFormat
} else {
format = formatter.TableFormatKey
}
} }
if err := task.Print(ctx, dockerCli, tasks, idresolver.New(client, options.noResolve), !options.noTrunc, options.quiet, format); err != nil { if err := task.Print(ctx, dockerCli, tasks, idresolver.New(client, options.noResolve), !options.noTrunc, options.quiet, format); err != nil {
return err return err

View File

@ -5,7 +5,6 @@ import (
"github.com/docker/cli/cli" "github.com/docker/cli/cli"
"github.com/docker/cli/cli/command" "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/idresolver"
"github.com/docker/cli/cli/command/task" "github.com/docker/cli/cli/command/task"
"github.com/docker/cli/opts" "github.com/docker/cli/opts"
@ -58,17 +57,13 @@ func runPS(dockerCli command.Cli, options psOptions) error {
} }
if len(tasks) == 0 { 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 return nil
} }
format := options.format format := options.format
if len(format) == 0 { if len(format) == 0 {
if len(dockerCli.ConfigFile().TasksFormat) > 0 && !options.quiet { format = task.DefaultFormat(dockerCli.ConfigFile(), options.quiet)
format = dockerCli.ConfigFile().TasksFormat
} else {
format = formatter.TableFormatKey
}
} }
return task.Print(ctx, dockerCli, tasks, idresolver.New(client, options.noResolve), !options.noTrunc, options.quiet, format) return task.Print(ctx, dockerCli, tasks, idresolver.New(client, options.noResolve), !options.noTrunc, options.quiet, format)

View File

@ -53,15 +53,20 @@ func TestStackPsErrors(t *testing.T) {
} }
func TestStackPsEmptyStack(t *testing.T) { func TestStackPsEmptyStack(t *testing.T) {
buf := new(bytes.Buffer) out := new(bytes.Buffer)
cmd := newPsCommand(test.NewFakeCli(&fakeClient{ stderr := new(bytes.Buffer)
fakeCli := test.NewFakeCli(&fakeClient{
taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) { taskListFunc: func(options types.TaskListOptions) ([]swarm.Task, error) {
return []swarm.Task{}, nil return []swarm.Task{}, nil
}, },
}, buf)) }, out)
fakeCli.SetErr(stderr)
cmd := newPsCommand(fakeCli)
cmd.SetArgs([]string{"foo"}) cmd.SetArgs([]string{"foo"})
assert.NoError(t, cmd.Execute()) 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) { func TestStackPsWithQuietOption(t *testing.T) {

View File

@ -72,7 +72,7 @@ func runRemove(dockerCli command.Cli, opts removeOptions) error {
} }
if len(services)+len(networks)+len(secrets)+len(configs) == 0 { 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 continue
} }

View File

@ -56,8 +56,9 @@ func TestRemoveStack(t *testing.T) {
assert.Equal(t, allConfigIDs, cli.removedConfigs) assert.Equal(t, allConfigIDs, cli.removedConfigs)
} }
func TestSkipEmptyStack(t *testing.T) { func TestRemoveStackSkipEmpty(t *testing.T) {
buf := new(bytes.Buffer) out := new(bytes.Buffer)
stderr := new(bytes.Buffer)
allServices := []string{objectName("bar", "service1"), objectName("bar", "service2")} allServices := []string{objectName("bar", "service1"), objectName("bar", "service2")}
allServiceIDs := buildObjectIDs(allServices) allServiceIDs := buildObjectIDs(allServices)
@ -70,21 +71,24 @@ func TestSkipEmptyStack(t *testing.T) {
allConfigs := []string{objectName("bar", "config1")} allConfigs := []string{objectName("bar", "config1")}
allConfigIDs := buildObjectIDs(allConfigs) allConfigIDs := buildObjectIDs(allConfigs)
cli := &fakeClient{ fakeClient := &fakeClient{
services: allServices, services: allServices,
networks: allNetworks, networks: allNetworks,
secrets: allSecrets, secrets: allSecrets,
configs: allConfigs, configs: allConfigs,
} }
cmd := newRemoveCommand(test.NewFakeCli(cli, buf)) fakeCli := test.NewFakeCli(fakeClient, out)
fakeCli.SetErr(stderr)
cmd := newRemoveCommand(fakeCli)
cmd.SetArgs([]string{"foo", "bar"}) cmd.SetArgs([]string{"foo", "bar"})
assert.NoError(t, cmd.Execute()) assert.NoError(t, cmd.Execute())
assert.Contains(t, buf.String(), "Nothing found in stack: foo") assert.Equal(t, "", out.String())
assert.Equal(t, allServiceIDs, cli.removedServices) assert.Contains(t, stderr.String(), "Nothing found in stack: foo\n")
assert.Equal(t, allNetworkIDs, cli.removedNetworks) assert.Equal(t, allServiceIDs, fakeClient.removedServices)
assert.Equal(t, allSecretIDs, cli.removedSecrets) assert.Equal(t, allNetworkIDs, fakeClient.removedNetworks)
assert.Equal(t, allConfigIDs, cli.removedConfigs) assert.Equal(t, allSecretIDs, fakeClient.removedSecrets)
assert.Equal(t, allConfigIDs, fakeClient.removedConfigs)
} }
func TestRemoveContinueAfterError(t *testing.T) { func TestRemoveContinueAfterError(t *testing.T) {

View File

@ -51,11 +51,9 @@ func runServices(dockerCli command.Cli, options servicesOptions) error {
return err return err
} }
out := dockerCli.Out()
// if no services in this stack, print message and exit 0 // if no services in this stack, print message and exit 0
if len(services) == 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 return nil
} }

View File

@ -83,17 +83,19 @@ func TestStackServicesErrors(t *testing.T) {
} }
func TestStackServicesEmptyServiceList(t *testing.T) { func TestStackServicesEmptyServiceList(t *testing.T) {
buf := new(bytes.Buffer) out := new(bytes.Buffer)
cmd := newServicesCommand( stderr := new(bytes.Buffer)
test.NewFakeCli(&fakeClient{ fakeCli := test.NewFakeCli(&fakeClient{
serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) {
return []swarm.Service{}, nil return []swarm.Service{}, nil
}, },
}, buf), }, out)
) fakeCli.SetErr(stderr)
cmd := newServicesCommand(fakeCli)
cmd.SetArgs([]string{"foo"}) cmd.SetArgs([]string{"foo"})
assert.NoError(t, cmd.Execute()) 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) { func TestStackServicesWithQuietOption(t *testing.T) {

View File

@ -4,12 +4,12 @@ import (
"fmt" "fmt"
"sort" "sort"
"golang.org/x/net/context"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/formatter" "github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/cli/command/idresolver" "github.com/docker/cli/cli/command/idresolver"
"github.com/docker/cli/cli/config/configfile"
"github.com/docker/docker/api/types/swarm" "github.com/docker/docker/api/types/swarm"
"golang.org/x/net/context"
) )
type tasksBySlot []swarm.Task type tasksBySlot []swarm.Task
@ -82,3 +82,12 @@ func Print(ctx context.Context, dockerCli command.Cli, tasks []swarm.Task, resol
return formatter.TaskWrite(tasksCtx, tasks, names, nodes) 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
}