github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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  	"github.com/juju/juju/cmd/modelcmd"
    15  )
    16  
    17  const listCommandDoc = `
    18  List cached os images in the Juju model.
    19  
    20  Images can be filtered on:
    21    Kind         eg "lxd"
    22    Series       eg "xenial"
    23    Architecture eg "amd64"
    24  The filter attributes are optional.
    25  
    26  Examples:
    27  
    28    # List all cached images.
    29    juju list-cache-images
    30  
    31    # List cached images for xenial.
    32    juju list-cache-images --series xenial 
    33  
    34    # List all cached lxd images for xenial amd64.
    35    juju list-cache-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:    "list-cached-images",
    54  		Purpose: "shows cached os images",
    55  		Doc:     listCommandDoc,
    56  		Aliases: []string{"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", map[string]cmd.Formatter{
    67  		"yaml": cmd.FormatYaml,
    68  		"json": cmd.FormatJson,
    69  	})
    70  }
    71  
    72  // Init implements Command.Init.
    73  func (c *listCommand) Init(args []string) (err error) {
    74  	return cmd.CheckEmpty(args)
    75  }
    76  
    77  // ListImagesAPI defines the imagemanager API methods that the list command uses.
    78  type ListImagesAPI interface {
    79  	ListImages(kind, series, arch string) ([]params.ImageMetadata, error)
    80  	Close() error
    81  }
    82  
    83  var getListImagesAPI = func(p *CachedImagesCommandBase) (ListImagesAPI, error) {
    84  	return p.NewImagesManagerClient()
    85  }
    86  
    87  // ImageInfo defines the serialization behaviour of image metadata.
    88  type ImageInfo struct {
    89  	Kind      string `yaml:"kind" json:"kind"`
    90  	Series    string `yaml:"series" json:"series"`
    91  	Arch      string `yaml:"arch" json:"arch"`
    92  	SourceURL string `yaml:"source-url" json:"source-url"`
    93  	Created   string `yaml:"created" json:"created"`
    94  }
    95  
    96  func (c *listCommand) imageMetadataToImageInfo(images []params.ImageMetadata) []ImageInfo {
    97  	var output []ImageInfo
    98  	for _, metadata := range images {
    99  		imageInfo := ImageInfo{
   100  			Kind:      metadata.Kind,
   101  			Series:    metadata.Series,
   102  			Arch:      metadata.Arch,
   103  			Created:   metadata.Created.Format(time.RFC1123),
   104  			SourceURL: metadata.URL,
   105  		}
   106  		output = append(output, imageInfo)
   107  	}
   108  	return output
   109  }
   110  
   111  // Run implements Command.Run.
   112  func (c *listCommand) Run(ctx *cmd.Context) (err error) {
   113  	client, err := getListImagesAPI(&c.CachedImagesCommandBase)
   114  	if err != nil {
   115  		return err
   116  	}
   117  	defer client.Close()
   118  
   119  	results, err := client.ListImages(c.Kind, c.Series, c.Arch)
   120  	if err != nil {
   121  		return err
   122  	}
   123  	imageInfo := c.imageMetadataToImageInfo(results)
   124  	if len(imageInfo) == 0 {
   125  		fmt.Fprintf(ctx.Stdout, "no matching images found\n")
   126  		return nil
   127  	}
   128  	fmt.Fprintf(ctx.Stdout, "Cached images:\n")
   129  	return c.out.Write(ctx, imageInfo)
   130  }