make unit not optional in parseVolumeSize

This commit is contained in:
Julien Gilli 2017-04-05 15:19:46 -07:00
parent d4dbff084e
commit 348db1ebcc
3 changed files with 5 additions and 9 deletions

View File

@ -154,9 +154,7 @@ do_create.options = [
helpArg: 'SIZE',
help: 'The size of the volume to create, in the form ' +
'`<integer><unit>`, e.g. `20G`. <integer> must be > 0. Supported ' +
'units are `G` or `g` for gibibytes and `M` or `m` for mebibytes.' +
' If <unit> is not provided, <integer> is considered to be in ' +
'mebibytes.',
'units are `G` or `g` for gibibytes and `M` or `m` for mebibytes.',
completionType: 'tritonvolumesize'
},
{

View File

@ -20,11 +20,9 @@ function throwInvalidSize(size) {
* Returns the number of MiBs (Mebibytes) represented by the string "size". That
* string has the following format: <integer><unit>. The integer must be > 0.
* Unit format suffixes are 'G' or 'g' for gibibytes and 'M' or 'm' for
* mebibytes. If no unit suffix is provided, the unit is considered to be
* mebibytes.
*
* Examples:
* - the string '100' represents 100 mebibytes
* - the strings '100m' and '100M' represent 100 mebibytes
* - the strings '100g' and '100G' represent 100 gibibytes
*
@ -42,11 +40,11 @@ function parseVolumeSize(size) {
M: 1
};
var multiplier = 1; /* default unit is mebibytes */
var multiplier;
var multiplierSymbol;
var baseValue;
var matches = size.match(/^([1-9]\d*)(g|m|G|M)?$/);
var matches = size.match(/^([1-9]\d*)(g|m|G|M)$/);
if (!matches) {
throwInvalidSize(size);
}
@ -57,7 +55,8 @@ function parseVolumeSize(size) {
}
baseValue = Number(matches[1]);
if (isNaN(baseValue)) {
if (isNaN(baseValue) || multiplier === undefined) {
throwInvalidSize(size);
}

View File

@ -63,7 +63,6 @@ test('parseVolumeSize', function (tt) {
tt.test('parsing valid sizes', function (t) {
var validVolumeSizes = [
{input: '42', expectedOutput: 42},
{input: '42m', expectedOutput: 42},
{input: '42M', expectedOutput: 42},
{input: '42g', expectedOutput: 42 * 1024},