-
Notifications
You must be signed in to change notification settings - Fork 91
/
hello_lune.luau
226 lines (179 loc) Β· 4.99 KB
/
hello_lune.luau
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
--> A walkthrough of all of the basic Lune features.
print("Hello, lune! π")
--[==[
EXAMPLE #1
Using a function from another module
]==]
local module = require("./module")
module.sayHello()
--[==[
EXAMPLE #2
Using arguments given to the program
]==]
if #process.args > 0 then
print("\nGot arguments while running hello_lune:")
console.log(process.args)
if #process.args > 3 then
error("Too many arguments!")
end
end
--[==[
EXAMPLE #3
Spawning tasks
]==]
task.spawn(function()
print("\nSpawned a task that will run instantly but not block")
task.wait(5)
end)
print("Spawning a delayed task that will run in 5 seconds")
task.delay(5, function()
print("\n...")
task.wait(1)
print("Hello again!")
task.wait(1)
print("Goodbye again! π")
end)
--[==[
EXAMPLE #4
Get & set environment variables
Checks if environment variables are empty or not,
prints out β if empty and β
if they have a value
]==]
print("\nReading current environment π")
-- Environment variables can be read directly
assert(process.env.PATH ~= nil, "Missing PATH")
assert(process.env.PWD ~= nil, "Missing PWD")
-- And they can also be accessed using generalized iteration (not pairs!)
for key, value in process.env do
local box = if value and value ~= "" then "β
" else "β"
print(string.format("[%s] %s", box, key))
end
--[==[
EXAMPLE #5
Read files in the current directory
This prints out directory & file names with some fancy icons
]==]
print("\nReading current dir ποΈ")
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 entryIsDir = {}
for _, entry in entries do
entryIsDir[entry] = fs.isDir(entry)
end
-- Sort prioritizing directories first, then alphabetically
table.sort(entries, function(entry0, entry1)
if entryIsDir[entry0] ~= entryIsDir[entry1] then
return entryIsDir[entry0]
end
return entry0 < entry1
end)
-- Make sure we got some known files that should always exist
assert(table.find(entries, "Cargo.toml") ~= nil, "Missing Cargo.toml")
assert(table.find(entries, "Cargo.lock") ~= nil, "Missing Cargo.lock")
-- Print the pretty stuff
for _, entry in entries do
if fs.isDir(entry) then
print("π " .. entry)
else
print("π " .. entry)
end
end
-- NOTE: We skip the ping example in GitHub Actions
-- since the ping command does not work in azure
if not process.env.GITHUB_ACTIONS then
--[==[
EXAMPLE #6
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 #7
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, here we
also exit with an error (exit code 1) if spawning the process failed
]==]
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("\nFailed to send ping to google!")
print(result.stderr)
process.exit(result.code)
end
end
--[==[
EXAMPLE #8
Using the built-in networking library
]==]
print("\nSending PATCH request to web API π€")
local apiResult = net.request({
url = "https://jsonplaceholder.typicode.com/posts/1",
method = "PATCH",
headers = {
["Content-Type"] = "application/json",
},
body = net.jsonEncode({
title = "foo",
body = "bar",
}),
})
if not apiResult.ok then
print("\nFailed to send network request!")
print(string.format("%d (%s)", apiResult.statusCode, apiResult.statusMessage))
print(apiResult.body)
process.exit(1)
end
type ApiResponse = {
id: number,
title: string,
body: string,
userId: number,
}
local apiResponse: ApiResponse = net.jsonDecode(apiResult.body)
assert(apiResponse.title == "foo", "Invalid json response")
assert(apiResponse.body == "bar", "Invalid json response")
print("Got valid JSON response with changes applied")
--[==[
EXAMPLE #9
Using the console library to print pretty
]==]
print("\nPrinting with pretty colors and auto-formatting π¨")
console.setColor("blue")
print(string.rep("β", 22))
console.resetColor()
console.info("API response:", apiResponse)
console.warn({
Oh = {
No = {
TooMuch = {
Nesting = {
"Will not print",
},
},
},
},
})
console.setColor("blue")
print(string.rep("β", 22))
console.resetColor()
--[==[
EXAMPLE #10
Saying goodbye π
]==]
print("\nGoodbye, lune! π")