github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/payload/status/list.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package status
     5  
     6  import (
     7  	"fmt"
     8  	"io"
     9  
    10  	"github.com/juju/cmd"
    11  	"github.com/juju/errors"
    12  	"launchpad.net/gnuflag"
    13  
    14  	"github.com/juju/juju/cmd/modelcmd"
    15  	"github.com/juju/juju/payload"
    16  )
    17  
    18  // ListAPI has the API methods needed by ListCommand.
    19  type ListAPI interface {
    20  	ListFull(patterns ...string) ([]payload.FullPayloadInfo, error)
    21  	io.Closer
    22  }
    23  
    24  // ListCommand implements the list-payloads command.
    25  type ListCommand struct {
    26  	modelcmd.ModelCommandBase
    27  	out      cmd.Output
    28  	patterns []string
    29  
    30  	newAPIClient func(c *ListCommand) (ListAPI, error)
    31  }
    32  
    33  // NewListCommand returns a new command that lists charm payloads
    34  // in the current environment.
    35  func NewListCommand(newAPIClient func(c *ListCommand) (ListAPI, error)) *ListCommand {
    36  	cmd := &ListCommand{
    37  		newAPIClient: newAPIClient,
    38  	}
    39  	return cmd
    40  }
    41  
    42  // TODO(ericsnow) Change "tag" to "label" in the help text?
    43  
    44  var listDoc = `
    45  This command will report on the runtime state of defined payloads.
    46  
    47  When one or more pattern is given, Juju will limit the results to only
    48  those payloads which match *any* of the provided patterns. Each pattern
    49  will be checked against the following info in Juju:
    50  
    51  - unit name
    52  - machine id
    53  - payload type
    54  - payload class
    55  - payload id
    56  - payload tag
    57  - payload status
    58  `
    59  
    60  func (c *ListCommand) Info() *cmd.Info {
    61  	return &cmd.Info{
    62  		Name:    "list-payloads",
    63  		Args:    "[pattern ...]",
    64  		Purpose: "display status information about known payloads",
    65  		Doc:     listDoc,
    66  	}
    67  }
    68  
    69  func (c *ListCommand) SetFlags(f *gnuflag.FlagSet) {
    70  	defaultFormat := "tabular"
    71  	c.out.AddFlags(f, defaultFormat, map[string]cmd.Formatter{
    72  		"tabular": FormatTabular,
    73  		"yaml":    cmd.FormatYaml,
    74  		"json":    cmd.FormatJson,
    75  	})
    76  }
    77  
    78  func (c *ListCommand) Init(args []string) error {
    79  	c.patterns = args
    80  	return nil
    81  }
    82  
    83  func (c *ListCommand) Run(ctx *cmd.Context) error {
    84  	apiclient, err := c.newAPIClient(c)
    85  	if err != nil {
    86  		return errors.Trace(err)
    87  	}
    88  	defer apiclient.Close()
    89  
    90  	payloads, err := apiclient.ListFull(c.patterns...)
    91  	if err != nil {
    92  		if payloads == nil {
    93  			// List call completely failed; there is nothing to report.
    94  			return errors.Trace(err)
    95  		}
    96  		// Display any error, but continue to print info if some was returned.
    97  		fmt.Fprintf(ctx.Stderr, "%v\n", err)
    98  	}
    99  
   100  	// Note that we do not worry about c.CompatVersion for list-payloads...
   101  	formatter := newListFormatter(payloads)
   102  	formatted := formatter.format()
   103  	return c.out.Write(ctx, formatted)
   104  }