all repos — pls @ a885644070ae9ead336ad1a5ca7ba6e8fb5dac4a

A polite but determined task runner.

src/pls.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
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
import 
  os,
  parseopt,
  strutils,
  sequtils,
  pegs,
  tables

import
  plspkg/config

type ConfigParseError = ref object of ValueError
type RuntimeError = ref object of ValueError

let USAGE* = """$1 v$2 - $3
(c) 2021 $4

Usage:
  pls <action> [<thing>]           Executes <action> (on <thing>).

Options:
  --help,    -h           Displays this message.
  --actions, -a           Display all known actions.
  --things,  -t           Display all known things.
  --version, -v           Displays the version of the application.
""" % [pkgTitle, pkgVersion, pkgDescription, pkgAuthor]

let placeholder = peg"'{{' {[a-zA-Z0-9._-]+} '}}'"
let id = peg"^[a-z0-9][a-zA-Z0-9._-]+$"
let def = peg"^[a-z0-9][a-zA-Z0-9._-]+ ('+' [a-z0-9][a-zA-Z0-9._-]+)*$"

var DATA = newTable[string, TableRef[string, TableRef[string, string]]]()
DATA["actions"] = newTable[string, TableRef[string, string]]()
DATA["things"] = newTable[string, TableRef[string, string]]()

const defaultConfig = """
actions:
  # Define actions here
things:
  # Define things here
"""

var CONFIG: string

if defined(windows):
  CONFIG = getenv("USERPROFILE") / "pls.yml"
else:
  CONFIG = getenv("HOME") / "pls.yml"

# Helper Methods

proc parseProperty(line: string, index: int): tuple[name: string, value: string] =
  let parts = line.split(":")
  if parts.len < 2:
    raise ConfigParseError(msg: "Line $1 - Invalid property." % $index)
  result.name = parts[0].strip
  result.value = parts[1..parts.len-1].join(":").strip

proc load(cfg: string): void =
  var section = ""
  var itemId = ""
  var indent = 0
  var count = 0
  for l in cfg.lines:
    count += 1
    if l.startsWith("    "):
      var line = l.strip
      var obj = ""
      if line.len == 0:
        raise ConfigParseError(msg: "Line $1 - Invalid empty line within item." % $count)
      if line[0] == '#':
        # comment
        continue
      if section == "actions":
        obj = "action ID"
      if section == "things":
        obj = "property name"
      if l.strip(true, false).len < l.strip(false, true).len-4:
        raise ConfigParseError(msg: "Line $1 - Invalid $2 indentation, expected 4 spaces." % [$count, obj])
      if section == "" or indent == 0:
        raise ConfigParseError(msg: "Line $1 - Invalid $2 indentation." % [$count, obj])
      if itemId == "":
        raise ConfigParseError(msg: "Line $1 - Invalid $2 indentation (not within an item)." % [$count, obj])
      let p = parseProperty(line, count)
      if (section == "actions" and not p.name.match(def)) or (section == "things" and not p.name.match(id)):
        raise ConfigParseError(msg: "Line $1 - Invalid $2 '$3'" % [$count, obj, p.name])
      DATA[section][itemId][p.name] = p.value
      indent = 4
      continue
    if l.startsWith("  "):
      var line = l.strip
      var obj = ""
      if line.len == 0:
        raise ConfigParseError(msg: "Line $1 - Invalid empty line within section." % $count)
      if line[0] == '#':
        # comment
        continue
      if section == "actions":
        obj = "action"
      if section == "things":
        obj = "thing"
      if l.strip(true, false).len < l.strip(false, true).len-2:
        raise ConfigParseError(msg: "Line $1 - Invalid $2 indentationn, expected 2 spaces." % [$count, obj])
      if section == "":
        raise ConfigParseError(msg: "Line $1 - Invalid $2 indentation." % [$count, obj])
      if line[line.len-1] != ':' or line == ":":
        raise ConfigParseError(msg: "Line $1 - Invalid $2 identifier." % [$count, obj])
      itemId = line[0..line.len-2]
      if not itemId.match(id):
        raise ConfigParseError(msg: "Line $1 - Invalid $2 identifier '$3'." % [$count, obj, itemId])
      # Start new item
      DATA[section][itemId] = newTable[string, string]()
      indent = 2
      continue
    if l == "":
      itemId = ""
      continue
    if l == "actions:":
      if section == "actions":
        raise ConfigParseError(msg: "Line $1 - Duplicated 'actions' section." % $count)
      section = "actions"
      continue
    if l == "things:":
      if section == "things":
        raise ConfigParseError(msg: "Line $1 - Duplicated 'things' section." % $count)
      section = "things"
      continue
    if l.strip.startsWith("#"):
      # comment
      continue
    else:
      raise ConfigParseError(msg: "Line $1 - Invalid line." % $count)

