forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
megabytes.go
48 lines (37 loc) · 820 Bytes
/
megabytes.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
package flag
import (
"strings"
"code.cloudfoundry.org/cli/types"
"code.cloudfoundry.org/bytefmt"
flags "github.com/jessevdk/go-flags"
)
const (
AllowedUnits = "mg"
)
type Megabytes struct {
types.NullUint64
}
func (m *Megabytes) UnmarshalFlag(val string) error {
if val == "" {
return nil
}
size, err := ConvertToMb(val)
if err != nil {
return err
}
m.Value = size
m.IsSet = true
return nil
}
func ConvertToMb(val string) (uint64, error) {
size, err := bytefmt.ToMegabytes(val)
if err != nil ||
!strings.ContainsAny(strings.ToLower(val), AllowedUnits) ||
strings.Contains(strings.ToLower(val), ".") {
return size, &flags.Error{
Type: flags.ErrRequired,
Message: `Byte quantity must be an integer with a unit of measurement like M, MB, G, or GB`,
}
}
return size, nil
}