all repos — litestore @ 5533c4d97b9d81537d7a923b76ae11691566e4c3

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
import
  parseopt2,
  strutils,
  strtabs
import
  logger,
  config,
  types,
  utils

const favicon = "admin/favicon.ico".slurp

var 
  operation = opRun
  directory:string = nil
  readonly = false
  logLevel = "warn"
  mount = false
  
let
  usage* = appname & " v" & version & " - Lightweight REST Document Store" & """
  (c) 2015 Fabio Cevasco

  Usage:
    litestore [command] [option1 option2 ...]

  Commands:
    run                 Start LiteStore server (default if no command specified).
    delete              Delete a previously-imported specified directory (requires -d).
    import              Import the specified directory into the datastore (requires -d).
    export              Export the previously-imported specified directory to the current directory (requires -d).
    optimize            Optimize search indexes.
    vacuum              Vacuum datastore.

  Options:
    -a, --address       Specify server address (default: 127.0.0.1).
    -d, --directory     Specify a directory to serve, import, export, delete, or mount.
    -h, --help          Display this message.
    -l, --log           Specify the log level: debug, info, warn, error, none (default: info)
    -m, --mount         Mirror database changes to the specified directory on the filesystem.
    -p, --port          Specify server port number (default: 9500).
    -r, --readonly      Allow only data retrieval operations.
    -s, --store         Specify a datastore file (default: data.db)
    -v, --version       Display the program version.
"""

for kind, key, val in getOpt():
  case kind:
    of cmdArgument:
      case key:
        of "run":
          operation = opRun
        of "import":
          operation = opImport
        of "export":
          operation = opExport
        of "delete":
          operation = opDelete
        of "optimize":
          operation = opOptimize
        of "vacuum":
          operation = opVacuum
        else:
          discard
    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 "store", "s":
          file = val
        of "log", "l":
          if val == "":
            fail(102, "Log level not specified.")
          case val:
            of "info":
              LOG.level = lvInfo
            of "warn":
              LOG.level = lvWarn
            of "debug":
              LOG.level = lvDebug
            of "error":
              LOG.level = lvError
            of "none":
              LOG.level = lvNone
            else:
              fail(103, "Invalid log level '$1'" % val)
          loglevel = val
        of "directory", "d":
          if val == "":
            fail(104, "Directory not specified.")
          directory = val
        of "mount", "m":
          mount = true
        of "version", "v":
          echo version
          quit(0)
        of "help", "h":
          echo usage
          quit(0)
        of "readonly", "r":
          readonly = true
        else:
          discard
    else:
      discard

# Validation

if directory == nil and (operation in [opDelete, opImport, opExport] or mount):
  fail(105, "Directory option not specified.")

LS.operation = operation
LS.address = address
LS.port = port
LS.file = file
LS.directory = directory
LS.readonly = readonly
LS.favicon = favicon
LS.loglevel = loglevel
LS.mount = mount