Use commandeer

Closes #1
This commit is contained in:
Christine Dodrill 2016-02-04 17:39:13 -08:00
parent 4bec75a3b6
commit bb3b1da25b
5 changed files with 28 additions and 65 deletions

View File

@ -1,17 +1,13 @@
import otp import otp
import os import os
const codeUsage = const codeUsage* =
"""Usage: totptool code <name> """Usage: totptool code <name>
This will generate a TOTP code for the given secret This will generate a TOTP code for the given secret
name in the secret store. name in the secret store.
""" """
proc help*() =
echo codeUsage
quit 2
proc code*(root: string, args: seq[string]) = proc code*(root: string, args: seq[string]) =
if args.len != 1: if args.len != 1:
quit "I need a machine name" quit "I need a machine name"

View File

@ -2,16 +2,12 @@ import algorithm
import os import os
import strutils import strutils
const lsUsage = const lsUsage* =
"""Usage: totptool ls """Usage: totptool ls
Shows all of the totp codes available. Shows all of the totp codes available.
""" """
proc help*() =
echo lsUsage
quit 2
proc ls*(root: string, args: seq[string]) = proc ls*(root: string, args: seq[string]) =
for file in walkFiles root / "*": for file in walkFiles root / "*":
var name = file.split("/") var name = file.split("/")

View File

@ -7,7 +7,7 @@ import random
import strutils import strutils
import uri import uri
const newSecretUsage = const newSecretUsage* =
"""Usage: totptool newSecret <name> """Usage: totptool newSecret <name>
This will newSecreterate a new secret and recovery codes This will newSecreterate a new secret and recovery codes
@ -16,15 +16,10 @@ to your configured secret store and make it
available for `totptool code` instantly. available for `totptool code` instantly.
""" """
proc help*() =
echo newSecretUsage
quit 2
proc newSecret*(root: string, args: seq[string]) = proc newSecret*(root: string, args: seq[string]) =
if args.len != 1: if args.len != 1:
quit "I need a machine name" quit "I need a machine name"
var var
res = generateUUID()[0 .. 19] res = generateUUID()[0 .. 19]
body: string body: string

View File

@ -1,5 +1,5 @@
import commandeer
import os import os
import parseopt2
# Subcommands # Subcommands
import cmds/code import cmds/code
@ -11,7 +11,7 @@ const usageHelp =
Options: Options:
-h --help Show this screen -h --help Show this screen
-r --root:path Use path as the totp root for secrets -r --root=path Use path as the totp root for secrets
-v --version Show version of totptool -v --version Show version of totptool
Subcommands: Subcommands:
@ -38,55 +38,31 @@ proc writeVersion() =
echo gitlog echo gitlog
quit 1 quit 1
when isMainModule: commandline:
var subcommand codecmd, "code":
totpRoot: string = getHomeDir() / "life" / "crypto" / "totp" arguments name, string
command: string exitoption "help", "h", codeUsage
args: seq[string]
for kind, key, val in getopt(): subcommand newSecretcmd, "newSecret":
case kind arguments newSecretName, string
of cmdArgument: exitoption "help", "h", newSecretUsage
if command == nil:
command = key
else:
args = args & key
of cmdLongOption, cmdShortOption: subcommand lscmd, "ls":
case key exitoption "help", "h", lsUsage
of "help", "h":
case command
of "code":
code.help()
of "newSecret":
newSecret.help()
of "ls":
ls.help()
else:
writeHelp()
of "version", "v": writeVersion() option root, string, "root", "r", getHomeDir() / "life" / "crypto" / "totp"
of "root", "r":
totpRoot = val
else: discard
of cmdEnd: assert(false) # cannot happen exitoption "help", "h", usageHelp
errormsg "Invalid state or input"
if command == nil: if not existsDir(root):
writeHelp() quit "Cannot use key store " & root
else: discard
if not existsDir(totpRoot): if codecmd:
totpRoot.createDir code.code(root, name)
echo "Created totp root " & totpRoot elif newSecretcmd:
newSecret.newSecret(root, newSecretName)
case command elif lscmd:
of "code": ls(root, @[])
code(totpRoot, args) else:
of "newSecret": quit usageHelp
newSecret(totpRoot, args)
of "ls":
ls(totpRoot, args)
else:
echo "Command " & command & " not found."
writeHelp()

View File

@ -3,6 +3,6 @@ author = "Xena <me@christine.website>"
description = "TOTP generation tool" description = "TOTP generation tool"
license = "MIT" license = "MIT"
bin = @["totptool"] bin = @["totptool"]
requires "nim >= 0.10.0", "otp", "nuuid", "random" requires "nim >= 0.13.0", "otp", "nuuid", "random", "base32", "commandeer"
# vim: set ft=nim ts=2 sw=2 tw=0 et : # vim: set ft=nim ts=2 sw=2 tw=0 et :