-
Notifications
You must be signed in to change notification settings - Fork 6
/
pool_ls.go
67 lines (61 loc) · 1.58 KB
/
pool_ls.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
package omcmd
import (
"context"
"fmt"
"github.com/opensvc/om3/core/client"
"github.com/opensvc/om3/core/output"
"github.com/opensvc/om3/core/rawconfig"
"github.com/opensvc/om3/daemon/api"
"github.com/opensvc/om3/util/sizeconv"
"github.com/opensvc/om3/util/unstructured"
)
type (
CmdPoolLs struct {
OptsGlobal
Name string
}
)
func (t *CmdPoolLs) Run() error {
render := func(items api.PoolItems) {
lines := make(unstructured.List, len(items))
for i, item := range items {
u := item.Unstructured()
u["bin_free"] = sizeconv.BSizeCompact(float64(item.Free))
u["bin_used"] = sizeconv.BSizeCompact(float64(item.Used))
u["bin_size"] = sizeconv.BSizeCompact(float64(item.Size))
lines[i] = u
}
output.Renderer{
DefaultOutput: "tab=NAME:name,TYPE:type,CAPABILITIES:capabilities[*],HEAD:head,VOLUME_COUNT:volume_count,BIN_SIZE:bin_size,BIN_USED:bin_used,BIN_FREE:bin_free",
Output: t.Output,
Color: t.Color,
Data: lines,
Colorize: rawconfig.Colorize,
}.Print()
}
c, err := client.New(client.WithURL(t.Server))
if err != nil {
return err
}
params := api.GetPoolsParams{}
if t.Name != "" {
params.Name = &t.Name
}
resp, err := c.GetPoolsWithResponse(context.Background(), ¶ms)
if err != nil {
return err
}
switch resp.StatusCode() {
case 200:
render(resp.JSON200.Items)
case 401:
return fmt.Errorf("%s", resp.JSON401)
case 403:
return fmt.Errorf("%s", resp.JSON403)
case 500:
return fmt.Errorf("%s", resp.JSON500)
default:
return fmt.Errorf("unexpected status code: %d", resp.StatusCode())
}
return nil
}