From 271cf8a60344b06f84c4a09c8566682e1304a7bf Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Thu, 14 Nov 2019 14:09:14 -0800 Subject: [PATCH] Begin to add Numeric class helper for converting size strings --- lib/vagrant/util/numeric.rb | 37 ++++++++++++++++++++++++++++++++ plugins/kernel_v2/config/disk.rb | 9 ++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 lib/vagrant/util/numeric.rb diff --git a/lib/vagrant/util/numeric.rb b/lib/vagrant/util/numeric.rb new file mode 100644 index 000000000..05d029218 --- /dev/null +++ b/lib/vagrant/util/numeric.rb @@ -0,0 +1,37 @@ +module Vagrant + module Util + class Numeric + # Authors Note: This class has borrowed some code from the ActiveSupport Numeric class + + # Conversion helper constants + KILOBYTE = 1024 + MEGABYTE = KILOBYTE * 1024 + GIGABYTE = MEGABYTE * 1024 + TERABYTE = GIGABYTE * 1024 + PETABYTE = TERABYTE * 1024 + EXABYTE = PETABYTE * 1024 + + class << self + + # A helper that converts a shortcut string to its bytes representation. + # The expected format of `str` is essentially: "XX" + # Where `XX` is shorthand for KB, MB, GB, TB, PB, or EB. For example, 50 megabytes: + # + # str = "50MB" + # + # @param [String] - str + # @return [Integer] - bytes + def string_to_bytes(str) + str = str.to_s.strip + end + + # @private + # Reset the cached values for platform. This is not considered a public + # API and should only be used for testing. + def reset! + instance_variables.each(&method(:remove_instance_variable)) + end + end + end + end +end diff --git a/plugins/kernel_v2/config/disk.rb b/plugins/kernel_v2/config/disk.rb index afdcfdc7c..85f67106d 100644 --- a/plugins/kernel_v2/config/disk.rb +++ b/plugins/kernel_v2/config/disk.rb @@ -1,6 +1,8 @@ require "log4r" require "securerandom" +require "vagrant/util/numeric" + module VagrantPlugins module Kernel_V2 class VagrantConfigDisk < Vagrant.plugin("2", :config) @@ -124,9 +126,12 @@ module VagrantPlugins errors << "Disk type '#{@type}' is not a valid type. Please pick one of the following supported disk types: #{DEFAULT_DISK_TYPES.join(', ')}" end - # TODO: Convert a string to int here? if @size && !@size.is_a?(Integer) - errors << "Config option size for disk is not an integer" + if @size.is_a?(String) + @size = Vagrant::Util::Numeric.string_to_bytes(@size) + else + errors << "Config option size for disk is not an integer" + end end if @file