DockerCLI/cli/command/stack/list.go

63 lines
1.7 KiB
Go
Raw Normal View History

package stack
import (
"context"
"io"
"sort"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion"
"github.com/docker/cli/cli/command/stack/formatter"
"github.com/docker/cli/cli/command/stack/options"
"github.com/docker/cli/cli/command/stack/swarm"
flagsHelper "github.com/docker/cli/cli/flags"
"github.com/fvbommel/sortorder"
"github.com/spf13/cobra"
)
func newListCommand(dockerCli command.Cli) *cobra.Command {
opts := options.List{}
cmd := &cobra.Command{
Use: "ls [OPTIONS]",
Aliases: []string{"list"},
Short: "List stacks",
Args: cli.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return RunList(cmd.Context(), dockerCli, opts)
},
ValidArgsFunction: completion.NoComplete,
}
flags := cmd.Flags()
flags.StringVar(&opts.Format, "format", "", flagsHelper.FormatHelp)
return cmd
}
// RunList performs a stack list against the specified swarm cluster
func RunList(ctx context.Context, dockerCli command.Cli, opts options.List) error {
ss, err := swarm.GetStacks(ctx, dockerCli.Client())
if err != nil {
return err
}
cli/command/stack: deprecate now obsolete wrappers These wrappers were added to abstract stack deploy to k8s and swarm. Now that support for deploying to k8s was removed, we can remove these wrappers. This deprecates: - RunDeploy() - RunPs() - RunRemove() - GetServices() This also addresses some linting failers, due to these functions having unused arguments: cli/command/stack/deploy.go:51:39: unused-parameter: parameter 'flags' seems to be unused, consider removing or renaming it as _ (revive) func RunDeploy(dockerCli command.Cli, flags *pflag.FlagSet, config *composetypes.Config, opts options.Deploy) error { ^ cli/command/stack/ps.go:42:35: unused-parameter: parameter 'flags' seems to be unused, consider removing or renaming it as _ (revive) func RunPs(dockerCli command.Cli, flags *pflag.FlagSet, opts options.PS) error { ^ cli/command/stack/remove.go:35:39: unused-parameter: parameter 'flags' seems to be unused, consider removing or renaming it as _ (revive) func RunRemove(dockerCli command.Cli, flags *pflag.FlagSet, opts options.Remove) error { ^ cli/command/stack/list.go:37:14: unused-parameter: parameter 'cmd' seems to be unused, consider removing or renaming it as _ (revive) func RunList(cmd *cobra.Command, dockerCli command.Cli, opts options.List) error { ^ cli/command/stack/services.go:56:41: unused-parameter: parameter 'flags' seems to be unused, consider removing or renaming it as _ (revive) func GetServices(dockerCli command.Cli, flags *pflag.FlagSet, opts options.Services) ([]swarmtypes.Service, error) { ^ Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-03-29 09:42:17 -04:00
stacks := make([]*formatter.Stack, 0, len(ss))
stacks = append(stacks, ss...)
return format(dockerCli.Out(), opts, stacks)
}
func format(out io.Writer, opts options.List, stacks []*formatter.Stack) error {
cli/command/stack: deprecate now obsolete wrappers These wrappers were added to abstract stack deploy to k8s and swarm. Now that support for deploying to k8s was removed, we can remove these wrappers. This deprecates: - RunDeploy() - RunPs() - RunRemove() - GetServices() This also addresses some linting failers, due to these functions having unused arguments: cli/command/stack/deploy.go:51:39: unused-parameter: parameter 'flags' seems to be unused, consider removing or renaming it as _ (revive) func RunDeploy(dockerCli command.Cli, flags *pflag.FlagSet, config *composetypes.Config, opts options.Deploy) error { ^ cli/command/stack/ps.go:42:35: unused-parameter: parameter 'flags' seems to be unused, consider removing or renaming it as _ (revive) func RunPs(dockerCli command.Cli, flags *pflag.FlagSet, opts options.PS) error { ^ cli/command/stack/remove.go:35:39: unused-parameter: parameter 'flags' seems to be unused, consider removing or renaming it as _ (revive) func RunRemove(dockerCli command.Cli, flags *pflag.FlagSet, opts options.Remove) error { ^ cli/command/stack/list.go:37:14: unused-parameter: parameter 'cmd' seems to be unused, consider removing or renaming it as _ (revive) func RunList(cmd *cobra.Command, dockerCli command.Cli, opts options.List) error { ^ cli/command/stack/services.go:56:41: unused-parameter: parameter 'flags' seems to be unused, consider removing or renaming it as _ (revive) func GetServices(dockerCli command.Cli, flags *pflag.FlagSet, opts options.Services) ([]swarmtypes.Service, error) { ^ Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-03-29 09:42:17 -04:00
fmt := formatter.Format(opts.Format)
if fmt == "" || fmt == formatter.TableFormatKey {
fmt = formatter.SwarmStackTableFormat
}
stackCtx := formatter.Context{
Output: out,
cli/command/stack: deprecate now obsolete wrappers These wrappers were added to abstract stack deploy to k8s and swarm. Now that support for deploying to k8s was removed, we can remove these wrappers. This deprecates: - RunDeploy() - RunPs() - RunRemove() - GetServices() This also addresses some linting failers, due to these functions having unused arguments: cli/command/stack/deploy.go:51:39: unused-parameter: parameter 'flags' seems to be unused, consider removing or renaming it as _ (revive) func RunDeploy(dockerCli command.Cli, flags *pflag.FlagSet, config *composetypes.Config, opts options.Deploy) error { ^ cli/command/stack/ps.go:42:35: unused-parameter: parameter 'flags' seems to be unused, consider removing or renaming it as _ (revive) func RunPs(dockerCli command.Cli, flags *pflag.FlagSet, opts options.PS) error { ^ cli/command/stack/remove.go:35:39: unused-parameter: parameter 'flags' seems to be unused, consider removing or renaming it as _ (revive) func RunRemove(dockerCli command.Cli, flags *pflag.FlagSet, opts options.Remove) error { ^ cli/command/stack/list.go:37:14: unused-parameter: parameter 'cmd' seems to be unused, consider removing or renaming it as _ (revive) func RunList(cmd *cobra.Command, dockerCli command.Cli, opts options.List) error { ^ cli/command/stack/services.go:56:41: unused-parameter: parameter 'flags' seems to be unused, consider removing or renaming it as _ (revive) func GetServices(dockerCli command.Cli, flags *pflag.FlagSet, opts options.Services) ([]swarmtypes.Service, error) { ^ Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-03-29 09:42:17 -04:00
Format: fmt,
}
sort.Slice(stacks, func(i, j int) bool {
return sortorder.NaturalLess(stacks[i].Name, stacks[j].Name)
})
return formatter.StackWrite(stackCtx, stacks)
}