storj/private/memory/sizes.go

43 lines
840 B
Go
Raw Normal View History

2019-01-24 20:15:10 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
2018-10-01 12:29:27 +01:00
// See LICENSE for copying information.
package memory
import "strings"
// Sizes implements flag.Value for collecting memory size
type Sizes struct {
Default []Size
Custom []Size
}
// Sizes returns the loaded values
func (sizes Sizes) Sizes() []Size {
if len(sizes.Custom) > 0 {
return sizes.Custom
}
return sizes.Default
}
// String converts values to a string
func (sizes Sizes) String() string {
sz := sizes.Sizes()
xs := make([]string, len(sz))
for i, size := range sz {
xs[i] = size.String()
}
return strings.Join(xs, " ")
}
// Set adds values from byte values
func (sizes *Sizes) Set(s string) error {
for _, x := range strings.Fields(s) {
var size Size
if err := size.Set(x); err != nil {
return err
}
sizes.Custom = append(sizes.Custom, size)
}
return nil
}