github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/cmd/juju/charms/list.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package charms
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"launchpad.net/gnuflag"
     9  )
    10  
    11  const ListCommandDoc = `
    12  List charm URLs for requested charms.
    13  If no charms are requested, URLs for all charms
    14  in the Juju environment will be listed.
    15  
    16  options:
    17      -e, --environment (= "")
    18          juju environment to operate in
    19      --format (= yaml)
    20          specify output format (json|yaml)
    21      [<charm names>]
    22          list URLs for these charms only
    23      `
    24  
    25  // CharmsListAPI defines the API methods that the list command uses.
    26  type CharmsListAPI interface {
    27  	List(names []string) ([]string, error)
    28  	Close() error
    29  }
    30  
    31  // ListCommand lists charms URLs.
    32  type ListCommand struct {
    33  	CharmsCommandBase
    34  	Names []string
    35  	out   cmd.Output
    36  }
    37  
    38  // Info implements Command.Info.
    39  func (c *ListCommand) Info() *cmd.Info {
    40  	return &cmd.Info{
    41  		Name:    "list",
    42  		Args:    "[<charm names>]",
    43  		Purpose: "lists charm URLs",
    44  		Doc:     ListCommandDoc,
    45  	}
    46  }
    47  
    48  // SetFlags implements Command.SetFlags.
    49  func (c *ListCommand) SetFlags(f *gnuflag.FlagSet) {
    50  	c.CharmsCommandBase.SetFlags(f)
    51  	c.out.AddFlags(f, "yaml", map[string]cmd.Formatter{
    52  		"yaml": cmd.FormatYaml,
    53  		"json": cmd.FormatJson,
    54  	})
    55  }
    56  
    57  // Init implements Command.Init.
    58  func (c *ListCommand) Init(args []string) (err error) {
    59  	c.Names = args
    60  	return nil
    61  }
    62  
    63  var getCharmsListAPI = func(c *ListCommand) (CharmsListAPI, error) {
    64  	return c.NewCharmsClient()
    65  }
    66  
    67  // Run implements Command.Run.
    68  func (c *ListCommand) Run(ctx *cmd.Context) (err error) {
    69  	client, err := getCharmsListAPI(c)
    70  	if err != nil {
    71  		return err
    72  	}
    73  	defer client.Close()
    74  	result, err := client.List(c.Names)
    75  	if err != nil {
    76  		return err
    77  	}
    78  	return c.out.Write(ctx, result)
    79  }