Signed-off-by: David Calavera <david.calavera@gmail.com>
| ... | ... |
@@ -28,11 +28,11 @@ import ( |
| 28 | 28 |
"github.com/docker/docker/pkg/progress" |
| 29 | 29 |
"github.com/docker/docker/pkg/streamformatter" |
| 30 | 30 |
"github.com/docker/docker/pkg/ulimit" |
| 31 |
- "github.com/docker/docker/pkg/units" |
|
| 32 | 31 |
"github.com/docker/docker/pkg/urlutil" |
| 33 | 32 |
"github.com/docker/docker/registry" |
| 34 | 33 |
tagpkg "github.com/docker/docker/tag" |
| 35 | 34 |
"github.com/docker/docker/utils" |
| 35 |
+ "github.com/docker/go-units" |
|
| 36 | 36 |
) |
| 37 | 37 |
|
| 38 | 38 |
// CmdBuild builds a new image from the source code at a given path. |
| ... | ... |
@@ -11,7 +11,7 @@ import ( |
| 11 | 11 |
flag "github.com/docker/docker/pkg/mflag" |
| 12 | 12 |
"github.com/docker/docker/pkg/stringid" |
| 13 | 13 |
"github.com/docker/docker/pkg/stringutils" |
| 14 |
- "github.com/docker/docker/pkg/units" |
|
| 14 |
+ "github.com/docker/go-units" |
|
| 15 | 15 |
) |
| 16 | 16 |
|
| 17 | 17 |
// CmdHistory shows the history of an image. |
| ... | ... |
@@ -13,7 +13,7 @@ import ( |
| 13 | 13 |
flag "github.com/docker/docker/pkg/mflag" |
| 14 | 14 |
"github.com/docker/docker/pkg/parsers/filters" |
| 15 | 15 |
"github.com/docker/docker/pkg/stringid" |
| 16 |
- "github.com/docker/docker/pkg/units" |
|
| 16 |
+ "github.com/docker/go-units" |
|
| 17 | 17 |
) |
| 18 | 18 |
|
| 19 | 19 |
// CmdImages lists the images in a specified repository, or all top-level images if no repository is specified. |
| ... | ... |
@@ -26,7 +26,7 @@ import ( |
| 26 | 26 |
"github.com/docker/docker/pkg/idtools" |
| 27 | 27 |
"github.com/docker/docker/pkg/mount" |
| 28 | 28 |
"github.com/docker/docker/pkg/parsers" |
| 29 |
- "github.com/docker/docker/pkg/units" |
|
| 29 |
+ "github.com/docker/go-units" |
|
| 30 | 30 |
|
| 31 | 31 |
"github.com/opencontainers/runc/libcontainer/label" |
| 32 | 32 |
) |
| ... | ... |
@@ -14,7 +14,7 @@ import ( |
| 14 | 14 |
"github.com/docker/docker/daemon/logger" |
| 15 | 15 |
"github.com/docker/docker/daemon/logger/loggerutils" |
| 16 | 16 |
"github.com/docker/docker/pkg/jsonlog" |
| 17 |
- "github.com/docker/docker/pkg/units" |
|
| 17 |
+ "github.com/docker/go-units" |
|
| 18 | 18 |
) |
| 19 | 19 |
|
| 20 | 20 |
// Name is the name of the file that the jsonlogger logs to. |
| 13 | 13 |
deleted file mode 100644 |
| ... | ... |
@@ -1,33 +0,0 @@ |
| 1 |
-// Package units provides helper function to parse and print size and time units |
|
| 2 |
-// in human-readable format. |
|
| 3 |
-package units |
|
| 4 |
- |
|
| 5 |
-import ( |
|
| 6 |
- "fmt" |
|
| 7 |
- "time" |
|
| 8 |
-) |
|
| 9 |
- |
|
| 10 |
-// HumanDuration returns a human-readable approximation of a duration |
|
| 11 |
-// (eg. "About a minute", "4 hours ago", etc.). |
|
| 12 |
-func HumanDuration(d time.Duration) string {
|
|
| 13 |
- if seconds := int(d.Seconds()); seconds < 1 {
|
|
| 14 |
- return "Less than a second" |
|
| 15 |
- } else if seconds < 60 {
|
|
| 16 |
- return fmt.Sprintf("%d seconds", seconds)
|
|
| 17 |
- } else if minutes := int(d.Minutes()); minutes == 1 {
|
|
| 18 |
- return "About a minute" |
|
| 19 |
- } else if minutes < 60 {
|
|
| 20 |
- return fmt.Sprintf("%d minutes", minutes)
|
|
| 21 |
- } else if hours := int(d.Hours()); hours == 1 {
|
|
| 22 |
- return "About an hour" |
|
| 23 |
- } else if hours < 48 {
|
|
| 24 |
- return fmt.Sprintf("%d hours", hours)
|
|
| 25 |
- } else if hours < 24*7*2 {
|
|
| 26 |
- return fmt.Sprintf("%d days", hours/24)
|
|
| 27 |
- } else if hours < 24*30*3 {
|
|
| 28 |
- return fmt.Sprintf("%d weeks", hours/24/7)
|
|
| 29 |
- } else if hours < 24*365*2 {
|
|
| 30 |
- return fmt.Sprintf("%d months", hours/24/30)
|
|
| 31 |
- } |
|
| 32 |
- return fmt.Sprintf("%d years", int(d.Hours())/24/365)
|
|
| 33 |
-} |
| 34 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,46 +0,0 @@ |
| 1 |
-package units |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "testing" |
|
| 5 |
- "time" |
|
| 6 |
-) |
|
| 7 |
- |
|
| 8 |
-func TestHumanDuration(t *testing.T) {
|
|
| 9 |
- // Useful duration abstractions |
|
| 10 |
- day := 24 * time.Hour |
|
| 11 |
- week := 7 * day |
|
| 12 |
- month := 30 * day |
|
| 13 |
- year := 365 * day |
|
| 14 |
- |
|
| 15 |
- assertEquals(t, "Less than a second", HumanDuration(450*time.Millisecond)) |
|
| 16 |
- assertEquals(t, "47 seconds", HumanDuration(47*time.Second)) |
|
| 17 |
- assertEquals(t, "About a minute", HumanDuration(1*time.Minute)) |
|
| 18 |
- assertEquals(t, "3 minutes", HumanDuration(3*time.Minute)) |
|
| 19 |
- assertEquals(t, "35 minutes", HumanDuration(35*time.Minute)) |
|
| 20 |
- assertEquals(t, "35 minutes", HumanDuration(35*time.Minute+40*time.Second)) |
|
| 21 |
- assertEquals(t, "About an hour", HumanDuration(1*time.Hour)) |
|
| 22 |
- assertEquals(t, "About an hour", HumanDuration(1*time.Hour+45*time.Minute)) |
|
| 23 |
- assertEquals(t, "3 hours", HumanDuration(3*time.Hour)) |
|
| 24 |
- assertEquals(t, "3 hours", HumanDuration(3*time.Hour+59*time.Minute)) |
|
| 25 |
- assertEquals(t, "4 hours", HumanDuration(3*time.Hour+60*time.Minute)) |
|
| 26 |
- assertEquals(t, "24 hours", HumanDuration(24*time.Hour)) |
|
| 27 |
- assertEquals(t, "36 hours", HumanDuration(1*day+12*time.Hour)) |
|
| 28 |
- assertEquals(t, "2 days", HumanDuration(2*day)) |
|
| 29 |
- assertEquals(t, "7 days", HumanDuration(7*day)) |
|
| 30 |
- assertEquals(t, "13 days", HumanDuration(13*day+5*time.Hour)) |
|
| 31 |
- assertEquals(t, "2 weeks", HumanDuration(2*week)) |
|
| 32 |
- assertEquals(t, "2 weeks", HumanDuration(2*week+4*day)) |
|
| 33 |
- assertEquals(t, "3 weeks", HumanDuration(3*week)) |
|
| 34 |
- assertEquals(t, "4 weeks", HumanDuration(4*week)) |
|
| 35 |
- assertEquals(t, "4 weeks", HumanDuration(4*week+3*day)) |
|
| 36 |
- assertEquals(t, "4 weeks", HumanDuration(1*month)) |
|
| 37 |
- assertEquals(t, "6 weeks", HumanDuration(1*month+2*week)) |
|
| 38 |
- assertEquals(t, "8 weeks", HumanDuration(2*month)) |
|
| 39 |
- assertEquals(t, "3 months", HumanDuration(3*month+1*week)) |
|
| 40 |
- assertEquals(t, "5 months", HumanDuration(5*month+2*week)) |
|
| 41 |
- assertEquals(t, "13 months", HumanDuration(13*month)) |
|
| 42 |
- assertEquals(t, "23 months", HumanDuration(23*month)) |
|
| 43 |
- assertEquals(t, "24 months", HumanDuration(24*month)) |
|
| 44 |
- assertEquals(t, "2 years", HumanDuration(24*month+2*week)) |
|
| 45 |
- assertEquals(t, "3 years", HumanDuration(3*year+2*month)) |
|
| 46 |
-} |
| 47 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,95 +0,0 @@ |
| 1 |
-package units |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "fmt" |
|
| 5 |
- "regexp" |
|
| 6 |
- "strconv" |
|
| 7 |
- "strings" |
|
| 8 |
-) |
|
| 9 |
- |
|
| 10 |
-// See: http://en.wikipedia.org/wiki/Binary_prefix |
|
| 11 |
-const ( |
|
| 12 |
- // Decimal |
|
| 13 |
- |
|
| 14 |
- KB = 1000 |
|
| 15 |
- MB = 1000 * KB |
|
| 16 |
- GB = 1000 * MB |
|
| 17 |
- TB = 1000 * GB |
|
| 18 |
- PB = 1000 * TB |
|
| 19 |
- |
|
| 20 |
- // Binary |
|
| 21 |
- |
|
| 22 |
- KiB = 1024 |
|
| 23 |
- MiB = 1024 * KiB |
|
| 24 |
- GiB = 1024 * MiB |
|
| 25 |
- TiB = 1024 * GiB |
|
| 26 |
- PiB = 1024 * TiB |
|
| 27 |
-) |
|
| 28 |
- |
|
| 29 |
-type unitMap map[string]int64 |
|
| 30 |
- |
|
| 31 |
-var ( |
|
| 32 |
- decimalMap = unitMap{"k": KB, "m": MB, "g": GB, "t": TB, "p": PB}
|
|
| 33 |
- binaryMap = unitMap{"k": KiB, "m": MiB, "g": GiB, "t": TiB, "p": PiB}
|
|
| 34 |
- sizeRegex = regexp.MustCompile(`^(\d+)([kKmMgGtTpP])?[bB]?$`) |
|
| 35 |
-) |
|
| 36 |
- |
|
| 37 |
-var decimapAbbrs = []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
|
|
| 38 |
-var binaryAbbrs = []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}
|
|
| 39 |
- |
|
| 40 |
-// CustomSize returns a human-readable approximation of a size |
|
| 41 |
-// using custom format. |
|
| 42 |
-func CustomSize(format string, size float64, base float64, _map []string) string {
|
|
| 43 |
- i := 0 |
|
| 44 |
- for size >= base {
|
|
| 45 |
- size = size / base |
|
| 46 |
- i++ |
|
| 47 |
- } |
|
| 48 |
- return fmt.Sprintf(format, size, _map[i]) |
|
| 49 |
-} |
|
| 50 |
- |
|
| 51 |
-// HumanSize returns a human-readable approximation of a size |
|
| 52 |
-// capped at 4 valid numbers (eg. "2.746 MB", "796 KB"). |
|
| 53 |
-func HumanSize(size float64) string {
|
|
| 54 |
- return CustomSize("%.4g %s", size, 1000.0, decimapAbbrs)
|
|
| 55 |
-} |
|
| 56 |
- |
|
| 57 |
-// BytesSize returns a human-readable size in bytes, kibibytes, |
|
| 58 |
-// mebibytes, gibibytes, or tebibytes (eg. "44kiB", "17MiB"). |
|
| 59 |
-func BytesSize(size float64) string {
|
|
| 60 |
- return CustomSize("%.4g %s", size, 1024.0, binaryAbbrs)
|
|
| 61 |
-} |
|
| 62 |
- |
|
| 63 |
-// FromHumanSize returns an integer from a human-readable specification of a |
|
| 64 |
-// size using SI standard (eg. "44kB", "17MB"). |
|
| 65 |
-func FromHumanSize(size string) (int64, error) {
|
|
| 66 |
- return parseSize(size, decimalMap) |
|
| 67 |
-} |
|
| 68 |
- |
|
| 69 |
-// RAMInBytes parses a human-readable string representing an amount of RAM |
|
| 70 |
-// in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and |
|
| 71 |
-// returns the number of bytes, or -1 if the string is unparseable. |
|
| 72 |
-// Units are case-insensitive, and the 'b' suffix is optional. |
|
| 73 |
-func RAMInBytes(size string) (int64, error) {
|
|
| 74 |
- return parseSize(size, binaryMap) |
|
| 75 |
-} |
|
| 76 |
- |
|
| 77 |
-// Parses the human-readable size string into the amount it represents. |
|
| 78 |
-func parseSize(sizeStr string, uMap unitMap) (int64, error) {
|
|
| 79 |
- matches := sizeRegex.FindStringSubmatch(sizeStr) |
|
| 80 |
- if len(matches) != 3 {
|
|
| 81 |
- return -1, fmt.Errorf("invalid size: '%s'", sizeStr)
|
|
| 82 |
- } |
|
| 83 |
- |
|
| 84 |
- size, err := strconv.ParseInt(matches[1], 10, 0) |
|
| 85 |
- if err != nil {
|
|
| 86 |
- return -1, err |
|
| 87 |
- } |
|
| 88 |
- |
|
| 89 |
- unitPrefix := strings.ToLower(matches[2]) |
|
| 90 |
- if mul, ok := uMap[unitPrefix]; ok {
|
|
| 91 |
- size *= mul |
|
| 92 |
- } |
|
| 93 |
- |
|
| 94 |
- return size, nil |
|
| 95 |
-} |
| 96 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,108 +0,0 @@ |
| 1 |
-package units |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "reflect" |
|
| 5 |
- "runtime" |
|
| 6 |
- "strings" |
|
| 7 |
- "testing" |
|
| 8 |
-) |
|
| 9 |
- |
|
| 10 |
-func TestBytesSize(t *testing.T) {
|
|
| 11 |
- assertEquals(t, "1 KiB", BytesSize(1024)) |
|
| 12 |
- assertEquals(t, "1 MiB", BytesSize(1024*1024)) |
|
| 13 |
- assertEquals(t, "1 MiB", BytesSize(1048576)) |
|
| 14 |
- assertEquals(t, "2 MiB", BytesSize(2*MiB)) |
|
| 15 |
- assertEquals(t, "3.42 GiB", BytesSize(3.42*GiB)) |
|
| 16 |
- assertEquals(t, "5.372 TiB", BytesSize(5.372*TiB)) |
|
| 17 |
- assertEquals(t, "2.22 PiB", BytesSize(2.22*PiB)) |
|
| 18 |
-} |
|
| 19 |
- |
|
| 20 |
-func TestHumanSize(t *testing.T) {
|
|
| 21 |
- assertEquals(t, "1 kB", HumanSize(1000)) |
|
| 22 |
- assertEquals(t, "1.024 kB", HumanSize(1024)) |
|
| 23 |
- assertEquals(t, "1 MB", HumanSize(1000000)) |
|
| 24 |
- assertEquals(t, "1.049 MB", HumanSize(1048576)) |
|
| 25 |
- assertEquals(t, "2 MB", HumanSize(2*MB)) |
|
| 26 |
- assertEquals(t, "3.42 GB", HumanSize(float64(3.42*GB))) |
|
| 27 |
- assertEquals(t, "5.372 TB", HumanSize(float64(5.372*TB))) |
|
| 28 |
- assertEquals(t, "2.22 PB", HumanSize(float64(2.22*PB))) |
|
| 29 |
-} |
|
| 30 |
- |
|
| 31 |
-func TestFromHumanSize(t *testing.T) {
|
|
| 32 |
- assertSuccessEquals(t, 32, FromHumanSize, "32") |
|
| 33 |
- assertSuccessEquals(t, 32, FromHumanSize, "32b") |
|
| 34 |
- assertSuccessEquals(t, 32, FromHumanSize, "32B") |
|
| 35 |
- assertSuccessEquals(t, 32*KB, FromHumanSize, "32k") |
|
| 36 |
- assertSuccessEquals(t, 32*KB, FromHumanSize, "32K") |
|
| 37 |
- assertSuccessEquals(t, 32*KB, FromHumanSize, "32kb") |
|
| 38 |
- assertSuccessEquals(t, 32*KB, FromHumanSize, "32Kb") |
|
| 39 |
- assertSuccessEquals(t, 32*MB, FromHumanSize, "32Mb") |
|
| 40 |
- assertSuccessEquals(t, 32*GB, FromHumanSize, "32Gb") |
|
| 41 |
- assertSuccessEquals(t, 32*TB, FromHumanSize, "32Tb") |
|
| 42 |
- assertSuccessEquals(t, 32*PB, FromHumanSize, "32Pb") |
|
| 43 |
- |
|
| 44 |
- assertError(t, FromHumanSize, "") |
|
| 45 |
- assertError(t, FromHumanSize, "hello") |
|
| 46 |
- assertError(t, FromHumanSize, "-32") |
|
| 47 |
- assertError(t, FromHumanSize, "32.3") |
|
| 48 |
- assertError(t, FromHumanSize, " 32 ") |
|
| 49 |
- assertError(t, FromHumanSize, "32.3Kb") |
|
| 50 |
- assertError(t, FromHumanSize, "32 mb") |
|
| 51 |
- assertError(t, FromHumanSize, "32m b") |
|
| 52 |
- assertError(t, FromHumanSize, "32bm") |
|
| 53 |
-} |
|
| 54 |
- |
|
| 55 |
-func TestRAMInBytes(t *testing.T) {
|
|
| 56 |
- assertSuccessEquals(t, 32, RAMInBytes, "32") |
|
| 57 |
- assertSuccessEquals(t, 32, RAMInBytes, "32b") |
|
| 58 |
- assertSuccessEquals(t, 32, RAMInBytes, "32B") |
|
| 59 |
- assertSuccessEquals(t, 32*KiB, RAMInBytes, "32k") |
|
| 60 |
- assertSuccessEquals(t, 32*KiB, RAMInBytes, "32K") |
|
| 61 |
- assertSuccessEquals(t, 32*KiB, RAMInBytes, "32kb") |
|
| 62 |
- assertSuccessEquals(t, 32*KiB, RAMInBytes, "32Kb") |
|
| 63 |
- assertSuccessEquals(t, 32*MiB, RAMInBytes, "32Mb") |
|
| 64 |
- assertSuccessEquals(t, 32*GiB, RAMInBytes, "32Gb") |
|
| 65 |
- assertSuccessEquals(t, 32*TiB, RAMInBytes, "32Tb") |
|
| 66 |
- assertSuccessEquals(t, 32*PiB, RAMInBytes, "32Pb") |
|
| 67 |
- assertSuccessEquals(t, 32*PiB, RAMInBytes, "32PB") |
|
| 68 |
- assertSuccessEquals(t, 32*PiB, RAMInBytes, "32P") |
|
| 69 |
- |
|
| 70 |
- assertError(t, RAMInBytes, "") |
|
| 71 |
- assertError(t, RAMInBytes, "hello") |
|
| 72 |
- assertError(t, RAMInBytes, "-32") |
|
| 73 |
- assertError(t, RAMInBytes, "32.3") |
|
| 74 |
- assertError(t, RAMInBytes, " 32 ") |
|
| 75 |
- assertError(t, RAMInBytes, "32.3Kb") |
|
| 76 |
- assertError(t, RAMInBytes, "32 mb") |
|
| 77 |
- assertError(t, RAMInBytes, "32m b") |
|
| 78 |
- assertError(t, RAMInBytes, "32bm") |
|
| 79 |
-} |
|
| 80 |
- |
|
| 81 |
-func assertEquals(t *testing.T, expected, actual interface{}) {
|
|
| 82 |
- if expected != actual {
|
|
| 83 |
- t.Errorf("Expected '%v' but got '%v'", expected, actual)
|
|
| 84 |
- } |
|
| 85 |
-} |
|
| 86 |
- |
|
| 87 |
-// func that maps to the parse function signatures as testing abstraction |
|
| 88 |
-type parseFn func(string) (int64, error) |
|
| 89 |
- |
|
| 90 |
-// Define 'String()' for pretty-print |
|
| 91 |
-func (fn parseFn) String() string {
|
|
| 92 |
- fnName := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name() |
|
| 93 |
- return fnName[strings.LastIndex(fnName, ".")+1:] |
|
| 94 |
-} |
|
| 95 |
- |
|
| 96 |
-func assertSuccessEquals(t *testing.T, expected int64, fn parseFn, arg string) {
|
|
| 97 |
- res, err := fn(arg) |
|
| 98 |
- if err != nil || res != expected {
|
|
| 99 |
- t.Errorf("%s(\"%s\") -> expected '%d' but got '%d' with error '%v'", fn, arg, expected, res, err)
|
|
| 100 |
- } |
|
| 101 |
-} |
|
| 102 |
- |
|
| 103 |
-func assertError(t *testing.T, fn parseFn, arg string) {
|
|
| 104 |
- res, err := fn(arg) |
|
| 105 |
- if err == nil && res != -1 {
|
|
| 106 |
- t.Errorf("%s(\"%s\") -> expected error but got '%d'", fn, arg, res)
|
|
| 107 |
- } |
|
| 108 |
-} |
| ... | ... |
@@ -12,8 +12,8 @@ import ( |
| 12 | 12 |
"github.com/docker/docker/pkg/parsers" |
| 13 | 13 |
"github.com/docker/docker/pkg/signal" |
| 14 | 14 |
"github.com/docker/docker/pkg/stringutils" |
| 15 |
- "github.com/docker/docker/pkg/units" |
|
| 16 | 15 |
"github.com/docker/docker/volume" |
| 16 |
+ "github.com/docker/go-units" |
|
| 17 | 17 |
) |
| 18 | 18 |
|
| 19 | 19 |
var ( |