From bb3b1da25b8704afb78e2446c53c0cc31b6ae9e7 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Thu, 4 Feb 2016 17:39:13 -0800 Subject: [PATCH] Use commandeer Closes #1 --- cmds/code.nim | 6 +--- cmds/ls.nim | 6 +--- cmds/newSecret.nim | 7 +---- totptool.nim | 72 ++++++++++++++++------------------------------ totptool.nimble | 2 +- 5 files changed, 28 insertions(+), 65 deletions(-) diff --git a/cmds/code.nim b/cmds/code.nim index 313ca8f..433734e 100644 --- a/cmds/code.nim +++ b/cmds/code.nim @@ -1,17 +1,13 @@ import otp import os -const codeUsage = +const codeUsage* = """Usage: totptool code 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" diff --git a/cmds/ls.nim b/cmds/ls.nim index 58f0520..5a42bb1 100644 --- a/cmds/ls.nim +++ b/cmds/ls.nim @@ -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("/") diff --git a/cmds/newSecret.nim b/cmds/newSecret.nim index c3ed67f..5ba3f47 100644 --- a/cmds/newSecret.nim +++ b/cmds/newSecret.nim @@ -7,7 +7,7 @@ import random import strutils import uri -const newSecretUsage = +const newSecretUsage* = """Usage: totptool newSecret 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 diff --git a/totptool.nim b/totptool.nim index b4272a2..6f9c897 100644 --- a/totptool.nim +++ b/totptool.nim @@ -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 diff --git a/totptool.nimble b/totptool.nimble index 4585abc..5bf7d74 100644 --- a/totptool.nimble +++ b/totptool.nimble @@ -3,6 +3,6 @@ author = "Xena " 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 :