all repos — litestore @ 9cdae4f65883217af04ec93b9310bff1e5eafebc

A minimalist nosql document store.

lib/cli.nim

 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
import
  parseopt2,
  parsecfg,
  streams,
  strutils,
  logging
import
  types,
  utils

const cfgfile = "litestore.nimble".slurp
const favicon = "admin/favicon.ico".slurp

var 
  file*, address*, version*, appname*: string
  port*: int
  operation = opRun
  directory = ""
  readonly = false
  logLevelLabel = "INFO"
  logLevel = lvlInfo
  mount = false
  reset = false
  
var f = newStringStream(cfgfile)
if f != nil:
  var p: CfgParser
  open(p, f, "litestore.nimble")
  while true:
    var e = next(p)
    case e.kind
    of cfgEof:
      break
    of cfgKeyValuePair:
      case e.key:
        of "version":
          version = e.value
        of "appame":
          appname = e.value
        of "port":
          port = e.value.parseInt
        of "address":
          address = e.value
        of "file":
          file = e.value
        else:
          discard
    of cfgError:
      fail(1, "Configuration error.")
    else: 
      discard
  close(p)
else:
  fail(2, "Cannot process configuration file.")

let
  usage* = appname & " v" & version & " - Lightweight REST Document Store" & """
  (c) 2015 Fabio Cevasco

  Usage:
    LS [-p:<port> -a:<address>] [<file>] [--import:<directory> | --export:<directory> | --delete:<directory>] 

  Options:
    -a, --address       Specify address (default: 127.0.0.1).
    -d, --delete        Delete the previously-imported specified directory.
    --export            Export the previously-imported specified directory to the current directory.
    -h, --help          Display this message.
    --import            Import the specified directory (Store all its contents).
    -l, --log           Specify the log level: debug, info, warn, error, fatal, none (default: info)
    -p, --port          Specify port number (default: 9500).
    -r, --readonly      Allow only data retrieval operations.
    -m, --mount        Run server and mirror database changes to the specified directory on the filesystem.
    --reset             If --mount is specified, resets (deletes) any previously imported directory data.
    -v, --version       Display the program version.
"""

for kind, key, val in getOpt():
  case kind:
    of cmdLongOption, cmdShortOption:
      case key:
        of "address", "a":
          if val == "":
            fail(100, "Address not specified.")
          address = val
        of "port", "p":
          if val == "":
            fail(101, "Port not specified.")
          port = val.parseInt
        of "log", "l":
          if val == "":
            fail(102, "Log level not specified.")
          try:
            logLevelLabel = val.toUpper
            logLevel = logging.LevelNames.find(logLevelLabel).Level
          except:
            fail(103, "Invalid log level '$1'" % val)
        of "import":
          if val == "":
            fail(104, "Directory to import not specified.")
          operation = opImport
          directory = val
        of "mount", "m":
          if val == "":
            fail(104, "Directory to mount not specified.")
          operation = opRun
          directory = val
          mount = true
        of "reset":
          reset = true
        of "export":
          if val == "":
            fail(105, "Directory to export not specified.")
          operation = opExport
          directory = val
        of "delete", "d":
          operation = opDelete
          directory = val
        of "version", "v":
          echo version
          quit(0)
        of "help", "h":
          echo usage
          quit(0)
        of "readonly", "r":
          readonly = true
        else:
          discard
    of cmdArgument:
      file = key
    else:
      discard

var LS*: LiteStore

LS.port = port
LS.address = address
LS.operation = operation
LS.file = file
LS.directory = directory
LS.appversion = version
LS.readonly = readonly
LS.appname = appname
LS.favicon = favicon
LS.loglevel = logLevelLabel
LS.mount = mount
LS.reset = reset

# Initialize loggers

logging.level = logLevel
logging.handlers.add(newConsoleLogger(logLevel, "$date $time - "))
logging.handlers.add(newFileLogger("litestore.log.txt", fmAppend, logLevel, fmtStr = "$date $time - "))