~ubuntu-branches/ubuntu/vivid/juju-core/vivid-proposed

« back to all changes in this revision

Viewing changes to src/github.com/juju/govmomi/govc/host/esxcli/esxcli.go

  • Committer: Package Import Robot
  • Author(s): Curtis C. Hovey
  • Date: 2015-09-29 19:43:29 UTC
  • mfrom: (47.1.4 wily-proposed)
  • Revision ID: package-import@ubuntu.com-20150929194329-9y496tbic30hc7vp
Tags: 1.24.6-0ubuntu1~15.04.1
Backport of 1.24.6 from wily. (LP: #1500916, #1497087)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2014 VMware, Inc. All Rights Reserved.
 
3
 
 
4
Licensed under the Apache License, Version 2.0 (the "License");
 
5
you may not use this file except in compliance with the License.
 
6
You may obtain a copy of the License at
 
7
 
 
8
    http://www.apache.org/licenses/LICENSE-2.0
 
9
 
 
10
Unless required by applicable law or agreed to in writing, software
 
11
distributed under the License is distributed on an "AS IS" BASIS,
 
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
See the License for the specific language governing permissions and
 
14
limitations under the License.
 
15
*/
 
16
 
 
17
package esxcli
 
18
 
 
19
import (
 
20
        "flag"
 
21
        "fmt"
 
22
        "os"
 
23
        "sort"
 
24
        "strings"
 
25
        "text/tabwriter"
 
26
 
 
27
        "github.com/juju/govmomi/govc/cli"
 
28
        "github.com/juju/govmomi/govc/flags"
 
29
)
 
30
 
 
31
type esxcli struct {
 
32
        *flags.HostSystemFlag
 
33
}
 
34
 
 
35
func init() {
 
36
        cli.Register("host.esxcli", &esxcli{})
 
37
}
 
38
 
 
39
func (cmd *esxcli) Register(f *flag.FlagSet) {}
 
40
 
 
41
func (cmd *esxcli) Process() error { return nil }
 
42
 
 
43
func (cmd *esxcli) Run(f *flag.FlagSet) error {
 
44
        c, err := cmd.Client()
 
45
        if err != nil {
 
46
                return nil
 
47
        }
 
48
 
 
49
        host, err := cmd.HostSystem()
 
50
        if err != nil {
 
51
                return err
 
52
        }
 
53
 
 
54
        e, err := NewExecutor(c, host)
 
55
        if err != nil {
 
56
                return err
 
57
        }
 
58
 
 
59
        res, err := e.Run(f.Args())
 
60
        if err != nil {
 
61
                return err
 
62
        }
 
63
 
 
64
        if len(res.Values) == 0 {
 
65
                return nil
 
66
        }
 
67
 
 
68
        // TODO: OutputFlag / format options
 
69
        switch res.Info.Hints.Formatter() {
 
70
        case "table":
 
71
                cmd.formatTable(res)
 
72
        default:
 
73
                cmd.formatSimple(res)
 
74
        }
 
75
 
 
76
        return nil
 
77
}
 
78
 
 
79
func (cmd *esxcli) formatSimple(res *Response) {
 
80
        var keys []string
 
81
        for key := range res.Values[0] {
 
82
                keys = append(keys, key)
 
83
        }
 
84
        sort.Strings(keys)
 
85
 
 
86
        tw := tabwriter.NewWriter(os.Stdout, 2, 0, 2, ' ', 0)
 
87
 
 
88
        for i, rv := range res.Values {
 
89
                if i > 0 {
 
90
                        fmt.Fprintln(tw)
 
91
                        _ = tw.Flush()
 
92
                }
 
93
                for _, key := range keys {
 
94
                        fmt.Fprintf(tw, "%s:\t%s\n", key, strings.Join(rv[key], ", "))
 
95
                }
 
96
        }
 
97
 
 
98
        _ = tw.Flush()
 
99
}
 
100
 
 
101
func (cmd *esxcli) formatTable(res *Response) {
 
102
        fields := res.Info.Hints.Fields()
 
103
 
 
104
        tw := tabwriter.NewWriter(os.Stdout, len(fields), 0, 2, ' ', 0)
 
105
 
 
106
        var hr []string
 
107
        for _, name := range fields {
 
108
                hr = append(hr, strings.Repeat("-", len(name)))
 
109
        }
 
110
 
 
111
        fmt.Fprintln(tw, strings.Join(fields, "\t"))
 
112
        fmt.Fprintln(tw, strings.Join(hr, "\t"))
 
113
 
 
114
        for _, vals := range res.Values {
 
115
                var row []string
 
116
 
 
117
                for _, name := range fields {
 
118
                        key := strings.Replace(name, " ", "", -1)
 
119
                        if val, ok := vals[key]; ok {
 
120
                                row = append(row, strings.Join(val, ", "))
 
121
                        } else {
 
122
                                row = append(row, "")
 
123
                        }
 
124
                }
 
125
 
 
126
                fmt.Fprintln(tw, strings.Join(row, "\t"))
 
127
        }
 
128
 
 
129
        _ = tw.Flush()
 
130
}