Skip to content

Commit 88f6827

Browse files
authored
Merge pull request #6406 from thaJeztah/rm_GetStacks_StackWrite
cli/command/stack: move GetStacks and StackWrite internal
2 parents a2c8862 + 6647e22 commit 88f6827

File tree

8 files changed

+76
-112
lines changed

8 files changed

+76
-112
lines changed

cli/command/stack/cmd.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/docker/cli/cli"
77
"github.com/docker/cli/cli/command"
88
"github.com/docker/cli/cli/command/completion"
9-
"github.com/docker/cli/cli/command/stack/swarm"
109
"github.com/docker/cli/internal/commands"
1110
"github.com/spf13/cobra"
1211
)
@@ -53,7 +52,7 @@ func newStackCommand(dockerCLI command.Cli) *cobra.Command {
5352
// completeNames offers completion for swarm stacks
5453
func completeNames(dockerCLI completion.APIClientProvider) cobra.CompletionFunc {
5554
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
56-
list, err := swarm.GetStacks(cmd.Context(), dockerCLI.Client())
55+
list, err := getStacks(cmd.Context(), dockerCLI.Client())
5756
if err != nil {
5857
return nil, cobra.ShellCompDirectiveError
5958
}

cli/command/stack/common.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,9 @@ func getStackFilterFromOpt(namespace string, opt opts.FilterOpt) filters.Args {
3939
filter.Add("label", convert.LabelNamespace+"="+namespace)
4040
return filter
4141
}
42+
43+
func getAllStacksFilter() filters.Args {
44+
filter := filters.NewArgs()
45+
filter.Add("label", convert.LabelNamespace)
46+
return filter
47+
}

cli/command/stack/formatter/formatter.go

Lines changed: 0 additions & 80 deletions
This file was deleted.

cli/command/stack/list.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ package stack
22

33
import (
44
"context"
5-
"io"
65
"sort"
76

87
"github.com/docker/cli/cli"
98
"github.com/docker/cli/cli/command"
10-
"github.com/docker/cli/cli/command/stack/formatter"
11-
"github.com/docker/cli/cli/command/stack/swarm"
9+
"github.com/docker/cli/cli/command/formatter"
1210
flagsHelper "github.com/docker/cli/cli/flags"
1311
"github.com/fvbommel/sortorder"
1412
"github.com/spf13/cobra"
@@ -40,24 +38,21 @@ func newListCommand(dockerCLI command.Cli) *cobra.Command {
4038

4139
// runList performs a stack list against the specified swarm cluster
4240
func runList(ctx context.Context, dockerCLI command.Cli, opts listOptions) error {
43-
stacks, err := swarm.GetStacks(ctx, dockerCLI.Client())
41+
stacks, err := getStacks(ctx, dockerCLI.Client())
4442
if err != nil {
4543
return err
4644
}
47-
return format(dockerCLI.Out(), opts, stacks)
48-
}
4945

50-
func format(out io.Writer, opts listOptions, stacks []formatter.Stack) error {
51-
fmt := formatter.Format(opts.format)
52-
if fmt == "" || fmt == formatter.TableFormatKey {
53-
fmt = formatter.SwarmStackTableFormat
46+
format := formatter.Format(opts.format)
47+
if format == "" || format == formatter.TableFormatKey {
48+
format = stackTableFormat
5449
}
5550
stackCtx := formatter.Context{
56-
Output: out,
57-
Format: fmt,
51+
Output: dockerCLI.Out(),
52+
Format: format,
5853
}
5954
sort.Slice(stacks, func(i, j int) bool {
6055
return sortorder.NaturalLess(stacks[i].Name, stacks[j].Name)
6156
})
62-
return formatter.StackWrite(stackCtx, stacks)
57+
return stackWrite(stackCtx, stacks)
6358
}

cli/command/stack/list_formatter.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package stack
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/docker/cli/cli/command/formatter"
7+
)
8+
9+
// stackTableFormat is the default Swarm stack format
10+
const stackTableFormat formatter.Format = "table {{.Name}}\t{{.Services}}"
11+
12+
// stackSummary contains deployed stack information.
13+
type stackSummary struct {
14+
Name string // Name is the name of the stack.
15+
Services int // Services is the number services in the stack.
16+
}
17+
18+
// stackWrite writes formatted stacks using the Context
19+
func stackWrite(fmtCtx formatter.Context, stacks []stackSummary) error {
20+
stackCtx := &stackContext{
21+
HeaderContext: formatter.HeaderContext{
22+
Header: formatter.SubHeaderContext{
23+
"Name": formatter.NameHeader,
24+
"Services": "SERVICES",
25+
},
26+
},
27+
}
28+
return fmtCtx.Write(stackCtx, func(format func(subContext formatter.SubContext) error) error {
29+
for _, stack := range stacks {
30+
if err := format(&stackContext{s: stack}); err != nil {
31+
return err
32+
}
33+
}
34+
return nil
35+
})
36+
}
37+
38+
type stackContext struct {
39+
formatter.HeaderContext
40+
s stackSummary
41+
}
42+
43+
func (s *stackContext) MarshalJSON() ([]byte, error) {
44+
return formatter.MarshalJSON(s)
45+
}
46+
47+
func (s *stackContext) Name() string {
48+
return s.s.Name
49+
}
50+
51+
func (s *stackContext) Services() string {
52+
return strconv.Itoa(s.s.Services)
53+
}

cli/command/stack/formatter/formatter_test.go renamed to cli/command/stack/list_formatter_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package formatter
1+
package stack
22

33
import (
44
"bytes"
@@ -26,7 +26,7 @@ func TestStackContextWrite(t *testing.T) {
2626
},
2727
{
2828
name: "table format",
29-
format: SwarmStackTableFormat,
29+
format: stackTableFormat,
3030
expected: `NAME SERVICES
3131
baz 2
3232
bar 1
@@ -56,7 +56,7 @@ bar
5656
Format: tc.format,
5757
Output: &out,
5858
}
59-
if err := StackWrite(fmtCtx, []Stack{
59+
if err := stackWrite(fmtCtx, []stackSummary{
6060
{Name: "baz", Services: 2},
6161
{Name: "bar", Services: 1},
6262
}); err != nil {
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
package swarm
1+
package stack
22

33
import (
44
"context"
55

6-
"github.com/docker/cli/cli/command/stack/formatter"
76
"github.com/docker/cli/cli/compose/convert"
87
"github.com/moby/moby/client"
98
"github.com/pkg/errors"
109
)
1110

12-
// GetStacks lists the swarm stacks with the number of services they contain.
13-
//
14-
// Deprecated: this function was for internal use and will be removed in the next release.
15-
func GetStacks(ctx context.Context, apiClient client.ServiceAPIClient) ([]formatter.Stack, error) {
11+
// getStacks lists the swarm stacks with the number of services they contain.
12+
func getStacks(ctx context.Context, apiClient client.ServiceAPIClient) ([]stackSummary, error) {
1613
services, err := apiClient.ServiceList(ctx, client.ServiceListOptions{
1714
Filters: getAllStacksFilter(),
1815
})
@@ -21,7 +18,7 @@ func GetStacks(ctx context.Context, apiClient client.ServiceAPIClient) ([]format
2118
}
2219

2320
idx := make(map[string]int, len(services))
24-
out := make([]formatter.Stack, 0, len(services))
21+
out := make([]stackSummary, 0, len(services))
2522

2623
for _, svc := range services {
2724
name, ok := svc.Spec.Labels[convert.LabelNamespace]
@@ -33,7 +30,7 @@ func GetStacks(ctx context.Context, apiClient client.ServiceAPIClient) ([]format
3330
continue
3431
}
3532
idx[name] = len(out)
36-
out = append(out, formatter.Stack{Name: name, Services: 1})
33+
out = append(out, stackSummary{Name: name, Services: 1})
3734
}
3835
return out, nil
3936
}

cli/command/stack/swarm/common.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ func getStackFilter(namespace string) filters.Args {
1616
return filter
1717
}
1818

19-
func getAllStacksFilter() filters.Args {
20-
filter := filters.NewArgs()
21-
filter.Add("label", convert.LabelNamespace)
22-
return filter
23-
}
24-
2519
func getStackServices(ctx context.Context, apiclient client.APIClient, namespace string) ([]swarm.Service, error) {
2620
return apiclient.ServiceList(ctx, client.ServiceListOptions{Filters: getStackFilter(namespace)})
2721
}

0 commit comments

Comments
 (0)