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