Skip to content

Commit 54fa919

Browse files
committed
feat(cli): Add autogen to cli
1 parent ec8505e commit 54fa919

File tree

3 files changed

+66
-26
lines changed

3 files changed

+66
-26
lines changed

sailor

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env lua
22

33
--------------------------------------------------------------------------------
4-
-- sailor v0.6: Command line utility for sailor
4+
-- sailor v0.6.1: Command line utility for sailor
55
-- This file is a part of Sailor project
66
-- Copyright (c) 2014 Etiene Dalcol <[email protected]>
77
-- License: MIT
@@ -27,17 +27,41 @@ create_cmd:epilog(colors([[
2727
Example: %{bright red} sailor create 'Hey Arnold' /var/www %{reset}
2828
This will create your web app under /var/www/hey_arnold.]]))
2929

30-
local test_cmd = parser:command("t test", "Will run the tests specified for an application. Must be called from the base dir of the application.")
30+
local test_cmd = parser:command("t test", "Runs the tests specified for an application. Must be called from the base dir of the application.")
3131
:action(actions.test)
3232
test_cmd:usage(colors"%{bright red}Usage: sailor test [--resty] [-- EXTRA_FLAGS]")
3333
test_cmd:argument("EXTRA_FLAGS", "Flags that will be passed to the Busted library."):args("*")
3434
test_cmd:flag("--resty", "Run the tests using the resty bootstrap.", false)
3535

36-
local enable_cmd = parser:command("e enable", "Will install an extension to Sailor and copy necessary files to your app. Must be called from the base dir of the application.")
36+
local enable_cmd = parser:command("e enable", "Installs an extension to Sailor and copy necessary files to your app. Must be called from the base dir of the application.")
3737
:action(actions.enable)
3838
enable_cmd:usage(colors("%{bright red}Usage: sailor enable NAME"))
3939
enable_cmd:argument("name", "The name of the extension to be enabled.")
4040

41+
42+
local gen_cmd = parser:command("g gen", "Generates some scaffolding for your app.")
43+
gen_cmd:usage(colors("%{bright red}Usage: sailor gen COMMAND ARG"))
44+
gen_cmd:epilog(colors([[
45+
Example: %{bright red} sailor gen model users%{reset}
46+
Given a table called 'users' exist in the database, this will generate a model based on it.]]))
47+
48+
local model_cmd = gen_cmd:command("m model", "Generates a basic model based on an existing table on the database.")
49+
:action(actions.gen_model)
50+
model_cmd:argument("table_name", "The name of the database table that the model should reflect.")
51+
model_cmd:usage(colors("%{bright red}Usage: sailor gen model TABLE_NAME"))
52+
53+
local crud_cmd = gen_cmd:command("crud c", "Generates Create-Read-Update-Delete scaffolding for a given model name.")
54+
:action(actions.gen_crud)
55+
crud_cmd:argument("model_name", "The name of the model table that CRUD should act on.")
56+
crud_cmd:usage(colors("%{bright red}Usage: sailor gen crud MODEL_NAME"))
57+
58+
local all_cmd = gen_cmd:command("a all", "Generates both the model and a CRUD for an existing table name.")
59+
:action(actions.gen_all)
60+
all_cmd:argument("table_name", "The name of the database table that the model and the CRUD should reflect.")
61+
all_cmd:usage(colors("%{bright red}Usage: sailor gen all TABLE_NAME"))
62+
63+
64+
4165
parser:flag("-v --version", "Show the current Sailor version installed and exit.", false):action(actions.version)
4266

4367
parser:usage(parser:get_help())

src/sailor/cli.lua

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--------------------------------------------------------------------------------
2-
-- cli.lua v0.1: Functions for sailor's command line utility
2+
-- cli.lua v0.1.1: Functions for sailor's command line utility
33
-- This file is a part of Sailor project
44
-- Copyright (c) 2014 Etiene Dalcol <[email protected]>
55
-- License: MIT
@@ -63,26 +63,6 @@ function cli.create(args, _)
6363
os.exit(0)
6464
end
6565

66-
function cli.test(args, _)
67-
local ok, code
68-
69-
flags = table.concat(args.EXTRA_FLAGS, " ")
70-
71-
if args.resty then
72-
ok, code = os.execute('resty tests/bootstrap_resty.lua')
73-
else
74-
ok, code = os.execute('busted --helper=tests/bootstrap.lua '..flags..' tests/unit/* tests/functional/*')
75-
end
76-
77-
if type(ok) == "number" then return ok end -- Lua 5.1 just returns the status code
78-
exit_code = ok and 0 or 1 -- don't care about actual value
79-
80-
if exit_code and exit_code ~= 0 then
81-
-- exit code sometimes is > 255 and fails to be propagated
82-
os.exit(1, true)
83-
end
84-
end
85-
8666
function cli.enable(args, _)
8767
local name = 'sailor-'..args.name
8868
local current_dir = lfs.currentdir()
@@ -112,6 +92,42 @@ function cli.enable(args, _)
11292
end
11393
end
11494

95+
function cli.gen_all(args)
96+
local model = require "sailor.model"
97+
model.generate_model(args.table_name)
98+
model.generate_crud(args.table_name)
99+
end
100+
101+
function cli.gen_crud(args)
102+
local model = require "sailor.model"
103+
model.generate_crud(args.model_name)
104+
end
105+
106+
function cli.gen_model(args)
107+
local model = require "sailor.model"
108+
model.generate_model(args.table_name)
109+
end
110+
111+
function cli.test(args, _)
112+
local ok, code
113+
114+
flags = table.concat(args.EXTRA_FLAGS, " ")
115+
116+
if args.resty then
117+
ok, code = os.execute('resty tests/bootstrap_resty.lua')
118+
else
119+
ok, code = os.execute('busted --helper=tests/bootstrap.lua '..flags..' tests/unit/* tests/functional/*')
120+
end
121+
122+
if type(ok) == "number" then return ok end -- Lua 5.1 just returns the status code
123+
exit_code = ok and 0 or 1 -- don't care about actual value
124+
125+
if exit_code and exit_code ~= 0 then
126+
-- exit code sometimes is > 255 and fails to be propagated
127+
os.exit(1, true)
128+
end
129+
end
130+
115131
function cli.version()
116132
print("Sailor: version " .. require "sailor.version")
117133
os.exit(0)

src/sailor/model.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--------------------------------------------------------------------------------
2-
-- model.lua, v0.10.3: basic model creator, uses db module
2+
-- model.lua, v0.10.4: basic model creator, uses db module
33
-- This file is a part of Sailor project
44
-- Copyright (c) 2014 Etiene Dalcol <[email protected]>
55
-- License: MIT
@@ -347,7 +347,7 @@ end
347347
-- Generates a CRUD based on the given model, model must already exist
348348
-- model_name: string, the name of the model
349349
function model.generate_crud(model_name)
350-
local f=io.open(sailor.path.."/models/"..model_name..".lua","r")
350+
local f=io.open("models/"..model_name..".lua","r")
351351
if f == nil then
352352
error("The model '"..model_name.."'does not exist")
353353
else

0 commit comments

Comments
 (0)