github.1git.de/docker/cli@v26.1.3+incompatible/cli/command/stack/services.go (about)

     1  package stack
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"sort"
     7  
     8  	"github.com/docker/cli/cli"
     9  	"github.com/docker/cli/cli/command"
    10  	"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"
    14  	flagsHelper "github.com/docker/cli/cli/flags"
    15  	cliopts "github.com/docker/cli/opts"
    16  	swarmtypes "github.com/docker/docker/api/types/swarm"
    17  	"github.com/fvbommel/sortorder"
    18  	"github.com/spf13/cobra"
    19  )
    20  
    21  func newServicesCommand(dockerCli command.Cli) *cobra.Command {
    22  	opts := options.Services{Filter: cliopts.NewFilterOpt()}
    23  
    24  	cmd := &cobra.Command{
    25  		Use:   "services [OPTIONS] STACK",
    26  		Short: "List the services in the stack",
    27  		Args:  cli.ExactArgs(1),
    28  		RunE: func(cmd *cobra.Command, args []string) error {
    29  			opts.Namespace = args[0]
    30  			if err := validateStackName(opts.Namespace); err != nil {
    31  				return err
    32  			}
    33  			return RunServices(cmd.Context(), dockerCli, opts)
    34  		},
    35  		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    36  			return completeNames(dockerCli)(cmd, args, toComplete)
    37  		},
    38  	}
    39  	flags := cmd.Flags()
    40  	flags.BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs")
    41  	flags.StringVar(&opts.Format, "format", "", flagsHelper.FormatHelp)
    42  	flags.VarP(&opts.Filter, "filter", "f", "Filter output based on conditions provided")
    43  	return cmd
    44  }
    45  
    46  // RunServices performs a stack services against the specified swarm cluster
    47  func RunServices(ctx context.Context, dockerCli command.Cli, opts options.Services) error {
    48  	services, err := swarm.GetServices(ctx, dockerCli, opts)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	return formatWrite(dockerCli, services, opts)
    53  }
    54  
    55  func formatWrite(dockerCli command.Cli, services []swarmtypes.Service, opts options.Services) error {
    56  	// if no services in the stack, print message and exit 0
    57  	if len(services) == 0 {
    58  		_, _ = fmt.Fprintf(dockerCli.Err(), "Nothing found in stack: %s\n", opts.Namespace)
    59  		return nil
    60  	}
    61  	sort.Slice(services, func(i, j int) bool {
    62  		return sortorder.NaturalLess(services[i].Spec.Name, services[j].Spec.Name)
    63  	})
    64  
    65  	format := opts.Format
    66  	if len(format) == 0 {
    67  		if len(dockerCli.ConfigFile().ServicesFormat) > 0 && !opts.Quiet {
    68  			format = dockerCli.ConfigFile().ServicesFormat
    69  		} else {
    70  			format = formatter.TableFormatKey
    71  		}
    72  	}
    73  
    74  	servicesCtx := formatter.Context{
    75  		Output: dockerCli.Out(),
    76  		Format: service.NewListFormat(format, opts.Quiet),
    77  	}
    78  	return service.ListFormatWrite(servicesCtx, services)
    79  }