lune/.lune/hello_lune.luau

122 lines
3 KiB
Text
Raw Normal View History

2023-01-19 01:47:14 +00:00
print("Hello, lune! 🌙")
2023-01-19 02:57:30 +00:00
--[==[
EXAMPLE #1
Using a function from another module
]==]
2023-01-19 01:47:14 +00:00
local module = require(".lune/module")
2023-01-19 01:47:14 +00:00
module.hello()
2023-01-19 02:57:30 +00:00
--[==[
EXAMPLE #2
Read files in the current directory
This prints out directory & file names with some fancy icons
]==]
2023-01-19 01:47:14 +00:00
2023-01-19 02:57:30 +00:00
print("\nReading current dir 🗂️")
2023-01-19 01:47:14 +00:00
local entries = fs.readDir(".")
-- NOTE: We have to do this outside of the sort function
-- to avoid yielding across the metamethod boundary, any
-- calls to fs functions may yield for any reason
local dirs = {}
for _, entry in entries do
dirs[entry] = fs.isDir(entry)
end
-- Sort directories first, then alphabetically
table.sort(entries, function(entry0, entry1)
if dirs[entry0] ~= dirs[entry1] then
return dirs[entry0]
end
return entry0 < entry1
end)
2023-01-19 02:57:30 +00:00
-- Make sure we got some known files that should always exist
2023-01-19 01:47:14 +00:00
assert(table.find(entries, "Cargo.toml") ~= nil, "Missing Cargo.toml")
assert(table.find(entries, "Cargo.lock") ~= nil, "Missing Cargo.lock")
2023-01-19 02:57:30 +00:00
-- Print the pretty stuff
2023-01-19 01:47:14 +00:00
for _, entry in entries do
if fs.isDir(entry) then
print("📁 " .. entry)
else
print("📄 " .. entry)
end
end
2023-01-19 02:57:30 +00:00
--[==[
EXAMPLE #3
Get & set environment variables
Checks if environment variables are empty or not,
prints out ❌ if empty and ✅ if they have a value
]==]
2023-01-19 01:47:14 +00:00
2023-01-19 02:57:30 +00:00
print("\nReading current environment 🔎")
2023-01-19 01:47:14 +00:00
local vars = process.getEnvVars()
table.sort(vars)
assert(table.find(vars, "PATH") ~= nil, "Missing PATH")
assert(table.find(vars, "PWD") ~= nil, "Missing PWD")
for _, key in vars do
local value = process.getEnvVar(key)
local box = if value and value ~= "" then "✅" else "❌"
print(string.format("[%s] %s", box, key))
end
2023-01-19 02:57:30 +00:00
-- NOTE: We skip the last example in GitHub Actions
-- since the ping command does not work in azure
if process.getEnvVar("GITHUB_ACTIONS") then
print("\nGoodbye, lune! 🌙")
process.exit(0)
end
2023-01-19 01:55:27 +00:00
2023-01-19 02:57:30 +00:00
--[==[
EXAMPLE #4
Call out to another program / executable
Here we send some pings to google to demonstrate that programs
that yield or perform any network requests work correctly
]==]
print("\nSending 4 pings to google 🌏")
local result = process.spawn("ping", {
"google.com",
"-c 4",
})
--[==[
EXAMPLE #5
Using the result of a spawned process, exiting the process
We use the result from the above ping command and parse it
to show the results it gave us in a nicer format, then we
either exit successfully or with an error (exit code 1)
]==]
if result.ok then
assert(#result.stdout > 0, "Result output was empty")
local min, avg, max, stddev = string.match(
result.stdout,
"min/avg/max/stddev = ([%d%.]+)/([%d%.]+)/([%d%.]+)/([%d%.]+) ms"
)
print(string.format("Minimum ping time: %.3fms", assert(tonumber(min))))
print(string.format("Maximum ping time: %.3fms", assert(tonumber(max))))
print(string.format("Average ping time: %.3fms", assert(tonumber(avg))))
print(string.format("Standard deviation: %.3fms", assert(tonumber(stddev))))
else
print(result.stderr)
process.exit(result.code)
2023-01-19 01:47:14 +00:00
end
print("\nGoodbye, lune! 🌙")