forked from odeke-em/drive
/
about.go
158 lines (134 loc) · 3.82 KB
/
about.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
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package drive
import (
"fmt"
drive "github.com/google/google-api-go-client/drive/v2"
"github.com/odeke-em/log"
)
const Version = "0.2.9"
const (
Barely = iota
AlmostExceeded
HalfwayExceeded
Exceeded
Unknown
)
const (
AboutNone = 1 << iota
AboutQuota
AboutFileSizes
AboutFeatures
)
func (g *Commands) About(mask int) (err error) {
if mask == AboutNone {
return nil
}
about, err := g.rem.About()
if err != nil {
return err
}
printSummary(g.log, about, mask)
return nil
}
func quotaRequested(mask int) bool {
return (mask & AboutQuota) != 0
}
func fileSizesRequested(mask int) bool {
return (mask & AboutFileSizes) != 0
}
func featuresRequested(mask int) bool {
return (mask & AboutFeatures) != 0
}
func printSummary(logy *log.Logger, about *drive.About, mask int) {
if quotaRequested(mask) {
quotaInformation(logy, about)
}
if fileSizesRequested(mask) {
fileSizesInfo(logy, about)
}
if featuresRequested(mask) {
featuresInformation(logy, about)
}
}
func fileSizesInfo(logy *log.Logger, about *drive.About) {
if len(about.MaxUploadSizes) >= 1 {
logy.Logln("\n* Maximum upload sizes per file type *")
logy.Logf("%-50s %-20s\n", "FileType", "Size")
for _, uploadInfo := range about.MaxUploadSizes {
logy.Logf("%-50s %-20s\n", uploadInfo.Type, prettyBytes(uploadInfo.Size))
}
logy.Logln()
}
return
}
func featuresInformation(logy *log.Logger, about *drive.About) {
if len(about.Features) >= 1 {
logy.Logf("%-30s %-30s\n", "Feature", "Request limit (queries/second)")
for _, feature := range about.Features {
if feature.FeatureName == "" {
continue
}
logy.Logf("%-30s %-30f\n", feature.FeatureName, feature.FeatureRate)
}
logy.Logln()
}
}
func quotaInformation(logy *log.Logger, about *drive.About) {
freeBytes := about.QuotaBytesTotal - about.QuotaBytesUsed
logy.Logf(
"Name: %s\nAccount type:\t%s\nBytes Used:\t%-20d (%s)\n"+
"Bytes Free:\t%-20d (%s)\nBytes InTrash:\t%-20d (%s)\n"+
"Total Bytes:\t%-20d (%s)\n",
about.Name, about.QuotaType,
about.QuotaBytesUsed, prettyBytes(about.QuotaBytesUsed),
freeBytes, prettyBytes(freeBytes),
about.QuotaBytesUsedInTrash, prettyBytes(about.QuotaBytesUsedInTrash),
about.QuotaBytesTotal, prettyBytes(about.QuotaBytesTotal))
if len(about.QuotaBytesByService) >= 1 {
logy.Logln("\n* Space used by Google Services *")
logy.Logf("%-36s %-36s\n", "Service", "Bytes")
for _, quotaService := range about.QuotaBytesByService {
logy.Logf("%-36s %-36s\n", quotaService.ServiceName, prettyBytes(quotaService.BytesUsed))
}
logy.Logf("%-36s %-36s\n", "Space used by all Google Apps",
prettyBytes(about.QuotaBytesUsedAggregate))
}
logy.Logln()
}
func (g *Commands) QuotaStatus(query int64) (status int, err error) {
if query < 0 {
return Unknown, err
}
about, err := g.rem.About()
if err != nil {
return Unknown, err
}
// Sanity check
if about.QuotaBytesTotal < 1 {
return Unknown, fmt.Errorf("QuotaBytesTotal < 1")
}
toBeUsed := query + about.QuotaBytesUsed
if toBeUsed >= about.QuotaBytesTotal {
return Exceeded, nil
}
percentage := float64(toBeUsed) / float64(about.QuotaBytesTotal)
if percentage < 0.5 {
return Barely, nil
}
if percentage < 0.8 {
return HalfwayExceeded, nil
}
return AlmostExceeded, nil
}