Permalink
Browse files

2fa: add -clip flag to copy code to clipboard

Fixes #5.
  • Loading branch information...
samdfonseca authored and rsc committed Jan 3, 2018
1 parent 3f62f54 commit de5e8c711e4667c392341d5fdb0dc0b8a4e7771f
Showing with 20 additions and 5 deletions.
  1. +2 −1 README.md
  2. +18 −4 main.go
View
@@ -22,7 +22,8 @@ By default the new key generates 6-digit codes; the `-7` and `-8` flags select
`2fa -list` lists the names of all the keys in the keychain.
`2fa name` prints a two-factor authentication code from the key with the
given name.
given name. If `-clip` is specified, `2fa` also copies to the code to the system
clipboard.
With no arguments, `2fa` prints two-factor authentication codes from all
known time-based keys.
View
22 main.go
@@ -8,7 +8,7 @@
//
// 2fa -add [-7] [-8] [-hotp] name
// 2fa -list
// 2fa name
// 2fa [-clip] name

This comment has been minimized.

Show comment
Hide comment
@dmitshur

dmitshur Jan 4, 2018

Is it intentional that you didn't update the copy of this line in README.md?

@dmitshur

dmitshur Jan 4, 2018

Is it intentional that you didn't update the copy of this line in README.md?

//
// “2fa -add name” adds a new key to the 2fa keychain with the given name.
// It prints a prompt to standard error and reads a two-factor key from standard input.
@@ -23,7 +23,8 @@
// “2fa -list” lists the names of all the keys in the keychain.
//
// “2fa name” prints a two-factor authentication code from the key with the
// given name.
// given name. If “-clip” is specified, 2fa also copies the code to the system
// clipboard.
//
// With no arguments, 2fa prints two-factor authentication codes from all
// known time-based keys.
@@ -78,6 +79,8 @@ import (
"strings"
"time"
"unicode"
"github.com/atotto/clipboard"
)
var (
@@ -86,13 +89,14 @@ var (
flagHotp = flag.Bool("hotp", false, "add key as HOTP (counter-based) key")
flag7 = flag.Bool("7", false, "generate 7-digit code")
flag8 = flag.Bool("8", false, "generate 8-digit code")
flagClip = flag.Bool("clip", false, "copy code to the clipboard")
)
func usage() {
fmt.Fprintf(os.Stderr, "usage:\n")
fmt.Fprintf(os.Stderr, "\t2fa -add [-7] [-8] [-hotp] keyname\n")
fmt.Fprintf(os.Stderr, "\t2fa -list\n")
fmt.Fprintf(os.Stderr, "\t2fa keyname\n")
fmt.Fprintf(os.Stderr, "\t2fa [-clip] keyname\n")
os.Exit(2)
}
@@ -112,6 +116,9 @@ func main() {
return
}
if flag.NArg() == 0 && !*flagAdd {
if *flagClip {
usage()
}
k.showAll()
return
}
@@ -123,6 +130,9 @@ func main() {
log.Fatal("name must not contain spaces")
}
if *flagAdd {
if *flagClip {
usage()
}
k.add(name)
return
}
@@ -287,7 +297,11 @@ func (c *Keychain) code(name string) string {
}
func (c *Keychain) show(name string) {
fmt.Printf("%s\n", c.code(name))
code := c.code(name)
if *flagClip {
clipboard.WriteAll(code)
}
fmt.Printf("%s\n", code)
}
func (c *Keychain) showAll() {

0 comments on commit de5e8c7

Please sign in to comment.