github.com/scaleway/scaleway-cli@v1.11.1/pkg/cli/cmd_images.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 cmdImages = &Command{
    15  	Exec:        runImages,
    16  	UsageLine:   "images [OPTIONS]",
    17  	Description: "List images",
    18  	Help:        "List images.",
    19  	Examples: `
    20      $ scw images
    21      $ scw images -a
    22      $ scw images -q
    23      $ scw images --no-trunc
    24      $ scw images -f organization=me
    25      $ scw images -f organization=official-distribs
    26      $ scw images -f organization=official-apps
    27      $ scw images -f organization=UUIDOFORGANIZATION
    28      $ scw images -f name=ubuntu
    29      $ scw images -f type=image
    30      $ scw images -f type=bootscript
    31      $ scw images -f type=snapshot
    32      $ scw images -f type=volume
    33      $ scw images -f public=true
    34      $ scw images -f public=false
    35      $ scw images -f "organization=me type=volume" -q
    36  `,
    37  }
    38  
    39  func init() {
    40  	cmdImages.Flag.BoolVar(&imagesA, []string{"a", "-all"}, false, "Show all images")
    41  	cmdImages.Flag.BoolVar(&imagesNoTrunc, []string{"-no-trunc"}, false, "Don't truncate output")
    42  	cmdImages.Flag.BoolVar(&imagesQ, []string{"q", "-quiet"}, false, "Only show numeric IDs")
    43  	cmdImages.Flag.BoolVar(&imagesHelp, []string{"h", "-help"}, false, "Print usage")
    44  	cmdImages.Flag.StringVar(&imagesFilters, []string{"f", "-filter"}, "", "Filter output based on conditions provided")
    45  }
    46  
    47  // Flags
    48  var imagesA bool         // -a flag
    49  var imagesQ bool         // -q flag
    50  var imagesNoTrunc bool   // -no-trunc flag
    51  var imagesHelp bool      // -h, --help flag
    52  var imagesFilters string // -f, --filters
    53  
    54  func runImages(cmd *Command, rawArgs []string) error {
    55  	if imagesHelp {
    56  		return cmd.PrintUsage()
    57  	}
    58  	if len(rawArgs) != 0 {
    59  		return cmd.PrintShortUsage()
    60  	}
    61  
    62  	args := commands.ImagesArgs{
    63  		All:     imagesA,
    64  		Quiet:   imagesQ,
    65  		NoTrunc: imagesNoTrunc,
    66  		Filters: make(map[string]string, 0),
    67  	}
    68  	if imagesFilters != "" {
    69  		for _, filter := range strings.Split(imagesFilters, " ") {
    70  			parts := strings.SplitN(filter, "=", 2)
    71  			if len(parts) != 2 {
    72  				logrus.Warnf("Invalid filter '%s', should be in the form 'key=value'", filter)
    73  				continue
    74  			}
    75  			if _, ok := args.Filters[parts[0]]; ok {
    76  				logrus.Warnf("Duplicated filter: %q", parts[0])
    77  			} else {
    78  				args.Filters[parts[0]] = parts[1]
    79  			}
    80  		}
    81  	}
    82  	ctx := cmd.GetContext(rawArgs)
    83  	return commands.RunImages(ctx, args)
    84  }