Begin to add Numeric class helper for converting size strings

This commit is contained in:
Brian Cain 2019-11-14 14:09:14 -08:00
parent 8031ebe9d1
commit 271cf8a603
No known key found for this signature in database
GPG Key ID: 9FC4639B2E4510A0
2 changed files with 44 additions and 2 deletions

View File

@ -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: "<Number>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

View File

@ -1,6 +1,8 @@
require "log4r" require "log4r"
require "securerandom" require "securerandom"
require "vagrant/util/numeric"
module VagrantPlugins module VagrantPlugins
module Kernel_V2 module Kernel_V2
class VagrantConfigDisk < Vagrant.plugin("2", :config) 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(', ')}" errors << "Disk type '#{@type}' is not a valid type. Please pick one of the following supported disk types: #{DEFAULT_DISK_TYPES.join(', ')}"
end end
# TODO: Convert a string to int here?
if @size && !@size.is_a?(Integer) 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 end
if @file if @file