github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/machine/base.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package machine
     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/apiserver/params"
    15  	"github.com/juju/juju/cmd/juju/status"
    16  	"github.com/juju/juju/cmd/modelcmd"
    17  )
    18  
    19  // statusAPI defines the API methods for the machines and show-machine commands.
    20  type statusAPI interface {
    21  	Status(pattern []string) (*params.FullStatus, error)
    22  	Close() error
    23  }
    24  
    25  type baseMachinesCommand struct {
    26  	modelcmd.ModelCommandBase
    27  	modelcmd.IAASOnlyCommand
    28  }
    29  
    30  // baseMachineCommand provides access to information about machines in a model.
    31  type baselistMachinesCommand struct {
    32  	baseMachinesCommand
    33  	out           cmd.Output
    34  	isoTime       bool
    35  	api           statusAPI
    36  	machineIds    []string
    37  	defaultFormat string
    38  	color         bool
    39  }
    40  
    41  // SetFlags sets utc and format flags based on user specified options.
    42  func (c *baselistMachinesCommand) SetFlags(f *gnuflag.FlagSet) {
    43  	c.baseMachinesCommand.SetFlags(f)
    44  	f.BoolVar(&c.isoTime, "utc", false, "Display time as UTC in RFC3339 format")
    45  	f.BoolVar(&c.color, "color", false, "Force use of ANSI color codes")
    46  	c.out.AddFlags(f, c.defaultFormat, map[string]cmd.Formatter{
    47  		"yaml":    cmd.FormatYaml,
    48  		"json":    cmd.FormatJson,
    49  		"tabular": c.tabular,
    50  	})
    51  }
    52  
    53  var newAPIClientForMachines = func(c *baselistMachinesCommand) (statusAPI, error) {
    54  	if c.api != nil {
    55  		return c.api, nil
    56  	}
    57  	return c.NewAPIClient()
    58  }
    59  
    60  // Run implements Command.Run for baseMachinesCommand.
    61  func (c *baselistMachinesCommand) Run(ctx *cmd.Context) error {
    62  	apiclient, err := newAPIClientForMachines(c)
    63  	if err != nil {
    64  		return errors.Trace(err)
    65  	}
    66  	defer apiclient.Close()
    67  
    68  	fullStatus, err := apiclient.Status(nil)
    69  	if err != nil {
    70  		if fullStatus == nil {
    71  			// Status call completely failed, there is nothing to report
    72  			return err
    73  		}
    74  		// Display any error, but continue to print status if some was returned
    75  		fmt.Fprintf(ctx.Stderr, "%v\n", err)
    76  	} else if fullStatus == nil {
    77  		return errors.Errorf("unable to obtain the current status")
    78  	}
    79  
    80  	formatter := status.NewStatusFormatter(fullStatus, c.isoTime)
    81  	formatted := formatter.MachineFormat(c.machineIds)
    82  	return c.out.Write(ctx, formatted)
    83  }
    84  
    85  func (c *baselistMachinesCommand) tabular(writer io.Writer, value interface{}) error {
    86  	return status.FormatMachineTabular(writer, c.color, value)
    87  }