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