forked from go-openapi/runtime
/
byte_size.go
38 lines (31 loc) · 915 Bytes
/
byte_size.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
package flagext
import (
"github.com/docker/go-units"
)
// ByteSize used to pass byte sizes to a go-flags CLI
type ByteSize int
// MarshalFlag implements go-flags Marshaller interface
func (b ByteSize) MarshalFlag() (string, error) {
return units.HumanSize(float64(b)), nil
}
// UnmarshalFlag implements go-flags Unmarshaller interface
func (b *ByteSize) UnmarshalFlag(value string) error {
sz, err := units.FromHumanSize(value)
if err != nil {
return err
}
*b = ByteSize(int(sz))
return nil
}
// String method for a bytesize (pflag value and stringer interface)
func (b ByteSize) String() string {
return units.HumanSize(float64(b))
}
// Set the value of this bytesize (pflag value interfaces)
func (b *ByteSize) Set(value string) error {
return b.UnmarshalFlag(value)
}
// Type returns the type of the pflag value (pflag value interface)
func (b *ByteSize) Type() string {
return "byte-size"
}