-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cmd_simplefs_quota.go
170 lines (151 loc) · 3.84 KB
/
cmd_simplefs_quota.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2018 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"encoding/json"
"fmt"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
"golang.org/x/net/context"
)
// CmdSimpleFSQuota is the 'fs quota' command.
type CmdSimpleFSQuota struct {
libkb.Contextified
git bool
bytes bool
archived bool
json bool
teamName keybase1.TeamName
}
// NewCmdSimpleFSQuota creates a new cli.Command.
func NewCmdSimpleFSQuota(
cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "quota",
Usage: "show quota usage for logged-in user or a team",
Action: func(c *cli.Context) {
cl.ChooseCommand(
&CmdSimpleFSQuota{Contextified: libkb.NewContextified(g)},
"quota", c)
cl.SetNoStandalone()
},
Flags: []cli.Flag{
cli.BoolFlag{
Name: "git",
Usage: "show git usage",
},
cli.BoolFlag{
Name: "bytes",
Usage: "show all data in units of bytes",
},
cli.BoolFlag{
Name: "archived",
Usage: "show archived usage",
},
cli.BoolFlag{
Name: "json",
Usage: "show output in json format",
},
cli.StringFlag{
Name: "team",
Usage: "print quota usage for a team, instead of the logged-in user",
},
},
}
}
// Run runs the command in client/server mode.
func (c *CmdSimpleFSQuota) Run() error {
cli, err := GetSimpleFSClient(c.G())
if err != nil {
return err
}
var usage keybase1.SimpleFSQuotaUsage
if c.teamName.Depth() == 0 {
usage, err = cli.SimpleFSGetUserQuotaUsage(context.TODO())
} else {
usage, err = cli.SimpleFSGetTeamQuotaUsage(context.TODO(), c.teamName)
}
if err != nil {
return err
}
return c.output(usage)
}
func humanizeBytes(n int64, bytesOnly bool) string {
const kb = 1024
const kbf = float64(kb)
const mb = kb * 1024
const mbf = float64(mb)
const gb = mb * 1024
const gbf = float64(gb)
if bytesOnly || n < kb {
return fmt.Sprintf("%d bytes", n)
} else if n < mb {
return fmt.Sprintf("%.2f KB", float64(n)/kbf)
} else if n < gb {
return fmt.Sprintf("%.2f MB", float64(n)/mbf)
}
return fmt.Sprintf("%.2f GB", float64(n)/gbf)
}
func (c *CmdSimpleFSQuota) humanizeBytes(n int64) string {
return humanizeBytes(n, c.bytes)
}
type simpleFSQuotaStruct struct {
UsageBytes int64
ArchivedBytes int64 `json:",omitempty"`
QuotaBytes int64
}
func (c *CmdSimpleFSQuota) output(usage keybase1.SimpleFSQuotaUsage) error {
ui := c.G().UI.GetTerminalUI()
usageBytes, archiveBytes, limitBytes :=
usage.UsageBytes, usage.ArchiveBytes, usage.LimitBytes
if c.git {
usageBytes, archiveBytes, limitBytes =
usage.GitUsageBytes, usage.GitArchiveBytes, usage.GitLimitBytes
}
if c.json {
data := simpleFSQuotaStruct{
UsageBytes: usageBytes,
QuotaBytes: limitBytes,
}
if c.archived {
data.ArchivedBytes = archiveBytes
}
output, err := json.Marshal(data)
if err != nil {
return err
}
ui.Printf("%s\n", output)
return nil
}
ui.Printf("Usage:\t\t%s\n", c.humanizeBytes(usageBytes))
if c.archived {
ui.Printf("Archived:\t%s\n", c.humanizeBytes(archiveBytes))
}
ui.Printf("Quota:\t\t%s\n", c.humanizeBytes(limitBytes))
return nil
}
// ParseArgv gets the optional -r switch
func (c *CmdSimpleFSQuota) ParseArgv(ctx *cli.Context) error {
c.git = ctx.Bool("git")
c.bytes = ctx.Bool("bytes")
c.archived = ctx.Bool("archived")
c.json = ctx.Bool("json")
if len(ctx.String("team")) > 0 {
teamName, err := keybase1.TeamNameFromString(ctx.String("team"))
if err != nil {
return err
}
c.teamName = teamName
}
return nil
}
// GetUsage says what this command needs to operate.
func (c *CmdSimpleFSQuota) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
KbKeyring: true,
API: true,
}
}