-
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.
Docker-DCO-1.1-Signed-off-by: Michael Crosby <[email protected]> (github: crosbymichael)
- Loading branch information
1 parent
0d78799
commit d33b465
Showing
11 changed files
with
165 additions
and
140 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
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
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
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,2 @@ | ||
Michael Crosby <[email protected]> (@crosbymichael) | ||
Victor Vieux <[email protected]> (@vieux) |
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,31 @@ | ||
package units | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// HumanDuration returns a human-readable approximation of a duration | ||
// (eg. "About a minute", "4 hours ago", etc.) | ||
func HumanDuration(d time.Duration) string { | ||
if seconds := int(d.Seconds()); seconds < 1 { | ||
return "Less than a second" | ||
} else if seconds < 60 { | ||
return fmt.Sprintf("%d seconds", seconds) | ||
} else if minutes := int(d.Minutes()); minutes == 1 { | ||
return "About a minute" | ||
} else if minutes < 60 { | ||
return fmt.Sprintf("%d minutes", minutes) | ||
} else if hours := int(d.Hours()); hours == 1 { | ||
return "About an hour" | ||
} else if hours < 48 { | ||
return fmt.Sprintf("%d hours", hours) | ||
} else if hours < 24*7*2 { | ||
return fmt.Sprintf("%d days", hours/24) | ||
} else if hours < 24*30*3 { | ||
return fmt.Sprintf("%d weeks", hours/24/7) | ||
} else if hours < 24*365*2 { | ||
return fmt.Sprintf("%d months", hours/24/30) | ||
} | ||
return fmt.Sprintf("%f years", d.Hours()/24/365) | ||
} |
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,56 @@ | ||
package units | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// HumanSize returns a human-readable approximation of a size | ||
// using SI standard (eg. "44kB", "17MB") | ||
func HumanSize(size int64) string { | ||
i := 0 | ||
var sizef float64 | ||
sizef = float64(size) | ||
units := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"} | ||
for sizef >= 1000.0 { | ||
sizef = sizef / 1000.0 | ||
i++ | ||
} | ||
return fmt.Sprintf("%.4g %s", sizef, units[i]) | ||
} | ||
|
||
// Parses a human-readable string representing an amount of RAM | ||
// in bytes, kibibytes, mebibytes or gibibytes, and returns the | ||
// number of bytes, or -1 if the string is unparseable. | ||
// Units are case-insensitive, and the 'b' suffix is optional. | ||
func RAMInBytes(size string) (bytes int64, err error) { | ||
re, error := regexp.Compile("^(\\d+)([kKmMgG])?[bB]?$") | ||
if error != nil { | ||
return -1, error | ||
} | ||
|
||
matches := re.FindStringSubmatch(size) | ||
|
||
if len(matches) != 3 { | ||
return -1, fmt.Errorf("Invalid size: '%s'", size) | ||
} | ||
|
||
memLimit, error := strconv.ParseInt(matches[1], 10, 0) | ||
if error != nil { | ||
return -1, error | ||
} | ||
|
||
unit := strings.ToLower(matches[2]) | ||
|
||
if unit == "k" { | ||
memLimit *= 1024 | ||
} else if unit == "m" { | ||
memLimit *= 1024 * 1024 | ||
} else if unit == "g" { | ||
memLimit *= 1024 * 1024 * 1024 | ||
} | ||
|
||
return memLimit, 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,54 @@ | ||
package units | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestHumanSize(t *testing.T) { | ||
|
||
size := strings.Trim(HumanSize(1000), " \t") | ||
expect := "1 kB" | ||
if size != expect { | ||
t.Errorf("1000 -> expected '%s', got '%s'", expect, size) | ||
} | ||
|
||
size = strings.Trim(HumanSize(1024), " \t") | ||
expect = "1.024 kB" | ||
if size != expect { | ||
t.Errorf("1024 -> expected '%s', got '%s'", expect, size) | ||
} | ||
} | ||
|
||
func TestRAMInBytes(t *testing.T) { | ||
assertRAMInBytes(t, "32", false, 32) | ||
assertRAMInBytes(t, "32b", false, 32) | ||
assertRAMInBytes(t, "32B", false, 32) | ||
assertRAMInBytes(t, "32k", false, 32*1024) | ||
assertRAMInBytes(t, "32K", false, 32*1024) | ||
assertRAMInBytes(t, "32kb", false, 32*1024) | ||
assertRAMInBytes(t, "32Kb", false, 32*1024) | ||
assertRAMInBytes(t, "32Mb", false, 32*1024*1024) | ||
assertRAMInBytes(t, "32Gb", false, 32*1024*1024*1024) | ||
|
||
assertRAMInBytes(t, "", true, -1) | ||
assertRAMInBytes(t, "hello", true, -1) | ||
assertRAMInBytes(t, "-32", true, -1) | ||
assertRAMInBytes(t, " 32 ", true, -1) | ||
assertRAMInBytes(t, "32 mb", true, -1) | ||
assertRAMInBytes(t, "32m b", true, -1) | ||
assertRAMInBytes(t, "32bm", true, -1) | ||
} | ||
|
||
func assertRAMInBytes(t *testing.T, size string, expectError bool, expectedBytes int64) { | ||
actualBytes, err := RAMInBytes(size) | ||
if (err != nil) && !expectError { | ||
t.Errorf("Unexpected error parsing '%s': %s", size, err) | ||
} | ||
if (err == nil) && expectError { | ||
t.Errorf("Expected to get an error parsing '%s', but got none (bytes=%d)", size, actualBytes) | ||
} | ||
if actualBytes != expectedBytes { | ||
t.Errorf("Expected '%s' to parse as %d bytes, got %d", size, expectedBytes, actualBytes) | ||
} | ||
} |
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
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
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
Oops, something went wrong.