formatter package heavy refactoring

- make it possible to extract the formatter implementation from the
  "common" code, that way, the formatter package stays small
- extract some formatter into their own packages

This is essentially moving the "formatter" implementation of each type
in their respective packages. The *main* reason to do that, is to be
able to depend on `cli/command/formatter` without depending of the
implementation detail of the formatter. As of now, depending on
`cli/command/formatter` means we depend on `docker/docker/api/types`,
`docker/licensing`, … — that should not be the case. `formatter`
should hold the common code (or helpers) to easily create formatter,
not all formatter implementations.

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
Vincent Demeester
2018-10-23 17:05:44 +02:00
parent ea836abed5
commit 69fdd2a4ad
75 changed files with 750 additions and 711 deletions

View File

@ -6,7 +6,7 @@ import (
"strings"
"time"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/cli/command/service"
"github.com/docker/cli/kubernetes/labels"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/swarm"
@ -154,16 +154,16 @@ const (
publishedOnRandomPortSuffix = "-random-ports"
)
func convertToServices(replicas *appsv1beta2.ReplicaSetList, daemons *appsv1beta2.DaemonSetList, services *apiv1.ServiceList) ([]swarm.Service, map[string]formatter.ServiceListInfo, error) {
func convertToServices(replicas *appsv1beta2.ReplicaSetList, daemons *appsv1beta2.DaemonSetList, services *apiv1.ServiceList) ([]swarm.Service, map[string]service.ListInfo, error) {
result := make([]swarm.Service, len(replicas.Items))
infos := make(map[string]formatter.ServiceListInfo, len(replicas.Items)+len(daemons.Items))
infos := make(map[string]service.ListInfo, len(replicas.Items)+len(daemons.Items))
for i, r := range replicas.Items {
s, err := convertToService(r.Labels[labels.ForServiceName], services, r.Spec.Template.Spec.Containers)
if err != nil {
return nil, nil, err
}
result[i] = *s
infos[s.ID] = formatter.ServiceListInfo{
infos[s.ID] = service.ListInfo{
Mode: "replicated",
Replicas: fmt.Sprintf("%d/%d", r.Status.AvailableReplicas, r.Status.Replicas),
}
@ -174,7 +174,7 @@ func convertToServices(replicas *appsv1beta2.ReplicaSetList, daemons *appsv1beta
return nil, nil, err
}
result = append(result, *s)
infos[s.ID] = formatter.ServiceListInfo{
infos[s.ID] = service.ListInfo{
Mode: "global",
Replicas: fmt.Sprintf("%d/%d", d.Status.NumberReady, d.Status.DesiredNumberScheduled),
}

View File

@ -3,7 +3,7 @@ package kubernetes
import (
"testing"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/cli/command/service"
"github.com/docker/cli/kubernetes/labels"
"github.com/docker/docker/api/types/swarm"
"gotest.tools/assert"
@ -28,7 +28,7 @@ func TestKubernetesServiceToSwarmServiceConversion(t *testing.T) {
replicas *appsv1beta2.ReplicaSetList
services *apiv1.ServiceList
expectedServices []swarm.Service
expectedListInfo map[string]formatter.ServiceListInfo
expectedListInfo map[string]service.ListInfo
}{
// Match replicas with headless stack services
{
@ -49,7 +49,7 @@ func TestKubernetesServiceToSwarmServiceConversion(t *testing.T) {
makeSwarmService("stack_service1", "uid1", nil),
makeSwarmService("stack_service2", "uid2", nil),
},
map[string]formatter.ServiceListInfo{
map[string]service.ListInfo{
"uid1": {Mode: "replicated", Replicas: "2/5"},
"uid2": {Mode: "replicated", Replicas: "3/3"},
},
@ -83,7 +83,7 @@ func TestKubernetesServiceToSwarmServiceConversion(t *testing.T) {
},
}),
},
map[string]formatter.ServiceListInfo{
map[string]service.ListInfo{
"uid1": {Mode: "replicated", Replicas: "1/1"},
},
},
@ -117,7 +117,7 @@ func TestKubernetesServiceToSwarmServiceConversion(t *testing.T) {
},
}),
},
map[string]formatter.ServiceListInfo{
map[string]service.ListInfo{
"uid1": {Mode: "replicated", Replicas: "1/1"},
},
},

View File

@ -8,7 +8,7 @@ import (
"net/url"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/cli/command/stack/formatter"
"github.com/docker/cli/cli/command/stack/options"
"github.com/docker/cli/cli/config/configfile"
"github.com/pkg/errors"

View File

@ -5,7 +5,7 @@ import (
"sort"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/cli/command/stack/formatter"
"github.com/docker/cli/cli/command/stack/options"
"github.com/docker/cli/cli/command/task"
"github.com/docker/docker/api/types/swarm"
@ -83,11 +83,11 @@ func printTasks(dockerCli command.Cli, options options.PS, namespace string, cli
tasksCtx := formatter.Context{
Output: dockerCli.Out(),
Format: formatter.NewTaskFormat(format, options.Quiet),
Format: task.NewTaskFormat(format, options.Quiet),
Trunc: !options.NoTrunc,
}
return formatter.TaskWrite(tasksCtx, tasks, names, nodes)
return task.FormatWrite(tasksCtx, tasks, names, nodes)
}
func resolveNode(name string, nodes *apiv1.NodeList, noResolve bool) (string, error) {

View File

@ -4,7 +4,8 @@ import (
"fmt"
"strings"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/cli/command/service"
"github.com/docker/cli/cli/command/stack/formatter"
"github.com/docker/cli/cli/command/stack/options"
"github.com/docker/cli/kubernetes/labels"
"github.com/docker/docker/api/types/filters"
@ -115,7 +116,7 @@ func RunServices(dockerCli *KubeCli, opts options.Services) error {
services = filterServicesByName(services, filters.Get("name"), stackName)
if opts.Quiet {
info = map[string]formatter.ServiceListInfo{}
info = map[string]service.ListInfo{}
}
format := opts.Format
@ -129,9 +130,9 @@ func RunServices(dockerCli *KubeCli, opts options.Services) error {
servicesCtx := formatter.Context{
Output: dockerCli.Out(),
Format: formatter.NewServiceListFormat(format, opts.Quiet),
Format: service.NewListFormat(format, opts.Quiet),
}
return formatter.ServiceListWrite(servicesCtx, services, info)
return service.ListFormatWrite(servicesCtx, services, info)
}
func filterServicesByName(services []swarm.Service, names []string, stackName string) []swarm.Service {