github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/storage/show.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package storage
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/errors"
     9  	"github.com/juju/names"
    10  	"launchpad.net/gnuflag"
    11  
    12  	"github.com/juju/juju/apiserver/params"
    13  )
    14  
    15  const ShowCommandDoc = `
    16  Show extended information about storage instances.
    17  Storage instances to display are specified by storage ids.
    18  
    19  * note use of positional arguments
    20  
    21  options:
    22  -e, --environment (= "")
    23     juju environment to operate in
    24  -o, --output (= "")
    25     specify an output file
    26  --format (= yaml)
    27     specify output format (json|yaml)
    28  [space separated storage ids]
    29  `
    30  
    31  // ShowCommand attempts to release storage instance.
    32  type ShowCommand struct {
    33  	StorageCommandBase
    34  	ids []string
    35  	out cmd.Output
    36  }
    37  
    38  // Init implements Command.Init.
    39  func (c *ShowCommand) Init(args []string) (err error) {
    40  	if len(args) < 1 {
    41  		return errors.New("must specify storage id(s)")
    42  	}
    43  	c.ids = args
    44  	return nil
    45  }
    46  
    47  // Info implements Command.Info.
    48  func (c *ShowCommand) Info() *cmd.Info {
    49  	return &cmd.Info{
    50  		Name:    "show",
    51  		Purpose: "shows storage instance",
    52  		Doc:     ShowCommandDoc,
    53  	}
    54  }
    55  
    56  // SetFlags implements Command.SetFlags.
    57  func (c *ShowCommand) SetFlags(f *gnuflag.FlagSet) {
    58  	c.StorageCommandBase.SetFlags(f)
    59  	c.out.AddFlags(f, "yaml", cmd.DefaultFormatters)
    60  }
    61  
    62  // Run implements Command.Run.
    63  func (c *ShowCommand) Run(ctx *cmd.Context) (err error) {
    64  	api, err := getStorageShowAPI(c)
    65  	if err != nil {
    66  		return err
    67  	}
    68  	defer api.Close()
    69  
    70  	tags, err := c.getStorageTags()
    71  	if err != nil {
    72  		return err
    73  	}
    74  	found, err := api.Show(tags)
    75  	if err != nil {
    76  		return err
    77  	}
    78  	output, err := formatStorageDetails(found)
    79  	if err != nil {
    80  		return err
    81  	}
    82  	return c.out.Write(ctx, output)
    83  }
    84  
    85  func (c *ShowCommand) getStorageTags() ([]names.StorageTag, error) {
    86  	tags := make([]names.StorageTag, len(c.ids))
    87  	for i, id := range c.ids {
    88  		if !names.IsValidStorage(id) {
    89  			return nil, errors.Errorf("invalid storage id %v", id)
    90  		}
    91  		tags[i] = names.NewStorageTag(id)
    92  	}
    93  	return tags, nil
    94  }
    95  
    96  var (
    97  	getStorageShowAPI = (*ShowCommand).getStorageShowAPI
    98  )
    99  
   100  // StorageAPI defines the API methods that the storage commands use.
   101  type StorageShowAPI interface {
   102  	Close() error
   103  	Show(tags []names.StorageTag) ([]params.StorageDetails, error)
   104  }
   105  
   106  func (c *ShowCommand) getStorageShowAPI() (StorageShowAPI, error) {
   107  	return c.NewStorageAPI()
   108  }