Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1321 from aiordache/local_ps_services
Browse files Browse the repository at this point in the history
Add `--services` flag to `compose ps`
  • Loading branch information
ndeloof authored Feb 18, 2021
2 parents 32fdd08 + b11fede commit 598f7bb
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions cli/cmd/compose/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ import (

type psOptions struct {
*projectOptions
Format string
All bool
Quiet bool
Format string
All bool
Quiet bool
Services bool
}

func psCommand(p *projectOptions) *cobra.Command {
Expand All @@ -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().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)")
return psCmd
}
Expand All @@ -71,6 +73,17 @@ func runPs(ctx context.Context, opts psOptions) error {
if err != nil {
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 {
for _, s := range containers {
fmt.Println(s.ID)
Expand Down Expand Up @@ -102,3 +115,12 @@ func runPs(ctx context.Context, opts psOptions) error {
},
"NAME", "SERVICE", "STATUS", "PORTS")
}

func contains(slice []string, item string) bool {
for _, v := range slice {
if v == item {
return true
}
}
return false
}

0 comments on commit 598f7bb

Please sign in to comment.