github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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  	"github.com/juju/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 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:    "payloads",
    63  		Args:    "[pattern ...]",
    64  		Purpose: "display status information about known payloads",
    65  		Doc:     listDoc,
    66  		Aliases: []string{"list-payloads"},
    67  	}
    68  }
    69  
    70  func (c *ListCommand) SetFlags(f *gnuflag.FlagSet) {
    71  	c.ModelCommandBase.SetFlags(f)
    72  	defaultFormat := "tabular"
    73  	c.out.AddFlags(f, defaultFormat, map[string]cmd.Formatter{
    74  		"tabular": FormatTabular,
    75  		"yaml":    cmd.FormatYaml,
    76  		"json":    cmd.FormatJson,
    77  	})
    78  }
    79  
    80  func (c *ListCommand) Init(args []string) error {
    81  	c.patterns = args
    82  	return nil
    83  }
    84  
    85  func (c *ListCommand) Run(ctx *cmd.Context) error {
    86  	apiclient, err := c.newAPIClient(c)
    87  	if err != nil {
    88  		return errors.Trace(err)
    89  	}
    90  	defer apiclient.Close()
    91  
    92  	payloads, err := apiclient.ListFull(c.patterns...)
    93  	if err != nil {
    94  		if payloads == nil {
    95  			// List call completely failed; there is nothing to report.
    96  			return errors.Trace(err)
    97  		}
    98  		// Display any error, but continue to print info if some was returned.
    99  		fmt.Fprintf(ctx.Stderr, "%v\n", err)
   100  	}
   101  
   102  	if len(payloads) == 0 {
   103  		ctx.Infof("No payloads to display.")
   104  		return nil
   105  	}
   106  
   107  	// Note that we do not worry about c.CompatVersion for payloads...
   108  	formatter := newListFormatter(payloads)
   109  	formatted := formatter.format()
   110  	return c.out.Write(ctx, formatted)
   111  }