~mna/tulip-cli

ref: v0.0.5 tulip-cli/src/utils.lua -rw-r--r-- 522 bytes
266f3ff6Martin Angers Release v0.0.5 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
local base64 = require 'base64'

local rand = assert(io.open('/dev/urandom', 'r'))
local enc = base64.makeencoder('_', '_', '_')

local M = {}

function M.random_string(len)
  local raw = rand:read(len)
  return base64.encode(raw, enc)
end

function M.log(s, ...)
  local msg = string.format(s, ...)
  io.write(msg)
  if not string.match(msg, '\n$') then
    io.flush()
  end
end

function M.write_file(path, content)
  local fd = assert(io.open(path, 'w+'))
  assert(fd:write(content))
  assert(fd:close())
end

return M