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

View File

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

View File

@ -7,7 +7,7 @@ import random
import strutils
import uri
const newSecretUsage =
const newSecretUsage* =
"""Usage: totptool newSecret <name>
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.
"""
proc help*() =
echo newSecretUsage
quit 2
proc newSecret*(root: string, args: seq[string]) =
if args.len != 1:
quit "I need a machine name"
var
res = generateUUID()[0 .. 19]
body: string

View File

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

View File

@ -3,6 +3,6 @@ author = "Xena <me@christine.website>"
description = "TOTP generation tool"
license = "MIT"
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 :