github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/cachedimages/list.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for infos. 3 4 package cachedimages 5 6 import ( 7 "fmt" 8 "time" 9 10 "github.com/juju/cmd" 11 "github.com/juju/gnuflag" 12 13 "github.com/juju/juju/apiserver/params" 14 "github.com/juju/juju/cmd/modelcmd" 15 "github.com/juju/juju/cmd/output" 16 ) 17 18 const listCommandDoc = ` 19 List cached os images in the Juju model. 20 21 Images can be filtered on: 22 Kind eg "lxd" 23 Series eg "xenial" 24 Architecture eg "amd64" 25 The filter attributes are optional. 26 27 Examples: 28 # List all cached images. 29 juju cached-images 30 31 # List cached images for xenial. 32 juju cached-images --series xenial 33 34 # List all cached lxd images for xenial amd64. 35 juju cached-images --kind lxd --series xenial --arch amd64 36 ` 37 38 // NewListCommand returns a command for listing chached images. 39 func NewListCommand() cmd.Command { 40 return modelcmd.Wrap(&listCommand{}) 41 } 42 43 // listCommand shows the images in the Juju server. 44 type listCommand struct { 45 CachedImagesCommandBase 46 out cmd.Output 47 Kind, Series, Arch string 48 } 49 50 // Info implements Command.Info. 51 func (c *listCommand) Info() *cmd.Info { 52 return &cmd.Info{ 53 Name: "cached-images", 54 Purpose: "Shows cached os images.", 55 Doc: listCommandDoc, 56 Aliases: []string{"list-cached-images"}, 57 } 58 } 59 60 // SetFlags implements Command.SetFlags. 61 func (c *listCommand) SetFlags(f *gnuflag.FlagSet) { 62 c.CachedImagesCommandBase.SetFlags(f) 63 f.StringVar(&c.Kind, "kind", "", "The image kind to list eg lxd") 64 f.StringVar(&c.Series, "series", "", "The series of the image to list eg xenial") 65 f.StringVar(&c.Arch, "arch", "", "The architecture of the image to list eg amd64") 66 c.out.AddFlags(f, "yaml", output.DefaultFormatters) 67 } 68 69 // Init implements Command.Init. 70 func (c *listCommand) Init(args []string) error { 71 return cmd.CheckEmpty(args) 72 } 73 74 // ListImagesAPI defines the imagemanager API methods that the list command uses. 75 type ListImagesAPI interface { 76 ListImages(kind, series, arch string) ([]params.ImageMetadata, error) 77 Close() error 78 } 79 80 var getListImagesAPI = func(p *CachedImagesCommandBase) (ListImagesAPI, error) { 81 return p.NewImagesManagerClient() 82 } 83 84 // ImageInfo defines the serialization behaviour of image metadata. 85 type ImageInfo struct { 86 Kind string `yaml:"kind" json:"kind"` 87 Series string `yaml:"series" json:"series"` 88 Arch string `yaml:"arch" json:"arch"` 89 SourceURL string `yaml:"source-url" json:"source-url"` 90 Created string `yaml:"created" json:"created"` 91 } 92 93 func (c *listCommand) imageMetadataToImageInfo(images []params.ImageMetadata) []ImageInfo { 94 var output []ImageInfo 95 for _, metadata := range images { 96 imageInfo := ImageInfo{ 97 Kind: metadata.Kind, 98 Series: metadata.Series, 99 Arch: metadata.Arch, 100 Created: metadata.Created.Format(time.RFC1123), 101 SourceURL: metadata.URL, 102 } 103 output = append(output, imageInfo) 104 } 105 return output 106 } 107 108 // Run implements Command.Run. 109 func (c *listCommand) Run(ctx *cmd.Context) (err error) { 110 client, err := getListImagesAPI(&c.CachedImagesCommandBase) 111 if err != nil { 112 return err 113 } 114 defer client.Close() 115 116 results, err := client.ListImages(c.Kind, c.Series, c.Arch) 117 if err != nil { 118 return err 119 } 120 imageInfo := c.imageMetadataToImageInfo(results) 121 if len(imageInfo) == 0 { 122 ctx.Infof("No images to display.") 123 return nil 124 } 125 fmt.Fprintf(ctx.Stdout, "Cached images:\n") 126 return c.out.Write(ctx, imageInfo) 127 }