-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.lua
More file actions
175 lines (142 loc) · 5.36 KB
/
Copy pathadmin.lua
File metadata and controls
175 lines (142 loc) · 5.36 KB
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
local flag = arg[1]
if flag == 'init' then
local name = ''
if arg[2] then
name = arg[2] .. '/'
end
os.execute('mkdir ' .. name)
os.execute('mkdir ' .. name .. 'migrations/')
os.execute('mkdir ' .. name .. 'functions/')
os.execute('mkdir ' .. name .. 'public/')
os.execute('touch ' .. name .. 'config.lua')
local f = assert(io.open(name .. 'config.lua', 'w'))
f:write(CONFIG_TEMPLATE)
f:close()
-- Generate styles.css file
local styles_file = assert(io.open(name .. 'public/styles.css', 'w'))
styles_file:write(STYLES_TEMPLATE)
styles_file:close()
print('Created: ' .. name .. 'public/styles.css')
-- Generate AGENTS.md file
local agents_file = assert(io.open(name .. 'AGENTS.md', 'w'))
agents_file:write(AGENTS_TEMPLATE)
agents_file:close()
print('Created: ' .. name .. 'AGENTS.md')
-- Generate .stylua.toml file
local stylua_file = assert(io.open(name .. '.stylua.toml', 'w'))
stylua_file:write(STYLUA_TEMPLATE)
stylua_file:close()
print('Created: ' .. name .. '.stylua.toml')
print 'Would you like to generate any migrations (m), functions (f), both (a) or not (n)?'
local input = io.read '*l'
if input == 'm' or input == 'a' then
local base_time = os.time()
-- Migration 1: Enable pgcrypto extension
local pgcrypto_file = assert(io.open(name .. 'migrations/' .. base_time .. ':enable_pgcrypto.sql', 'w'))
pgcrypto_file:write(MIGRATION_PGCRYPTO_TEMPLATE)
pgcrypto_file:close()
print('Created: ' .. name .. 'migrations/' .. base_time .. ':enable_pgcrypto.sql')
-- Migration 2: Create users table
local users_file = assert(io.open(name .. 'migrations/' .. (base_time + 1) .. ':create_users_table.sql', 'w'))
users_file:write(MIGRATION_USERS_TABLE_TEMPLATE)
users_file:close()
print('Created: ' .. name .. 'migrations/' .. (base_time + 1) .. ':create_users_table.sql')
-- Migration 3: Create ping counter table
local ping_file = assert(io.open(name .. 'migrations/' .. (base_time + 2) .. ':create_ping_counter_table.sql', 'w'))
ping_file:write(MIGRATION_PING_COUNTER_TEMPLATE)
ping_file:close()
print('Created: ' .. name .. 'migrations/' .. (base_time + 2) .. ':create_ping_counter_table.sql')
print 'Created 3 essential migrations for database setup'
end
if input == 'f' or input == 'a' then
-- Create authentication functions
-- Create the authentication function files
local auth_file = assert(io.open(name .. 'functions/authenticate_user.sql', 'w'))
auth_file:write(FUNCTION_AUTHENTICATE_USER_TEMPLATE)
auth_file:close()
print('Created: ' .. name .. 'functions/authenticate_user.sql')
local register_file = assert(io.open(name .. 'functions/register_user.sql', 'w'))
register_file:write(FUNCTION_REGISTER_USER_TEMPLATE)
register_file:close()
print('Created: ' .. name .. 'functions/register_user.sql')
-- Create pong function for ping endpoint
local pong_file = assert(io.open(name .. 'functions/pong.sql', 'w'))
pong_file:write(FUNCTION_PONG_TEMPLATE)
pong_file:close()
print('Created: ' .. name .. 'functions/pong.sql')
end
elseif flag == 'migrate' or flag == 'm' then
local input = arg[2]
if not input then
io.write 'Migration name: '
input = io.read '*l'
if input == nil then
print 'Error: Could not read migration name.'
return
end
end
input = string.gsub(input, ' ', '_')
input = string.gsub(input, '^%s*(.-)%s*$', '%1')
if input == '' then
print 'Migration name required'
return
end
local now = os.time()
local file_name = string.format('migrations/%d:%s.sql', now, input)
local file, err = io.open(file_name, 'w')
if file then
file:close()
print(string.format('Migration file %s created.', file_name))
else
print(string.format('migration creation failed: %s', err))
end
elseif flag == 'function' or flag == 'f' then
local input = arg[2]
if not input then
io.write 'SQL function name: '
input = io.read '*l'
if input == nil then
print 'Error reading input.'
return
end
end
input = string.gsub(input, ' ', '_')
input = string.gsub(input, '^%s*(.-)%s*$', '%1')
if input == '' then
print 'Function name required'
return
end
local file_path = string.format('functions/%s.sql', input)
local exists_check = io.open(file_path, 'r')
if exists_check then
exists_check:close()
print(string.format("function creation failed: File '%s' already exists.", file_path))
return
end
local file, err = io.open(file_path, 'w')
if not file then
print(string.format('function creation failed: %s', err))
return
end
-- Read template content from file
local template_file = io.open('templates/function_template.sql', 'r')
if not template_file then
print('Error: Could not find templates/function_template.sql')
return
end
local template_content = template_file:read('*all')
template_file:close()
local content = string.gsub(template_content, '{name}', input)
local success, write_err = file:write(content)
if not success then
print(string.format('function creation failed: Failed to write content: %s', write_err))
file:close()
return
end
-- Close the file handle
file:close()
print(string.format('Function file %s created.', input))
elseif flag == 'generate' or flag == 'ai' then
else
print 'Usage: lua admin.lua [init|migrate|function|generate|delete]'
end