~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/juju/storage/filesystemlist.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

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