forked from go-openapi/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
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.024 kB", 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) | ||
} | ||
} |