forked from Telmate/proxmox-api-go
-
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.
Add size unit for easier string formatting of proxmox related disk st…
…orage units Add create disk
- Loading branch information
Steven
committed
Aug 14, 2018
1 parent
6ca01f8
commit d6f3707
Showing
2 changed files
with
58 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
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,44 @@ | ||
package sizeUnit | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
type SizeUnit uint64 | ||
|
||
const ( | ||
KB SizeUnit = 1 << (10 * (iota + 1)) | ||
MB | ||
GB | ||
) | ||
|
||
var shortUnitMap = map[SizeUnit]string{ | ||
KB: "K", | ||
MB: "M", | ||
GB: "G", | ||
} | ||
|
||
var longUnitMap = map[SizeUnit]string{ | ||
KB: "kilobyte", | ||
MB: "megabyte", | ||
GB: "gigabyte", | ||
} | ||
|
||
func FormatToShortString(size int, sizeUnit SizeUnit) string { | ||
return strconv.Itoa(size) + shortUnitMap[sizeUnit] | ||
} | ||
|
||
func FormatToLongString(size int, sizeUnit SizeUnit) string { | ||
return fmt.Sprintf("%s %s", strconv.Itoa(size), longUnitMap[sizeUnit]) | ||
} | ||
|
||
func ConvertTo(size int, oldSizeUnit SizeUnit, newSizeUnit SizeUnit) (newSize int, newUnit SizeUnit) { | ||
if oldSizeUnit < newSizeUnit { | ||
return size / int(newSizeUnit), newSizeUnit | ||
} else if newSizeUnit > oldSizeUnit { | ||
return size * int(newSizeUnit), newSizeUnit | ||
} else { | ||
return size, newSizeUnit | ||
} | ||
} |