Skip to content

Commit 81ea282

Browse files
authored
Merge pull request #6398 from thaJeztah/rm_deprecated_stack
cli/command/stack: remove some deprecated functions
2 parents 0155c26 + 77205e7 commit 81ea282

File tree

10 files changed

+105
-159
lines changed

10 files changed

+105
-159
lines changed

cli/command/stack/common.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import (
44
"fmt"
55
"strings"
66
"unicode"
7+
8+
"github.com/docker/cli/cli/compose/convert"
9+
"github.com/docker/cli/opts"
10+
"github.com/moby/moby/api/types/filters"
711
)
812

913
// validateStackName checks if the provided string is a valid stack name (namespace).
@@ -29,3 +33,9 @@ func validateStackNames(namespaces []string) error {
2933
func quotesOrWhitespace(r rune) bool {
3034
return unicode.IsSpace(r) || r == '"' || r == '\''
3135
}
36+
37+
func getStackFilterFromOpt(namespace string, opt opts.FilterOpt) filters.Args {
38+
filter := opt.Value()
39+
filter.Add("label", convert.LabelNamespace+"="+namespace)
40+
return filter
41+
}

cli/command/stack/list.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ import (
99
"github.com/docker/cli/cli/command"
1010
"github.com/docker/cli/cli/command/completion"
1111
"github.com/docker/cli/cli/command/stack/formatter"
12-
"github.com/docker/cli/cli/command/stack/options"
1312
"github.com/docker/cli/cli/command/stack/swarm"
1413
flagsHelper "github.com/docker/cli/cli/flags"
1514
"github.com/fvbommel/sortorder"
1615
"github.com/spf13/cobra"
1716
)
1817

19-
type listOptions = options.List
18+
// listOptions holds docker stack ls options
19+
type listOptions struct {
20+
format string
21+
}
2022

21-
func newListCommand(dockerCli command.Cli) *cobra.Command {
23+
func newListCommand(dockerCLI command.Cli) *cobra.Command {
2224
opts := listOptions{}
2325

2426
cmd := &cobra.Command{
@@ -27,23 +29,16 @@ func newListCommand(dockerCli command.Cli) *cobra.Command {
2729
Short: "List stacks",
2830
Args: cli.NoArgs,
2931
RunE: func(cmd *cobra.Command, args []string) error {
30-
return runList(cmd.Context(), dockerCli, opts)
32+
return runList(cmd.Context(), dockerCLI, opts)
3133
},
3234
ValidArgsFunction: completion.NoComplete,
3335
}
3436

3537
flags := cmd.Flags()
36-
flags.StringVar(&opts.Format, "format", "", flagsHelper.FormatHelp)
38+
flags.StringVar(&opts.format, "format", "", flagsHelper.FormatHelp)
3739
return cmd
3840
}
3941

40-
// RunList performs a stack list against the specified swarm cluster
41-
//
42-
// Deprecated: this function was for internal use and will be removed in the next release.
43-
func RunList(ctx context.Context, dockerCLI command.Cli, opts options.List) error {
44-
return runList(ctx, dockerCLI, opts)
45-
}
46-
4742
// runList performs a stack list against the specified swarm cluster
4843
func runList(ctx context.Context, dockerCLI command.Cli, opts listOptions) error {
4944
stacks, err := swarm.GetStacks(ctx, dockerCLI.Client())
@@ -54,7 +49,7 @@ func runList(ctx context.Context, dockerCLI command.Cli, opts listOptions) error
5449
}
5550

5651
func format(out io.Writer, opts listOptions, stacks []formatter.Stack) error {
57-
fmt := formatter.Format(opts.Format)
52+
fmt := formatter.Format(opts.format)
5853
if fmt == "" || fmt == formatter.TableFormatKey {
5954
fmt = formatter.SwarmStackTableFormat
6055
}

cli/command/stack/options/opts.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package options
22

3-
import "github.com/docker/cli/opts"
4-
53
// Deploy holds docker stack deploy options
64
//
75
// Deprecated: this type was for internal use and will be removed in the next release.
@@ -23,40 +21,10 @@ type Config struct {
2321
SkipInterpolation bool
2422
}
2523

26-
// List holds docker stack ls options
27-
//
28-
// Deprecated: this type was for internal use and will be removed in the next release.
29-
type List struct {
30-
Format string
31-
AllNamespaces bool
32-
}
33-
34-
// PS holds docker stack ps options
35-
//
36-
// Deprecated: this type was for internal use and will be removed in the next release.
37-
type PS struct {
38-
Filter opts.FilterOpt
39-
NoTrunc bool
40-
Namespace string
41-
NoResolve bool
42-
Quiet bool
43-
Format string
44-
}
45-
4624
// Remove holds docker stack remove options
4725
//
4826
// Deprecated: this type was for internal use and will be removed in the next release.
4927
type Remove struct {
5028
Namespaces []string
5129
Detach bool
5230
}
53-
54-
// Services holds docker stack services options
55-
//
56-
// Deprecated: this type was for internal use and will be removed in the next release.
57-
type Services struct {
58-
Quiet bool
59-
Format string
60-
Filter opts.FilterOpt
61-
Namespace string
62-
}

cli/command/stack/ps.go

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,73 @@
11
package stack
22

33
import (
4+
"context"
5+
"fmt"
6+
47
"github.com/docker/cli/cli"
58
"github.com/docker/cli/cli/command"
6-
"github.com/docker/cli/cli/command/stack/options"
7-
"github.com/docker/cli/cli/command/stack/swarm"
9+
"github.com/docker/cli/cli/command/idresolver"
10+
"github.com/docker/cli/cli/command/task"
811
flagsHelper "github.com/docker/cli/cli/flags"
912
cliopts "github.com/docker/cli/opts"
13+
"github.com/moby/moby/client"
1014
"github.com/spf13/cobra"
1115
)
1216

13-
func newPsCommand(dockerCli command.Cli) *cobra.Command {
14-
opts := options.PS{Filter: cliopts.NewFilterOpt()}
17+
// psOptions holds docker stack ps options
18+
type psOptions struct {
19+
filter cliopts.FilterOpt
20+
noTrunc bool
21+
namespace string
22+
noResolve bool
23+
quiet bool
24+
format string
25+
}
26+
27+
func newPsCommand(dockerCLI command.Cli) *cobra.Command {
28+
opts := psOptions{filter: cliopts.NewFilterOpt()}
1529

1630
cmd := &cobra.Command{
1731
Use: "ps [OPTIONS] STACK",
1832
Short: "List the tasks in the stack",
1933
Args: cli.ExactArgs(1),
2034
RunE: func(cmd *cobra.Command, args []string) error {
21-
opts.Namespace = args[0]
22-
if err := validateStackName(opts.Namespace); err != nil {
35+
opts.namespace = args[0]
36+
if err := validateStackName(opts.namespace); err != nil {
2337
return err
2438
}
25-
return swarm.RunPS(cmd.Context(), dockerCli, opts)
39+
return runPS(cmd.Context(), dockerCLI, opts)
2640
},
2741
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
28-
return completeNames(dockerCli)(cmd, args, toComplete)
42+
return completeNames(dockerCLI)(cmd, args, toComplete)
2943
},
3044
}
3145
flags := cmd.Flags()
32-
flags.BoolVar(&opts.NoTrunc, "no-trunc", false, "Do not truncate output")
33-
flags.BoolVar(&opts.NoResolve, "no-resolve", false, "Do not map IDs to Names")
34-
flags.VarP(&opts.Filter, "filter", "f", "Filter output based on conditions provided")
35-
flags.BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display task IDs")
36-
flags.StringVar(&opts.Format, "format", "", flagsHelper.FormatHelp)
46+
flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Do not truncate output")
47+
flags.BoolVar(&opts.noResolve, "no-resolve", false, "Do not map IDs to Names")
48+
flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
49+
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display task IDs")
50+
flags.StringVar(&opts.format, "format", "", flagsHelper.FormatHelp)
3751
return cmd
3852
}
53+
54+
// runPS is the swarm implementation of docker stack ps
55+
func runPS(ctx context.Context, dockerCLI command.Cli, opts psOptions) error {
56+
apiClient := dockerCLI.Client()
57+
tasks, err := apiClient.TaskList(ctx, client.TaskListOptions{
58+
Filters: getStackFilterFromOpt(opts.namespace, opts.filter),
59+
})
60+
if err != nil {
61+
return err
62+
}
63+
64+
if len(tasks) == 0 {
65+
return fmt.Errorf("nothing found in stack: %s", opts.namespace)
66+
}
67+
68+
if opts.format == "" {
69+
opts.format = task.DefaultFormat(dockerCLI.ConfigFile(), opts.quiet)
70+
}
71+
72+
return task.Print(ctx, dockerCLI, tasks, idresolver.New(apiClient, opts.noResolve), !opts.noTrunc, opts.quiet, opts.format)
73+
}

cli/command/stack/services.go

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,33 @@ import (
77

88
"github.com/docker/cli/cli"
99
"github.com/docker/cli/cli/command"
10+
"github.com/docker/cli/cli/command/formatter"
1011
"github.com/docker/cli/cli/command/service"
11-
"github.com/docker/cli/cli/command/stack/formatter"
12-
"github.com/docker/cli/cli/command/stack/options"
13-
"github.com/docker/cli/cli/command/stack/swarm"
1412
flagsHelper "github.com/docker/cli/cli/flags"
1513
cliopts "github.com/docker/cli/opts"
1614
"github.com/fvbommel/sortorder"
17-
swarmtypes "github.com/moby/moby/api/types/swarm"
15+
"github.com/moby/moby/api/types/swarm"
1816
"github.com/spf13/cobra"
1917
)
2018

21-
// servicesOptions holds docker stack services options
22-
type servicesOptions = options.Services
19+
// serviceListOptions holds docker stack services options
20+
type serviceListOptions = struct {
21+
quiet bool
22+
format string
23+
filter cliopts.FilterOpt
24+
namespace string
25+
}
2326

2427
func newServicesCommand(dockerCLI command.Cli) *cobra.Command {
25-
opts := servicesOptions{Filter: cliopts.NewFilterOpt()}
28+
opts := serviceListOptions{filter: cliopts.NewFilterOpt()}
2629

2730
cmd := &cobra.Command{
2831
Use: "services [OPTIONS] STACK",
2932
Short: "List the services in the stack",
3033
Args: cli.ExactArgs(1),
3134
RunE: func(cmd *cobra.Command, args []string) error {
32-
opts.Namespace = args[0]
33-
if err := validateStackName(opts.Namespace); err != nil {
35+
opts.namespace = args[0]
36+
if err := validateStackName(opts.namespace); err != nil {
3437
return err
3538
}
3639
return runServices(cmd.Context(), dockerCLI, opts)
@@ -40,41 +43,34 @@ func newServicesCommand(dockerCLI command.Cli) *cobra.Command {
4043
},
4144
}
4245
flags := cmd.Flags()
43-
flags.BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs")
44-
flags.StringVar(&opts.Format, "format", "", flagsHelper.FormatHelp)
45-
flags.VarP(&opts.Filter, "filter", "f", "Filter output based on conditions provided")
46+
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display IDs")
47+
flags.StringVar(&opts.format, "format", "", flagsHelper.FormatHelp)
48+
flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
4649
return cmd
4750
}
4851

49-
// RunServices performs a stack services against the specified swarm cluster
50-
//
51-
// Deprecated: this function was for internal use and will be removed in the next release.
52-
func RunServices(ctx context.Context, dockerCLI command.Cli, opts options.Services) error {
53-
return runServices(ctx, dockerCLI, opts)
54-
}
55-
5652
// runServices performs a stack services against the specified swarm cluster
57-
func runServices(ctx context.Context, dockerCLI command.Cli, opts servicesOptions) error {
58-
services, err := swarm.GetServices(ctx, dockerCLI, opts)
53+
func runServices(ctx context.Context, dockerCLI command.Cli, opts serviceListOptions) error {
54+
services, err := getServices(ctx, dockerCLI.Client(), opts)
5955
if err != nil {
6056
return err
6157
}
6258
return formatWrite(dockerCLI, services, opts)
6359
}
6460

65-
func formatWrite(dockerCLI command.Cli, services []swarmtypes.Service, opts servicesOptions) error {
61+
func formatWrite(dockerCLI command.Cli, services []swarm.Service, opts serviceListOptions) error {
6662
// if no services in the stack, print message and exit 0
6763
if len(services) == 0 {
68-
_, _ = fmt.Fprintln(dockerCLI.Err(), "Nothing found in stack:", opts.Namespace)
64+
_, _ = fmt.Fprintln(dockerCLI.Err(), "Nothing found in stack:", opts.namespace)
6965
return nil
7066
}
7167
sort.Slice(services, func(i, j int) bool {
7268
return sortorder.NaturalLess(services[i].Spec.Name, services[j].Spec.Name)
7369
})
7470

75-
f := opts.Format
71+
f := opts.format
7672
if len(f) == 0 {
77-
if len(dockerCLI.ConfigFile().ServicesFormat) > 0 && !opts.Quiet {
73+
if len(dockerCLI.ConfigFile().ServicesFormat) > 0 && !opts.quiet {
7874
f = dockerCLI.ConfigFile().ServicesFormat
7975
} else {
8076
f = formatter.TableFormatKey
@@ -83,7 +79,7 @@ func formatWrite(dockerCLI command.Cli, services []swarmtypes.Service, opts serv
8379

8480
servicesCtx := formatter.Context{
8581
Output: dockerCLI.Out(),
86-
Format: service.NewListFormat(f, opts.Quiet),
82+
Format: service.NewListFormat(f, opts.quiet),
8783
}
8884
return service.ListFormatWrite(servicesCtx, services)
8985
}
Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
1-
package swarm
1+
package stack
22

33
import (
44
"context"
55

6-
"github.com/docker/cli/cli/command"
76
"github.com/docker/cli/cli/command/service"
8-
"github.com/docker/cli/cli/command/stack/options"
97
"github.com/moby/moby/api/types/swarm"
108
"github.com/moby/moby/client"
119
)
1210

13-
// GetServices is the swarm implementation of listing stack services
14-
//
15-
// Deprecated: this function was for internal use and will be removed in the next release.
16-
func GetServices(ctx context.Context, dockerCLI command.Cli, opts options.Services) ([]swarm.Service, error) {
17-
var (
18-
err error
19-
apiClient = dockerCLI.Client()
20-
)
21-
11+
// getServices is the swarm implementation of listing stack services
12+
func getServices(ctx context.Context, apiClient client.APIClient, opts serviceListOptions) ([]swarm.Service, error) {
2213
listOpts := client.ServiceListOptions{
23-
Filters: getStackFilterFromOpt(opts.Namespace, opts.Filter),
14+
Filters: getStackFilterFromOpt(opts.namespace, opts.filter),
2415
// When not running "quiet", also get service status (number of running
2516
// and desired tasks). Note that this is only supported on API v1.41 and
2617
// up; older API versions ignore this option, and we will have to collect
2718
// the information manually below.
28-
Status: !opts.Quiet,
19+
Status: !opts.quiet,
2920
}
3021

3122
services, err := apiClient.ServiceList(ctx, listOpts)

cli/command/stack/swarm/common.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55

66
"github.com/docker/cli/cli/compose/convert"
7-
"github.com/docker/cli/opts"
87
"github.com/moby/moby/api/types/filters"
98
"github.com/moby/moby/api/types/network"
109
"github.com/moby/moby/api/types/swarm"
@@ -17,12 +16,6 @@ func getStackFilter(namespace string) filters.Args {
1716
return filter
1817
}
1918

20-
func getStackFilterFromOpt(namespace string, opt opts.FilterOpt) filters.Args {
21-
filter := opt.Value()
22-
filter.Add("label", convert.LabelNamespace+"="+namespace)
23-
return filter
24-
}
25-
2619
func getAllStacksFilter() filters.Args {
2720
filter := filters.NewArgs()
2821
filter.Add("label", convert.LabelNamespace)

0 commit comments

Comments
 (0)