github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/storage/filesystemlist.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 FilesystemListCommandDoc = `
    16  List filesystems in the environment.
    17  
    18  options:
    19  -e, --environment (= "")
    20      juju environment to operate in
    21  -o, --output (= "")
    22      specify an output file
    23  [machine]
    24      machine ids for filtering the list
    25  
    26  `
    27  
    28  // FilesystemListCommand lists storage filesystems.
    29  type FilesystemListCommand struct {
    30  	FilesystemCommandBase
    31  	Ids []string
    32  	out cmd.Output
    33  }
    34  
    35  // Init implements Command.Init.
    36  func (c *FilesystemListCommand) Init(args []string) (err error) {
    37  	c.Ids = args
    38  	return nil
    39  }
    40  
    41  // Info implements Command.Info.
    42  func (c *FilesystemListCommand) Info() *cmd.Info {
    43  	return &cmd.Info{
    44  		Name:    "list",
    45  		Purpose: "list storage filesystems",
    46  		Doc:     FilesystemListCommandDoc,
    47  	}
    48  }
    49  
    50  // SetFlags implements Command.SetFlags.
    51  func (c *FilesystemListCommand) SetFlags(f *gnuflag.FlagSet) {
    52  	c.StorageCommandBase.SetFlags(f)
    53  
    54  	c.out.AddFlags(f, "tabular", map[string]cmd.Formatter{
    55  		"yaml":    cmd.FormatYaml,
    56  		"json":    cmd.FormatJson,
    57  		"tabular": formatFilesystemListTabular,
    58  	})
    59  }
    60  
    61  // Run implements Command.Run.
    62  func (c *FilesystemListCommand) Run(ctx *cmd.Context) (err error) {
    63  	api, err := getFilesystemListAPI(c)
    64  	if err != nil {
    65  		return err
    66  	}
    67  	defer api.Close()
    68  
    69  	found, err := api.ListFilesystems(c.Ids)
    70  	if err != nil {
    71  		return err
    72  	}
    73  	// filter out valid output, if any
    74  	var valid []params.FilesystemDetailsResult
    75  	for _, one := range found {
    76  		if one.Error == nil {
    77  			valid = append(valid, one)
    78  			continue
    79  		}
    80  		// display individual error
    81  		fmt.Fprintf(ctx.Stderr, "%v\n", one.Error)
    82  	}
    83  	if len(valid) == 0 {
    84  		return nil
    85  	}
    86  	info, err := convertToFilesystemInfo(valid)
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	var output interface{}
    92  	switch c.out.Name() {
    93  	case "json", "yaml":
    94  		output = map[string]map[string]FilesystemInfo{"filesystems": info}
    95  	default:
    96  		output = info
    97  	}
    98  	return c.out.Write(ctx, output)
    99  }
   100  
   101  var getFilesystemListAPI = (*FilesystemListCommand).getFilesystemListAPI
   102  
   103  // FilesystemListAPI defines the API methods that the filesystem list command use.
   104  type FilesystemListAPI interface {
   105  	Close() error
   106  	ListFilesystems(machines []string) ([]params.FilesystemDetailsResult, error)
   107  }
   108  
   109  func (c *FilesystemListCommand) getFilesystemListAPI() (FilesystemListAPI, error) {
   110  	return c.NewStorageAPI()
   111  }