github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/storage/list.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  	"fmt"
     8  
     9  	"github.com/juju/cmd"
    10  	"launchpad.net/gnuflag"
    11  
    12  	"github.com/juju/juju/apiserver/params"
    13  )
    14  
    15  const ListCommandDoc = `
    16  List information about storage instances.
    17  
    18  options:
    19  -e, --environment (= "")
    20     juju environment to operate in
    21  -o, --output (= "")
    22     specify an output file
    23  --format (= tabular)
    24     specify output format (json|tabular|yaml)
    25  `
    26  
    27  // ListCommand returns storage instances.
    28  type ListCommand struct {
    29  	StorageCommandBase
    30  	out cmd.Output
    31  }
    32  
    33  // Init implements Command.Init.
    34  func (c *ListCommand) Init(args []string) (err error) {
    35  	return cmd.CheckEmpty(args)
    36  }
    37  
    38  // Info implements Command.Info.
    39  func (c *ListCommand) Info() *cmd.Info {
    40  	return &cmd.Info{
    41  		Name:    "list",
    42  		Purpose: "lists storage",
    43  		Doc:     ListCommandDoc,
    44  	}
    45  }
    46  
    47  // SetFlags implements Command.SetFlags.
    48  func (c *ListCommand) SetFlags(f *gnuflag.FlagSet) {
    49  	c.StorageCommandBase.SetFlags(f)
    50  	c.out.AddFlags(f, "tabular", map[string]cmd.Formatter{
    51  		"yaml":    cmd.FormatYaml,
    52  		"json":    cmd.FormatJson,
    53  		"tabular": formatListTabular,
    54  	})
    55  }
    56  
    57  // Run implements Command.Run.
    58  func (c *ListCommand) Run(ctx *cmd.Context) (err error) {
    59  	api, err := getStorageListAPI(c)
    60  	if err != nil {
    61  		return err
    62  	}
    63  	defer api.Close()
    64  
    65  	found, err := api.List()
    66  	if err != nil {
    67  		return err
    68  	}
    69  	// filter out valid output, if any
    70  	var valid []params.StorageDetails
    71  	for _, one := range found {
    72  		if one.Error != nil {
    73  			fmt.Fprintf(ctx.Stderr, "%v\n", one.Error)
    74  			continue
    75  		}
    76  		if one.Result != nil {
    77  			valid = append(valid, *one.Result)
    78  		} else {
    79  			details := storageDetailsFromLegacy(one.Legacy)
    80  			valid = append(valid, details)
    81  		}
    82  	}
    83  	if len(valid) == 0 {
    84  		return nil
    85  	}
    86  	details, err := formatStorageDetails(valid)
    87  	if err != nil {
    88  		return err
    89  	}
    90  	var output interface{}
    91  	switch c.out.Name() {
    92  	case "yaml", "json":
    93  		output = map[string]map[string]StorageInfo{"storage": details}
    94  	default:
    95  		output = details
    96  	}
    97  	return c.out.Write(ctx, output)
    98  }
    99  
   100  var (
   101  	getStorageListAPI = (*ListCommand).getStorageListAPI
   102  )
   103  
   104  // StorageAPI defines the API methods that the storage commands use.
   105  type StorageListAPI interface {
   106  	Close() error
   107  	List() ([]params.StorageDetailsResult, error)
   108  }
   109  
   110  func (c *ListCommand) getStorageListAPI() (StorageListAPI, error) {
   111  	return c.NewStorageAPI()
   112  }