github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/uniter/runner/jujuc/storage-list.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package jujuc
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/errors"
     9  	"github.com/juju/gnuflag"
    10  	"gopkg.in/juju/names.v2"
    11  )
    12  
    13  // StorageListCommand implements the storage-list command.
    14  //
    15  // StorageListCommand implements cmd.Command.
    16  type StorageListCommand struct {
    17  	cmd.CommandBase
    18  	ctx         Context
    19  	out         cmd.Output
    20  	storageName string
    21  }
    22  
    23  func NewStorageListCommand(ctx Context) (cmd.Command, error) {
    24  	return &StorageListCommand{ctx: ctx}, nil
    25  }
    26  
    27  func (c *StorageListCommand) Info() *cmd.Info {
    28  	doc := `
    29  storage-list will list the names of all storage instances
    30  attached to the unit. These names can be passed to storage-get
    31  via the "-s" flag to query the storage attributes.
    32  
    33  A storage name may be specified, in which case only storage
    34  instances for that named storage will be returned.
    35  `
    36  	return &cmd.Info{
    37  		Name:    "storage-list",
    38  		Args:    "[<storage-name>]",
    39  		Purpose: "list storage attached to the unit",
    40  		Doc:     doc,
    41  	}
    42  }
    43  
    44  func (c *StorageListCommand) SetFlags(f *gnuflag.FlagSet) {
    45  	c.out.AddFlags(f, "smart", cmd.DefaultFormatters)
    46  }
    47  
    48  func (c *StorageListCommand) Init(args []string) (err error) {
    49  	storageName, err := cmd.ZeroOrOneArgs(args)
    50  	if err != nil {
    51  		return err
    52  	}
    53  	c.storageName = storageName
    54  	return nil
    55  }
    56  
    57  func (c *StorageListCommand) Run(ctx *cmd.Context) error {
    58  	tags, err := c.ctx.StorageTags()
    59  	if err != nil {
    60  		return errors.Trace(err)
    61  	}
    62  	ids := make([]string, 0, len(tags))
    63  	for _, tag := range tags {
    64  		id := tag.Id()
    65  		if c.storageName != "" {
    66  			storageName, err := names.StorageName(id)
    67  			if err != nil {
    68  				return errors.Trace(err)
    69  			}
    70  			if storageName != c.storageName {
    71  				continue
    72  			}
    73  		}
    74  		ids = append(ids, id)
    75  	}
    76  	return c.out.Write(ctx, ids)
    77  }