Add ability to request only one code for storing it in a clipboard ()

This commit is contained in:
Dmitry Pyzhov 2023-05-25 23:59:45 +04:00 committed by GitHub
parent 64809cb4d1
commit 2ce9fbe9b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 7 deletions

@ -36,6 +36,13 @@ Usage
Github 911264 548790 784099
[======= ]
- Run `gauth KEYNAME` to print a specific key with progress bar.
- Run `gauth KEYNAME -b` to print a bare current key.
$ gauth Google -b
477615
- `gauth` is convenient to use in `watch`.
$ watch -n1 gauth

@ -10,11 +10,40 @@ import (
"syscall"
"text/tabwriter"
"github.com/creachadair/otp/otpauth"
"github.com/pcarrier/gauth/gauth"
"golang.org/x/term"
)
func main() {
accountName := ""
isBareCode := false
if len(os.Args) > 1 {
accountName = os.Args[1]
}
if len(os.Args) > 2 {
if os.Args[2] == "-b" || os.Args[2] == "-bare" {
isBareCode = true
}
}
urls := getUrls()
if isBareCode && accountName != "" {
printBareCode(accountName, urls)
} else {
printAllCodes(urls)
}
}
func getPassword() ([]byte, error) {
fmt.Printf("Encryption password: ")
defer fmt.Println()
return term.ReadPassword(int(syscall.Stdin))
}
func getUrls() []*otpauth.URL {
cfgPath := os.Getenv("GAUTH_CONFIG")
if cfgPath == "" {
user, err := user.Current()
@ -34,6 +63,23 @@ func main() {
log.Fatalf("Decoding configuration file: %v", err)
}
return urls
}
func printBareCode(accountName string, urls []*otpauth.URL) {
for _, url := range urls {
if strings.EqualFold(strings.ToLower(accountName), strings.ToLower(url.Account)) {
_, curr, _, err := gauth.Codes(url)
if err != nil {
log.Fatalf("Generating codes for %q: %v", url.Account, err)
}
fmt.Print(curr)
break
}
}
}
func printAllCodes(urls []*otpauth.URL) {
_, progress := gauth.IndexNow() // TODO: do this per-code
tw := tabwriter.NewWriter(os.Stdout, 0, 8, 1, ' ', 0)
@ -48,9 +94,3 @@ func main() {
tw.Flush()
fmt.Printf("[%-29s]\n", strings.Repeat("=", progress))
}
func getPassword() ([]byte, error) {
fmt.Printf("Encryption password: ")
defer fmt.Println()
return term.ReadPassword(int(syscall.Stdin))
}

2
go.sum

@ -4,4 +4,4 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=