From 0030bfea9fc20475695b256e18b97c9c0d2950e8 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 5 Jul 2017 13:40:08 -0400 Subject: [PATCH] Remove duplication in task formatting. Signed-off-by: Daniel Nephin --- cli/command/node/ps.go | 7 +------ cli/command/service/ps.go | 7 +------ cli/command/stack/ps.go | 7 +------ cli/command/task/print.go | 13 +++++++++++-- 4 files changed, 14 insertions(+), 20 deletions(-) diff --git a/cli/command/node/ps.go b/cli/command/node/ps.go index 6a586a3bcc..5212e596f3 100644 --- a/cli/command/node/ps.go +++ b/cli/command/node/ps.go @@ -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" @@ -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 { diff --git a/cli/command/service/ps.go b/cli/command/service/ps.go index 741f6b589f..07dbba7230 100644 --- a/cli/command/service/ps.go +++ b/cli/command/service/ps.go @@ -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" @@ -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 diff --git a/cli/command/stack/ps.go b/cli/command/stack/ps.go index 985d6ee8d8..25bd1eee10 100644 --- a/cli/command/stack/ps.go +++ b/cli/command/stack/ps.go @@ -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" @@ -64,11 +63,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) } return task.Print(ctx, dockerCli, tasks, idresolver.New(client, options.noResolve), !options.noTrunc, options.quiet, format) diff --git a/cli/command/task/print.go b/cli/command/task/print.go index 2376ecf803..6526c28beb 100644 --- a/cli/command/task/print.go +++ b/cli/command/task/print.go @@ -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 @@ -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 +}