From 2e324a4971bc3c5fc2f57df8c662eb939208d522 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Fri, 15 Nov 2019 15:21:43 -0800 Subject: [PATCH] Add conversion method for shortcut size in disk config --- lib/vagrant/util/numeric.rb | 28 ++++++++++++++++++++++++++-- plugins/kernel_v2/config/disk.rb | 4 +--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/vagrant/util/numeric.rb b/lib/vagrant/util/numeric.rb index 05d029218..3a2989d97 100644 --- a/lib/vagrant/util/numeric.rb +++ b/lib/vagrant/util/numeric.rb @@ -1,8 +1,10 @@ +require "log4r" + module Vagrant module Util class Numeric - # Authors Note: This class has borrowed some code from the ActiveSupport Numeric class + # Authors Note: This conversion has been borrowed from the ActiveSupport Numeric class # Conversion helper constants KILOBYTE = 1024 MEGABYTE = KILOBYTE * 1024 @@ -11,7 +13,14 @@ module Vagrant PETABYTE = TERABYTE * 1024 EXABYTE = PETABYTE * 1024 + BYTES_CONVERSION_MAP = {KB: KILOBYTE, MB: MEGABYTE, GB: GIGABYTE, TB: TERABYTE, + PB: PETABYTE, EB: EXABYTE} + + # Regex borrowed from the vagrant-disksize config class + SHORTHAND_MATCH_REGEX = /^(?[0-9]+)\s?(?KB|MB|GB|TB)?$/ + class << self + LOGGER = Log4r::Logger.new("vagrant::util::numeric") # A helper that converts a shortcut string to its bytes representation. # The expected format of `str` is essentially: "XX" @@ -20,9 +29,24 @@ module Vagrant # str = "50MB" # # @param [String] - str - # @return [Integer] - bytes + # @return [Integer,nil] - bytes - returns nil if method fails to convert to bytes def string_to_bytes(str) + bytes = nil + str = str.to_s.strip + matches = SHORTHAND_MATCH_REGEX.match(str) + if matches + number = matches[:number].to_i + unit = matches[:unit].to_sym + + if BYTES_CONVERSION_MAP.key?(unit) + bytes = number * BYTES_CONVERSION_MAP[unit] + else + LOGGER.error("An invalid unit or format was given, string_to_bytes cannot convert #{str}") + end + end + + bytes end # @private diff --git a/plugins/kernel_v2/config/disk.rb b/plugins/kernel_v2/config/disk.rb index 85f67106d..82398daf4 100644 --- a/plugins/kernel_v2/config/disk.rb +++ b/plugins/kernel_v2/config/disk.rb @@ -31,9 +31,7 @@ module VagrantPlugins # Size of disk to create # - # TODO: Should we have shortcuts for GB??? - # - # @return [Integer] + # @return [Integer,String] attr_accessor :size # Path to the location of the disk file (Optional)