Add --services flag to compose ps

Signed-off-by: aiordache <anca.iordache@docker.com>
This commit is contained in:
aiordache 2021-02-18 13:27:53 +01:00
parent 54b0cb9671
commit b11fedebdf

View File

@ -33,9 +33,10 @@ import (
type psOptions struct { type psOptions struct {
*projectOptions *projectOptions
Format string Format string
All bool All bool
Quiet bool Quiet bool
Services bool
} }
func psCommand(p *projectOptions) *cobra.Command { func psCommand(p *projectOptions) *cobra.Command {
@ -51,6 +52,7 @@ func psCommand(p *projectOptions) *cobra.Command {
} }
psCmd.Flags().StringVar(&opts.Format, "format", "pretty", "Format the output. Values: [pretty | json].") psCmd.Flags().StringVar(&opts.Format, "format", "pretty", "Format the output. Values: [pretty | json].")
psCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs") psCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs")
psCmd.Flags().BoolVar(&opts.Services, "services", false, "Display services")
psCmd.Flags().BoolVarP(&opts.All, "all", "a", false, "Show all stopped containers (including those created by the run command)") psCmd.Flags().BoolVarP(&opts.All, "all", "a", false, "Show all stopped containers (including those created by the run command)")
return psCmd return psCmd
} }
@ -71,6 +73,17 @@ func runPs(ctx context.Context, opts psOptions) error {
if err != nil { if err != nil {
return err return err
} }
if opts.Services {
services := []string{}
for _, s := range containers {
if !contains(services, s.Service) {
services = append(services, s.Service)
}
}
fmt.Println(strings.Join(services, "\n"))
return nil
}
if opts.Quiet { if opts.Quiet {
for _, s := range containers { for _, s := range containers {
fmt.Println(s.ID) fmt.Println(s.ID)
@ -102,3 +115,12 @@ func runPs(ctx context.Context, opts psOptions) error {
}, },
"NAME", "SERVICE", "STATUS", "PORTS") "NAME", "SERVICE", "STATUS", "PORTS")
} }
func contains(slice []string, item string) bool {
for _, v := range slice {
if v == item {
return true
}
}
return false
}