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

This commit is contained in:
Dmitry Pyzhov
2023-05-25 23:59:45 +04:00
committed by GitHub
parent 64809cb4d1
commit 2ce9fbe9b4
3 changed files with 54 additions and 7 deletions

View File

@ -36,6 +36,13 @@ Usage
Github 911264 548790 784099 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`. - `gauth` is convenient to use in `watch`.
$ watch -n1 gauth $ watch -n1 gauth

View File

@ -10,11 +10,40 @@ import (
"syscall" "syscall"
"text/tabwriter" "text/tabwriter"
"github.com/creachadair/otp/otpauth"
"github.com/pcarrier/gauth/gauth" "github.com/pcarrier/gauth/gauth"
"golang.org/x/term" "golang.org/x/term"
) )
func main() { 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") cfgPath := os.Getenv("GAUTH_CONFIG")
if cfgPath == "" { if cfgPath == "" {
user, err := user.Current() user, err := user.Current()
@ -34,6 +63,23 @@ func main() {
log.Fatalf("Decoding configuration file: %v", err) 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 _, progress := gauth.IndexNow() // TODO: do this per-code
tw := tabwriter.NewWriter(os.Stdout, 0, 8, 1, ' ', 0) tw := tabwriter.NewWriter(os.Stdout, 0, 8, 1, ' ', 0)
@ -48,9 +94,3 @@ func main() {
tw.Flush() tw.Flush()
fmt.Printf("[%-29s]\n", strings.Repeat("=", progress)) fmt.Printf("[%-29s]\n", strings.Repeat("=", progress))
} }
func getPassword() ([]byte, error) {
fmt.Printf("Encryption password: ")
defer fmt.Println()
return term.ReadPassword(int(syscall.Stdin))
}