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