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