support filtering without -b

This commit is contained in:
Pierre Carrier
2025-02-08 15:27:42 +01:00
parent 9adade7502
commit c491c6f62c

View File

@ -1,4 +1,3 @@
// Package main implements a command-line TOTP (Time-based One-Time Password) generator
package main package main
import ( import (
@ -101,7 +100,6 @@ func shouldShowHelp() bool {
} }
cfgPath := getConfigPath() cfgPath := getConfigPath()
if _, err := os.Stat(cfgPath); os.IsNotExist(err) { if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
// Show help if no config exists unless the user is adding a new account.
if len(os.Args) > 2 { if len(os.Args) > 2 {
if cmd := findCommand(os.Args[2]); cmd != nil && cmd.name == "add" { if cmd := findCommand(os.Args[2]); cmd != nil && cmd.name == "add" {
return false return false
@ -128,7 +126,6 @@ func main() {
accountName = os.Args[1] accountName = os.Args[1]
} }
// Handle commands or show all codes
var cmd *command var cmd *command
if len(os.Args) > 2 { if len(os.Args) > 2 {
cmd = findCommand(os.Args[2]) cmd = findCommand(os.Args[2])
@ -137,15 +134,13 @@ func main() {
if cmd != nil { if cmd != nil {
var urls []*otpauth.URL var urls []*otpauth.URL
if cmd.name != "add" { if cmd.name != "add" {
// Only load existing URLs if we're not adding a new account
urls = getUrls() urls = getUrls()
} }
cmd.handler(accountName, urls) cmd.handler(accountName, urls)
return return
} }
// Default behavior printCodes(getUrls(), accountName)
printAllCodes(getUrls())
} }
func getPassword() ([]byte, error) { func getPassword() ([]byte, error) {
@ -249,7 +244,7 @@ func addCode(accountName string) {
if err := validateAndSaveConfig(cfgPath, password, newConfig, accountName); err != nil { if err := validateAndSaveConfig(cfgPath, password, newConfig, accountName); err != nil {
log.Fatalf("Saving config: %v", err) log.Fatalf("Saving config: %v", err)
} }
cachedRaw = nil // Invalidate cache cachedRaw = nil
cachedUrls = nil cachedUrls = nil
} }
@ -358,12 +353,15 @@ func handleEncryption(cfgPath string) ([]byte, error) {
return pass, nil return pass, nil
} }
func printAllCodes(urls []*otpauth.URL) { func printCodes(urls []*otpauth.URL, filter string) {
tw := tabwriter.NewWriter(os.Stdout, 0, 8, 1, ' ', 0) tw := tabwriter.NewWriter(os.Stdout, 0, 8, 1, ' ', 0)
if _, err := fmt.Fprintln(tw, "\tprev\tcurr\tnext\tprog"); err != nil { if _, err := fmt.Fprintln(tw, "\tprev\tcurr\tnext\tprog"); err != nil {
log.Fatalf("Writing header: %v", err) log.Fatalf("Writing header: %v", err)
} }
for _, url := range urls { for _, url := range urls {
if filter != "" && !matchAccount(filter, url.Account) {
continue
}
prev, curr, next, err := gauth.Codes(url) prev, curr, next, err := gauth.Codes(url)
if err != nil { if err != nil {
log.Fatalf("Generating codes for %q: %v", url.Account, err) log.Fatalf("Generating codes for %q: %v", url.Account, err)