totptool/totptool.nim

86 lines
1.6 KiB
Nim
Raw Normal View History

2015-09-15 01:49:40 +00:00
import os
import parseopt2
# Subcommands
import cmds/code
import cmds/gen
2015-09-15 02:22:11 +00:00
import cmds/ls
2015-09-15 01:49:40 +00:00
const usageHelp =
"""Usage: totptool [options] <command> [args]
Options:
-h --help Show this screen
2015-09-15 02:10:07 +00:00
-r --root:path Use path as the totp root for secrets
2015-09-15 01:49:40 +00:00
-v --version Show version of totptool
Subcommands:
code <name>
Generate a two-factor auth code for <name>
gen <name>
Generate a new two-factor auth secret for
<name>
ls
List all the names of the secrets that
totptool tracks
"""
proc writeHelp() =
echo usageHelp
quit 2
proc writeVersion() =
const
gitlog = staticExec "git log -1"
echo gitlog
quit 1
var
totpRoot: string = getHomeDir() / "life" / "crypto" / "totp"
command: string
args: seq[string]
for kind, key, val in getopt():
case kind
of cmdArgument:
if command == nil:
command = key
else:
args = args & key
of cmdLongOption, cmdShortOption:
case key
of "help", "h":
case command
2015-09-15 02:22:11 +00:00
of "code": code.help()
of "gen": gen.help()
of "ls": ls.help()
2015-09-15 01:49:40 +00:00
else:
writeHelp()
of "version", "v": writeVersion()
of "root", "r":
totpRoot = val
else: discard
of cmdEnd: assert(false) # cannot happen
if command == nil:
writeHelp()
else: discard
if not existsDir(totpRoot):
totpRoot.createDir
echo "Created totp root " & totpRoot
case command
2015-09-15 02:22:11 +00:00
of "code": code totpRoot, args
of "gen": gen totpRoot, args
of "ls": ls totpRoot, args
2015-09-15 01:49:40 +00:00
else:
echo "Command " & command & " not found."
writeHelp()