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

@ -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))
}