Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs/cli help messages #130

Merged
merged 4 commits into from
Apr 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ dev:
echo "$$rock already installed, skipping" ; \
fi \
done;
bin/kong config -e TEST
bin/kong config -e DEVELOPMENT
bin/kong db -c $(DEVELOPMENT_CONF) migrations:up
bin/kong config -c kong.yml -e TEST
bin/kong config -c kong.yml -e DEVELOPMENT
bin/kong migrations -c $(DEVELOPMENT_CONF) up

clean:
@rm -f luacov.*
@rm -f $(DEVELOPMENT_CONF) $(TESTING_CONF)
@rm -rf nginx_tmp
@bin/kong db -c $(DEVELOPMENT_CONF) migrations:reset
@bin/kong migrations -c $(DEVELOPMENT_CONF) reset

run:
@bin/kong start -c $(DEVELOPMENT_CONF)
Expand Down
26 changes: 19 additions & 7 deletions bin/kong
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
#!/usr/bin/env lua

-- Kong CLI entry-point (bin/kong).
--
-- Kong's CLI is a set of small commands invoked by a global executable, this file.
--
-- All commands are invoked by this script, then parsed (arguments and options)
-- by lapp (see http://lua-users.org/wiki/LappFramework).
--
-- This script is not parsed by lapp due to limitations of the said framework as it
-- is currently implemented.

local cutils = require "kong.cli.utils"
local infos = cutils.get_infos()
local infos = cutils.get_kong_infos()
local commands = {
db = "kong.cli.db",
stop = "kong.cli.stop",
quit = "kong.cli.quit",
start = "kong.cli.start",
reload = "kong.cli.reload",
config = "kong.cli.config",
restart = "kong.cli.restart",
version = "kong.cli.version",
["--version"] = "kong.cli.version",
restart = "kong.cli.restart"
migrations = "kong.cli.migrations"
}

local help_message = string.format([[
Usage: kong <command>

where <command> is one of:
start, stop, quit, restart, reload, config, db, version
where <command> is one of:
start, restart, reload, stop, quit, version

kong --help print this message
kong <command> --help print the help message of a command
kong --help print this message
kong <command> --help print the help message of a command

%s@%s]], infos.name, infos.version)

-- Determine validity of the given command
local cmd = arg[1]

if not cmd then
print("Missing <command>\n\n"..help_message)
os.exit(1)
Expand All @@ -38,4 +49,5 @@ elseif not commands[cmd] then
os.exit(1)
end

-- Load and execute desired command
require(commands[cmd])
1 change: 1 addition & 0 deletions kong-0.2.0-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ build = {
["kong.cli.reload"] = "src/cli/reload.lua",
["kong.cli.restart"] = "src/cli/restart.lua",
["kong.cli.version"] = "src/cli/version.lua",
["kong.cli.migrations"] = "src/cli/migrations.lua",

["kong.tools.utils"] = "src/tools/utils.lua",
["kong.tools.io"] = "src/tools/io.lua",
Expand Down
4 changes: 3 additions & 1 deletion src/cli/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ local constants = require "kong.constants"
local cutils = require "kong.cli.utils"
local IO = require "kong.tools.io"
local args = require("lapp")(string.format([[
For development purposes only.

Duplicate an existing configuration for given environment.

Usage: kong config [options]

Options:
-c,--config (default %s) configuration file
-c,--config (default %s) path to configuration file
-o,--output (default .) ouput
-e,--env (string) environment name
]], constants.CLI.GLOBAL_KONG_CONF))
Expand Down
91 changes: 8 additions & 83 deletions src/cli/db.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
#!/usr/bin/env lua

local Faker = require "kong.tools.faker"
local Migrations = require "kong.tools.migrations"

local constants = require "kong.constants"
local cutils = require "kong.cli.utils"
local IO = require "kong.tools.io"
local lapp = require("lapp")
local args = lapp(string.format([[
Migrations, seeding of the DB.
For development purposes only.

Seed the database with random data or drop it.

Usage: kong db <command> [options]

Commands:
<command> (string) where <command> is one of:
migrations, migrations:up, migrations:down, migrations:reset, seed, drop
seed, drop

Options:
-c,--config (default %s) configuration file
-r,--random <seed>: flag to also insert random entities
-n,--number (default 1000) <seed>: number of random entities to insert if --random
-c,--config (default %s) path to configuration file
-r,--random flag to also insert random entities
-n,--number (default 1000) number of random entities to insert if --random
]], constants.CLI.GLOBAL_KONG_CONF))

-- $ kong db
Expand All @@ -29,83 +29,8 @@ end

local config_path = cutils.get_kong_config_path(args.config)
local _, dao_factory = IO.load_configuration_and_dao(config_path)
local migrations = Migrations(dao_factory, cutils.get_luarocks_install_dir())

if args.command == "migrations" then

local migrations, err = dao_factory:get_migrations()
if err then
cutils.logger:error_exit(err)
elseif migrations then
cutils.logger:log(string.format(
"Executed migrations for %s on keyspace: %s:\n%s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace),
table.concat(migrations, ", ")
))
else
cutils.logger:log(string.format(
"No migrations have been run yet for %s on keyspace: %s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace)
))
end

elseif args.command == "migrations:up" then

cutils.logger:log(string.format(
"Migrating %s keyspace: %s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace))
)

migrations:migrate(function(migration, err)
if err then
cutils.logger:error_exit(err)
elseif migration then
cutils.logger:success("Migrated up to: "..cutils.colors.yellow(migration.name))
else
cutils.logger:success("Schema already up to date")
end
end)

elseif args.command == "migrations:down" then

cutils.logger:log(string.format(
"Rolling back %s keyspace: %s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace)
))

migrations:rollback(function(migration, err)
if err then
cutils.logger:error_exit(err)
elseif migration then
cutils.logger:success("Rollbacked to: "..cutils.colors.yellow(migration.name))
else
cutils.logger:success("No migration to rollback")
end
end)

elseif args.command == "migrations:reset" then

cutils.logger:log(string.format(
"Reseting %s keyspace: %s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace))
)

migrations:reset(function(migration, err)
if err then
cutils.logger:error_exit(err)
elseif migration then
cutils.logger:success("Rollbacked: "..cutils.colors.yellow(migration.name))
else
cutils.logger:success("Schema reseted")
end
end)

elseif args.command == "seed" then
if args.command == "seed" then

-- Drop if exists
local err = dao_factory:drop()
Expand Down
106 changes: 106 additions & 0 deletions src/cli/migrations.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env lua

local Migrations = require "kong.tools.migrations"
local constants = require "kong.constants"
local cutils = require "kong.cli.utils"
local IO = require "kong.tools.io"
local lapp = require("lapp")
local args = lapp(string.format([[
Kong datastore migrations.

Usage: kong migrations <command> [options]

Commands:
<command> (string) where <command> is one of:
list, up, down, reset

Options:
-c,--config (default %s) path to configuration file
]], constants.CLI.GLOBAL_KONG_CONF))

-- $ kong migrations
if args.command == "migrations" then
lapp.quit("Missing required <command>.")
end

local config_path = cutils.get_kong_config_path(args.config)
local _, dao_factory = IO.load_configuration_and_dao(config_path)
local migrations = Migrations(dao_factory, cutils.get_luarocks_install_dir())

if args.command == "list" then

local migrations, err = dao_factory:get_migrations()
if err then
cutils.logger:error_exit(err)
elseif migrations then
cutils.logger:log(string.format(
"Executed migrations for %s on keyspace %s:\n%s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace),
table.concat(migrations, ", ")
))
else
cutils.logger:log(string.format(
"No migrations have been run yet for %s on keyspace: %s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace)
))
end

elseif args.command == "up" then

cutils.logger:log(string.format(
"Migrating %s keyspace: %s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace))
)

migrations:migrate(function(migration, err)
if err then
cutils.logger:error_exit(err)
elseif migration then
cutils.logger:success("Migrated up to: "..cutils.colors.yellow(migration.name))
else
cutils.logger:success("Schema already up to date")
end
end)

elseif args.command == "down" then

cutils.logger:log(string.format(
"Rolling back %s keyspace: %s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace)
))

migrations:rollback(function(migration, err)
if err then
cutils.logger:error_exit(err)
elseif migration then
cutils.logger:success("Rollbacked to: "..cutils.colors.yellow(migration.name))
else
cutils.logger:success("No migration to rollback")
end
end)

elseif args.command == "reset" then

cutils.logger:log(string.format(
"Reseting %s keyspace: %s",
cutils.colors.yellow(dao_factory.type),
cutils.colors.yellow(dao_factory._properties.keyspace))
)

migrations:reset(function(migration, err)
if err then
cutils.logger:error_exit(err)
elseif migration then
cutils.logger:success("Rollbacked: "..cutils.colors.yellow(migration.name))
else
cutils.logger:success("Schema reseted")
end
end)

else
lapp.quit("Invalid command: "..args.command)
end
4 changes: 2 additions & 2 deletions src/cli/quit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ local constants = require "kong.constants"
local cutils = require "kong.cli.utils"
local signal = require "kong.cli.utils.signal"
local args = require("lapp")(string.format([[
Graceful shutdown
Graceful shutdown. Stop the Kong instance running in the configured 'nginx_working_dir' directory.

Usage: kong stop [options]

Options:
-c,--config (default %s) configuration file
-c,--config (default %s) path to configuration file
]], constants.CLI.GLOBAL_KONG_CONF))

-- Check if running, will exit if not
Expand Down
6 changes: 4 additions & 2 deletions src/cli/reload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ local constants = require "kong.constants"
local cutils = require "kong.cli.utils"
local signal = require "kong.cli.utils.signal"
local args = require("lapp")(string.format([[
Gracefully reload Kong applying any configuration changes (including nginx)
Gracefully reload the Kong instance running in the configured 'nginx_working_dir'.

Any configuration change will be applied.

Usage: kong reload [options]

Options:
-c,--config (default %s) configuration file
-c,--config (default %s) path to configuration file
]], constants.CLI.GLOBAL_KONG_CONF))

signal.prepare_kong(args.config)
Expand Down
7 changes: 6 additions & 1 deletion src/cli/restart.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ local constants = require "kong.constants"
local cutils = require "kong.cli.utils"
local signal = require "kong.cli.utils.signal"
local args = require("lapp")(string.format([[
Restart the Kong instance running in the configured 'nginx_working_dir'.

Kong will be shutdown before restarting. For a zero-downtime reload
of your configuration, look at 'kong reload'.

Usage: kong restart [options]

Options:
-c,--config (default %s) configuration file
-c,--config (default %s) path to configuration file
]], constants.CLI.GLOBAL_KONG_CONF))

if signal.is_running(args.config) then
Expand Down
4 changes: 3 additions & 1 deletion src/cli/start.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ local constants = require "kong.constants"
local cutils = require "kong.cli.utils"
local signal = require "kong.cli.utils.signal"
local args = require("lapp")(string.format([[
Start Kong with given configuration. Kong will run in the configured 'nginx_working_dir' directory.

Usage: kong start [options]

Options:
-c,--config (default %s) configuration file
-c,--config (default %s) path to configuration file
]], constants.CLI.GLOBAL_KONG_CONF))

signal.prepare_kong(args.config)
Expand Down
Loading