totptool/totptool.nim

86 lines
1.6 KiB
Nim

import os
import parseopt2
# Subcommands
import cmds/code
import cmds/gen
import cmds/ls
const usageHelp =
"""Usage: totptool [options] <command> [args]
Options:
-h --help Show this screen
-r --root:path Use path as the totp root for secrets
-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
of "code": code.help()
of "gen": gen.help()
of "ls": ls.help()
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
of "code": code totpRoot, args
of "gen": gen totpRoot, args
of "ls": ls totpRoot, args
else:
echo "Command " & command & " not found."
writeHelp()