Global Utilities

Here you can find various helpers available in the root cloud namespace.

cloud


Log

.log

Prints a string message to the /usr/local/cloud/logs/cloud.log file.

cloud.log( message )

Parameters

Name Description Default Requried
message The message string to log. none Y

Usage

cloud.log('something to log')

String Format

.sf

Format a string based on token replacment. This method shares the same attributes as the C string format method.

cloud.sf( str_template, values )

Parameters

Name Description Default Requried
str_template The string containing replacment tokens. none Y
values A list of values for token replacment. none Y

Usage

local username = "Dave"
local formatted_str = cloud.sf("Hello, %s!", username)

-- formatted_str = "Hello, Dave!"

Notes

Some common token types:

Token Description
%s Used as a string replacment, any other type will error.
%d Used as a number replacment, any other type will error.

You can mix tokens, as well as have multiples:

local str = cloud.sf("Ordered %d %s with %s.", 2, 'burgers', 'cheese')
-- `str` contains "Ordered 2 burgers with cheese."

Did You Know?

The sf utility is useful for creating SQL queries, as well as other type-safe replacments.


Trim

.trim

Removes empty space from the start and end of a string, if any.

cloud.trim( string )

Parameters

Name Description Default Requried
string The string to trim. none Y

Usage

local trimmed_str = cloud.trim("  I could use a trim  ")

-- trimmed_str = "I could use a trim"

Split

.split

Converts a delimited string into a Lua table array.

cloud.split( string[, delimiter ] )

Parameters

Name Description Default Requried
string The delimited string to split. none Y
delimiter The delimiter to split the string with. comma(,) N

Usage

local tbl_array = cloud.split("Red,Green,Blue")

-- `tbl_array` contains {"Red","Green","Blue"}

Using a custom delimiter:

local tbl_array = cloud.split("User:2001:Storage", ":")

-- `tbl_array` contains {"User","2001","Storage"}

UUID

.uuid

Generates a universally unique id.

cloud.uuid()

Parameters

none

Usage

local uuid = cloud.uuid()

-- `uuid` will contain something like "1f0af2fa-8b06-4605-bace-e13a85aa36d5"

FileExists

.exists

Check if a file exists.

cloud.exists( filepath )

Parameters

Name Description Default Requried
filepath The absolute path to the file. nil Y

Returns

If the file is found, will return true, if not then nil

Usage

if not cloud.exists('/some/path/to/file') then
  print('no file')
end

TableMerge

.tmerge

Merge a series of tables, with optional default inputs. Last value wins.

cloud.tmerge( default_vals, tbl_obj_1[, tbl_obj_2[, ...]] )

Parameters

Name Description Type Default Requried
default_vals A keyed table of default values. These values can be overridden. table nil Y
tbl_obj_1 A keyed table of values. Will override any existing default_vals. You can add as many additional tables as needed. table nil Y

Returns

Merged table.

Usage

--some "defaults"
local defaults =
{
  color = "blue",
  points = 0
}

--a "user config"
local user_config =
{
  color = "red"
}

--some other meta
local meta =
{
  last_score = 200,
  fav_avatar = "orange",
  points = 10
}

--merge tables
local merged_tbl = cloud.tmerge( defaults, user_config, meta )

The merged_tbl will contain:

{
  color = "red",
  points = 10,
  last_score = 200,
  fav_avatar = "orange"
}