Puppet Function: to_bytes

Defined in:
lib/puppet/functions/to_bytes.rb
Function type:
Ruby 4.x API

Summary

Converts the argument into bytes, for example 4kB becomes 4096. Takes a single string value as an argument.

Overview

to_bytes(Variant[Numeric,String] $arg)Any

to_bytes.rb

Parameters:

  • arg (Variant[Numeric,String])

    The string containing a byte size

Returns:

  • (Any)

    Value converted into bytes



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/puppet/functions/to_bytes.rb', line 4

Puppet::Functions.create_function(:to_bytes) do
  # @summary Converts the argument into bytes, for example 4kB becomes 4096. Takes a single string value as an argument.
  # @param arg The string containing a byte size
  # @return Value converted into bytes
  # Supports both decimal and binary multiples: 1 MB = 1000 KB and 1 MiB = 1024 KiB.
  dispatch :to_bytes do
    param 'Variant[Numeric,String]', :arg
  end

  def to_bytes(arg)
    # raise(Puppet::ParseError, "to_bytes(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1

    return arg if arg.is_a? Numeric

    value, prefix = *%r{([0-9.e+-]*)\s*([^bB]{1,2})}.match(arg)[1, 2]

    value = value.to_f
    # binding.pry
    case prefix.downcase
    when '' then return value.to_i
    when 'ki' then return (value * (1 << 10)).to_i
    when 'mi' then return (value * (1 << 20)).to_i
    when 'gi' then return (value * (1 << 30)).to_i
    when 'ti' then return (value * (1 << 40)).to_i
    when 'pi' then return (value * (1 << 50)).to_i
    when 'ei' then return (value * (1 << 60)).to_i
    when 'k' then return (value * 10**3).to_i
    when 'm' then return (value * 10**6).to_i
    when 'g' then return (value * 10**9).to_i
    when 't' then return (value * 10**12).to_i
    when 'p' then return (value * 10**15).to_i
    when 'e' then return (value * 10**18).to_i
    else raise Puppet::ParseError, "to_bytes(): Unknown prefix #{prefix}"
    end
  end
end