proc lookupTask(action: string, props: seq[string]): string =
  result = ""
  if not DATA["actions"].hasKey(action):
    raise RuntimeError(msg: "Action '$1' not found" % action)
  var defs = DATA["actions"][action]
  var score = 0
  # Cycle through action definitions
  for key, val in defs.pairs:
    var params = key.split("+")
    # Check if all params are available
    var match = params.all do (x: string) -> bool:
      props.contains(x)
    if match and params.len > score:
      score = params.len
      result = val

proc resolvePlaceholder(ident, initialThing: string): string = 
  var id = ident
  var thing = initialThing
  let parts = id.split(".")
  if parts.len > 2:
    raise RuntimeError(msg: "Invalid placeholder '$1'." % id)
  elif parts.len == 2:
    thing = parts[0]
    id = parts[1]
  if not DATA["things"].hasKey(thing):
    raise RuntimeError(msg: "Unable to access thing '$1' in placeholder '$2'." % [thing, ident])
  if not DATA["things"][thing].hasKey(id):
    raise RuntimeError(msg: "Unable to access property '$1' in thing '$2' within placeholder '$3'." % [id, thing, ident])
  result = DATA["things"][thing][id]
  result = result.replace(placeholder) do (m: int, n: int, c: openArray[string]) -> string:
    return resolvePlaceholder(c[0], thing) 

proc execute*(action, thing: string): int {.discardable.} =
  if not DATA["things"].hasKey(thing):
    raise RuntimeError(msg: "Thing '$1' not found. Nothing to do." % thing)
  let props = DATA["things"][thing]
  var keys = newSeq[string](0)
  for key, val in props.pairs:
    keys.add key
  var cmd = lookupTask(action, keys)
  if cmd != "":
    cmd = cmd.replace(placeholder) do (m: int, n: int, c: openArray[string]) -> string:
      return resolvePlaceholder(c[0], thing)
    echo "\n[pls]-> $1\n" % cmd
    result = execShellCmd cmd

### MAIN ###

if not CONFIG.fileExists:
  CONFIG.writeFile(defaultConfig)

try:
  CONFIG.load()
except:
  echo "(!) Unable to parse pls.yml file: $1" % getCurrentExceptionMsg()
  quit(1)

var args = newSeq[string](0)

for kind, key, val in getopt():
  case kind:
    of cmdArgument:
      args.add key 
    of cmdLongOption, cmdShortOption:
      case key:
        of "help", "h":
          echo USAGE
          quit(0)
        of "version", "v":
          echo pkgVersion
          quit(0)
        of "actions", "a":
          for action, props in DATA["actions"].pairs:
            echo "\n$1:" % action
            for key, val in props.pairs:
              echo "  $1: $2" % [key, val]
          quit(0)
        of "things", "t":
          for thing, props in DATA["things"].pairs:
            echo "\n$1:" % thing
            for key, val in props.pairs:
              echo "  $1: $2" % [key, val]
          quit(0)
        else:
          discard
    else:
      discard

if args.len == 0:
  echo USAGE 
  quit(0)
elif args.len < 1:
  echo USAGE
  quit(0)
elif args.len < 2:
  if DATA["things"].len == 0:
    echo "(!) No targets defined - nothing to do."
    quit(0)
  for key in DATA["things"].keys:
    execute(args[0], key) 
else:
  try:
    execute(args[0], args[1]) 
  except:
    echo "(!) " & getCurrentExceptionMsg()