github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/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 "launchpad.net/gnuflag" 12 13 "github.com/juju/juju/apiserver/params" 14 ) 15 16 const ListCommandDoc = ` 17 List cached os images in the Juju environment. 18 19 Images can be filtered on: 20 Kind eg "lxc" 21 Series eg "trusty" 22 Architecture eg "amd64" 23 The filter attributes are optional. 24 25 Examples: 26 27 # List all cached images. 28 juju cache-images list 29 30 # List cached images for trusty. 31 juju cache-images list --series trusty 32 33 # List all cached lxc images for trusty amd64. 34 juju cache-images list --kind lxc --series trusty --arch amd64 35 ` 36 37 // ListCommand shows the images in the Juju server. 38 type ListCommand struct { 39 CachedImagesCommandBase 40 out cmd.Output 41 Kind, Series, Arch string 42 } 43 44 // Info implements Command.Info. 45 func (c *ListCommand) Info() *cmd.Info { 46 return &cmd.Info{ 47 Name: "list", 48 Purpose: "shows cached os images", 49 Doc: ListCommandDoc, 50 } 51 } 52 53 // SetFlags implements Command.SetFlags. 54 func (c *ListCommand) SetFlags(f *gnuflag.FlagSet) { 55 c.CachedImagesCommandBase.SetFlags(f) 56 f.StringVar(&c.Kind, "kind", "", "the image kind to list eg lxc") 57 f.StringVar(&c.Series, "series", "", "the series of the image to list eg trusty") 58 f.StringVar(&c.Arch, "arch", "", "the architecture of the image to list eg amd64") 59 c.out.AddFlags(f, "yaml", map[string]cmd.Formatter{ 60 "yaml": cmd.FormatYaml, 61 "json": cmd.FormatJson, 62 }) 63 } 64 65 // Init implements Command.Init. 66 func (c *ListCommand) Init(args []string) (err error) { 67 return cmd.CheckEmpty(args) 68 } 69 70 // ListImagesAPI defines the imagemanager API methods that the list command uses. 71 type ListImagesAPI interface { 72 ListImages(kind, series, arch string) ([]params.ImageMetadata, error) 73 Close() error 74 } 75 76 var getListImagesAPI = func(p *ListCommand) (ListImagesAPI, error) { 77 return p.NewImagesManagerClient() 78 } 79 80 // ImageInfo defines the serialization behaviour of image metadata. 81 type ImageInfo struct { 82 Kind string `yaml:"kind" json:"kind"` 83 Series string `yaml:"series" json:"series"` 84 Arch string `yaml:"arch" json:"arch"` 85 SourceURL string `yaml:"source-url" json:"source-url"` 86 Created string `yaml:"created" json:"created"` 87 } 88 89 func (c *ListCommand) imageMetadataToImageInfo(images []params.ImageMetadata) []ImageInfo { 90 var output []ImageInfo 91 for _, metadata := range images { 92 imageInfo := ImageInfo{ 93 Kind: metadata.Kind, 94 Series: metadata.Series, 95 Arch: metadata.Arch, 96 Created: metadata.Created.Format(time.RFC1123), 97 SourceURL: metadata.URL, 98 } 99 output = append(output, imageInfo) 100 } 101 return output 102 } 103 104 // Run implements Command.Run. 105 func (c *ListCommand) Run(ctx *cmd.Context) (err error) { 106 client, err := getListImagesAPI(c) 107 if err != nil { 108 return err 109 } 110 defer client.Close() 111 112 results, err := client.ListImages(c.Kind, c.Series, c.Arch) 113 if err != nil { 114 return err 115 } 116 imageInfo := c.imageMetadataToImageInfo(results) 117 if len(imageInfo) == 0 { 118 fmt.Fprintf(ctx.Stdout, "no matching images found\n") 119 return nil 120 } 121 fmt.Fprintf(ctx.Stdout, "Cached images:\n") 122 return c.out.Write(ctx, imageInfo) 123 }