github.com/scaleway/scaleway-cli@v1.11.1/pkg/cli/cmd_ps.go (about)

     1  // Copyright (C) 2015 Scaleway. All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE.md file.
     4  
     5  package cli
     6  
     7  import (
     8  	"strings"
     9  
    10  	"github.com/Sirupsen/logrus"
    11  	"github.com/scaleway/scaleway-cli/pkg/commands"
    12  )
    13  
    14  var cmdPs = &Command{
    15  	Exec:        runPs,
    16  	UsageLine:   "ps [OPTIONS]",
    17  	Description: "List servers",
    18  	Help:        "List servers. By default, only running servers are displayed.",
    19  	Examples: `
    20      $ scw ps
    21      $ scw ps -a
    22      $ scw ps -l
    23      $ scw ps -n=10
    24      $ scw ps -q
    25      $ scw ps --no-trunc
    26      $ scw ps -f state=booted
    27      $ scw ps -f state=running
    28      $ scw ps -f state=stopped
    29      $ scw ps -f ip=212.47.229.26
    30      $ scw ps -f tags=prod
    31      $ scw ps -f tags=boot=live
    32      $ scw ps -f image=docker
    33      $ scw ps -f image=alpine
    34      $ scw ps -f image=UUIDOFIMAGE
    35      $ scw ps -f arch=ARCH
    36      $ scw ps -f server-type=COMMERCIALTYPE
    37      $ scw ps -f "state=booted image=docker tags=prod"
    38  `,
    39  }
    40  
    41  func init() {
    42  	cmdPs.Flag.BoolVar(&psA, []string{"a", "-all"}, false, "Show all servers. Only running servers are shown by default")
    43  	cmdPs.Flag.BoolVar(&psL, []string{"l", "-latest"}, false, "Show only the latest created server, include non-running ones")
    44  	cmdPs.Flag.IntVar(&psN, []string{"n"}, 0, "Show n last created servers, include non-running ones")
    45  	cmdPs.Flag.BoolVar(&psNoTrunc, []string{"-no-trunc"}, false, "Don't truncate output")
    46  	cmdPs.Flag.BoolVar(&psQ, []string{"q", "-quiet"}, false, "Only display numeric IDs")
    47  	cmdPs.Flag.BoolVar(&psHelp, []string{"h", "-help"}, false, "Print usage")
    48  	cmdPs.Flag.StringVar(&psFilters, []string{"f", "-filter"}, "", "Filter output based on conditions provided")
    49  }
    50  
    51  // Flags
    52  var psA bool         // -a flag
    53  var psL bool         // -l flag
    54  var psQ bool         // -q flag
    55  var psNoTrunc bool   // -no-trunc flag
    56  var psN int          // -n flag
    57  var psHelp bool      // -h, --help flag
    58  var psFilters string // -f, --filter flag
    59  
    60  func runPs(cmd *Command, rawArgs []string) error {
    61  	if psHelp {
    62  		return cmd.PrintUsage()
    63  	}
    64  	if len(rawArgs) != 0 {
    65  		return cmd.PrintShortUsage()
    66  	}
    67  
    68  	args := commands.PsArgs{
    69  		All:     psA,
    70  		Latest:  psL,
    71  		Quiet:   psQ,
    72  		NoTrunc: psNoTrunc,
    73  		NLast:   psN,
    74  		Filters: make(map[string]string, 0),
    75  	}
    76  	if psFilters != "" {
    77  		for _, filter := range strings.Split(psFilters, " ") {
    78  			parts := strings.SplitN(filter, "=", 2)
    79  			if len(parts) != 2 {
    80  				logrus.Warnf("Invalid filter '%s', should be in the form 'key=value'", filter)
    81  				continue
    82  			}
    83  			if _, ok := args.Filters[parts[0]]; ok {
    84  				logrus.Warnf("Duplicated filter: %q", parts[0])
    85  			} else {
    86  				args.Filters[parts[0]] = parts[1]
    87  			}
    88  		}
    89  	}
    90  	ctx := cmd.GetContext(rawArgs)
    91  	return commands.RunPs(ctx, args)
    92  }