~mna/tulip-cli

tulip-cli/src/main.lua -rw-r--r-- 1.8 KiB
266f3ff6Martin Angers Release v0.0.5 2 years ago
                                                                                
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
local OptionParser = require 'optparse'
local VERSION = "0.0.5"

local help = string.format([[
tulip-cli v%s
Usage: tulip-cli CMD [<options>]

The following tulip commands are supported:

  $ tulip-cli init DIR
      Initialize environment for development of a tulip project in DIR,
      which is a path relative to the current working directory.

  $ tulip-cli db reset
      Drops and recreates the database, so that no migration has been
      applied yet. All data will be lost, use with care.

  $ tulip-cli db clean PREFIX
      Cleans the database by removing any databases and users that start
      with PREFIX. Useful if database copies are created for tests.

Options:

  -h, --help            Display this help and exit.
  --port=PORT           Expose the database on that port for the host
                        (default: 5432). Only used for the 'init' command.
  --prefix=PREFIX       Clean databases and users that have this prefix.
                        Only used for the 'db clean' command.
  -V, --version         Display the version and exit.
  -y, --yes             Skip confirmation of actions.
]], VERSION)

local cmds = require 'src.cmds'
local parser = OptionParser(help)

local defaults = {
  port = 5432,
}

return function(args)
  local arg, opts = parser:parse(args)
  if #arg == 0 then
    parser:opterr('the command is required')
    return
  end

  local cmd = table.remove(arg, 1)
  local f = cmds[cmd]
  if not f then
    parser:opterr(string.format('unknown command %q', cmd))
    return
  end

  -- set the default values for options
  if opts.port then
    opts.port = math.tointeger(opts.port)
    if not opts.port then
      parser:opterr('the --port option must be a number')
      return
    end
  end
  opts.port = opts.port or defaults.port

  return f(arg, opts, function(msg, ...)
    parser:opterr(string.format(msg, ...))
  end)
end