Skip to content

Commit

Permalink
Case-insensitive matching
Browse files Browse the repository at this point in the history
  • Loading branch information
pepa65 committed Nov 17, 2024
1 parent b271594 commit c6e42d2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<img src="https://raw.githubusercontent.com/pepa65/twofat/master/twofat.png" width="96" alt="twofat icon" align="right">

## Manage TOTPs from CLI
* **v2.0.7**
* **v2.1.0**
* Repo: [github.com/pepa65/twofat](https://github.com/pepa65/twofat)
* After: [github.com/slandx/tfat](https://github.com/slandx/tfat)
* Contact: github.com/pepa65
Expand Down Expand Up @@ -62,9 +62,9 @@ When output is redirected, only pertinent plain text is sent to Stdout.
* Data file: ~/.twofat.enc (default, depends on the binary's name)
* Usage: twofat [COMMAND] [ -d | --datafile DATAFILE ]
== COMMAND:
[ show | view ] [REGEX]
[ show | view ] [[-c|--case] REGEX]
Display all TOTPs with NAMEs [matching REGEX] (show/view is optional).
list | ls [REGEX]
list | ls [[-c|--case] REGEX]
List all NAMEs [matching REGEX].
add | insert | entry NAME [TOTP-OPTIONS] [ -f | --force ] [SECRET]
Add a new entry NAME with SECRET (queried when not given).
Expand All @@ -84,6 +84,7 @@ clip | copy | cp NAME Put TOTP of entry NAME onto the clipboard.
password | passwd | pw Change datafile encryption password.
version | --version | -V Show version.
help | --help | -h Show this help text.
== REGEX: Optional, case-insensitive matching (unless if -c/--case given)
== TOTP-OPTIONS:
-s | --size LENGTH TOTP length: 5-8 (default: 6)
-a | --algorithm HASH Hash algorithm: SHA1/SHA256/SHA512 (default: SHA1)
Expand Down
23 changes: 17 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

const (
version = "2.0.7"
version = "2.1.0"
maxNameLen = 20
period = 30
)
Expand Down Expand Up @@ -638,7 +638,7 @@ func importEntries(filename string) {
func main() {
self, cmd, regex, datafile, name, nname, file := "", "", "", "", "", "", ""
var secret []byte
datafileflag, sizeflag, algorithmflag, size, algorithm, ddash := 0, 0, 0, "6", "SHA1", false
datafileflag, sizeflag, algorithmflag, size, algorithm, ddash, cas := 0, 0, 0, "6", "SHA1", false, false
o, _ := os.Stdout.Stat()
if (o.Mode() & os.ModeCharDevice) == os.ModeCharDevice {
redirected = false
Expand Down Expand Up @@ -669,6 +669,10 @@ func main() {
ddash = true
continue
}
if arg == "-c" || arg == "--case" {
cas = true
continue
}
if arg == "-f" || arg == "--force" {
force = true
continue
Expand Down Expand Up @@ -704,9 +708,9 @@ func main() {
return

case "show", "view":
cmd = "s" // [REGEX]
cmd = "s" // [REGEX] [-c/--case]
case "list", "ls":
cmd = "l" // [REGEX]
cmd = "l" // [REGEX] [-c/--case]
case "rename", "move", "mv":
cmd = "m" // NAME NEWNAME [-f/--force]
case "add", "insert", "entry":
Expand Down Expand Up @@ -816,6 +820,9 @@ func main() {
}
}
// All arguments have been parsed, check
if cas && cmd != "s" && cmd != "l" {
usage("flag -c/--case can only be given on show/view and list/ls commands")
}
if datafileflag == 1 {
usage("flag -d/--datafile needs a DATAFILE as argument")
}
Expand All @@ -828,6 +835,9 @@ func main() {
if datafile != "" {
dbPath = datafile
}
if regex != "" && !cas {
regex = "(?i)"+regex
}
switch cmd {
case "", "s":
showTotps(regex)
Expand Down Expand Up @@ -885,9 +895,9 @@ func usage(err string) {
blue + "Datafile" + def + ": " + magenta + dbPath + def + " (default, depends on the binary's name)\n* " +
blue + "Usage" + def + ": " + magenta + self + def + " [" + green + "COMMAND" + def + "] [ " + yellow + "-d" + def + " | " + yellow + "--datafile " + cyan + " DATAFILE" + def + " ]\n" +
" == " + green + "COMMAND" + def + ":\n" +
"[ " + green + "show" + def + " | " + green + "view" + def + " ] [" + blue + "REGEX" + def + "]\n" +
"[ " + green + "show" + def + " | " + green + "view" + def + " ] [" + blue + "REGEX" + def + " [ " + yellow + "-c" + def + " | " + yellow + "--case" + def + " ]]\n" +
" Display all TOTPs with " + blue + "NAME" + def + "s [matching " + blue + "REGEX" + def + "] (" + green + "show" + def + "/" + green + "view" + def + " is optional).\n" +
green + "list" + def + " | " + green + "ls" + def + " [" + blue + "REGEX" + def + "]\n" +
green + "list" + def + " | " + green + "ls" + def + " [" + blue + "REGEX" + def + " [ " + yellow + "-c" + def + " | " + yellow + "--case" + def + " ]]\n" +
" List all " + blue + "NAME" + def + "s [matching " + blue + "REGEX" + def + "].\n" +
green + "add" + def + " | " + green + "insert" + def + " | " + green + "entry " + blue + "NAME" + def + " [" + yellow + "TOTP-OPTIONS" + def + "] [ " + yellow + "-f" + def + " | " + yellow + "--force" + def + " ] [" + blue + "SECRET" + def + "]\n" +
" Add a new entry " + blue + "NAME" + def + " with " + blue + "SECRET" + def + " (queried when not given).\n" +
Expand All @@ -907,6 +917,7 @@ func usage(err string) {
green + "password" + def + " | " + green + "passwd" + def + " | " + green + "pw" + def + " Change datafile encryption password.\n" +
green + "version" + def + " | " + green + "--version" + def + " | " + green + "-V" + def + " Show version.\n" +
green + "help" + def + " | " + green + "--help" + def + " | " + green + "-h" + def + " Show this help text.\n" +
" == " + blue + "REGEX" + def + ": Optional, case-insensitive matching (unless " + yellow + "-c" + def + "/" + yellow + "--case" + def + " is given).\n" +
" == " + yellow + "TOTP-OPTIONS" + def + ":\n" +
yellow + "-s" + def + " | " + yellow + "--size " + cyan + "LENGTH" + def + " TOTP length: " + cyan + "5" + def + "-" + cyan + "8" + def + " (default: " + cyan + "6" + def + ")\n" +
yellow + "-a" + def + " | " + yellow + "--algorithm " + cyan + "HASH" + def + " Hash algorithm: " + cyan + "SHA1" + def + "/" + cyan + "SHA256" + def + "/" + cyan + "SHA512" + def +" (default: " + cyan + "SHA1" + def + ")"
Expand Down

0 comments on commit c6e42d2

Please sign in to comment.