add functions/vendor files

This commit is contained in:
Reed Allman
2017-06-11 02:05:36 -07:00
parent 6ee9c1fa0a
commit f2c7aa5ee6
7294 changed files with 1629834 additions and 0 deletions

View File

@@ -0,0 +1,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"
}

View File

@@ -0,0 +1,43 @@
package flagext
import "testing"
import "github.com/stretchr/testify/assert"
func TestMarshalBytesize(t *testing.T) {
v, err := ByteSize(1024).MarshalFlag()
if assert.NoError(t, err) {
assert.Equal(t, "1.024kB", v)
}
}
func TestStringBytesize(t *testing.T) {
v := ByteSize(2048).String()
assert.Equal(t, "2.048kB", v)
}
func TestUnmarshalBytesize(t *testing.T) {
var b ByteSize
err := b.UnmarshalFlag("notASize")
assert.Error(t, err)
err = b.UnmarshalFlag("1MB")
if assert.NoError(t, err) {
assert.Equal(t, ByteSize(1000000), b)
}
}
func TestSetBytesize(t *testing.T) {
var b ByteSize
err := b.Set("notASize")
assert.Error(t, err)
err = b.Set("2MB")
if assert.NoError(t, err) {
assert.Equal(t, ByteSize(2000000), b)
}
}
func TestTypeBytesize(t *testing.T) {
var b ByteSize
assert.Equal(t, "byte-size", b.Type())
}