Do not check time/refresh TOTP every second - calculate time till next 30-second interval, instead

This commit is contained in:
2024-03-18 19:17:47 -04:00
parent ca14abbae9
commit 0687ce0954
2 changed files with 13 additions and 13 deletions
+1 -1
View File
@@ -77,7 +77,7 @@ func main() {
case "note", "-n": case "note", "-n":
field = 3 field = 3
case "totp", "-t": case "totp", "-t":
fmt.Println("TOTP code copied to clipboard - until this process is closed, your clipboard will be kept up to date with the current TOTP code") fmt.Println("TOTP code will be copied to clipboard - your clipboard will be kept up to date with the current TOTP code until this process is closed")
field = 5 // TODO Update field after removed from notes (breaking sshyp entry compatibility) field = 5 // TODO Update field after removed from notes (breaking sshyp entry compatibility)
default: default:
cli.HelpCopy() cli.HelpCopy()
+12 -12
View File
@@ -28,19 +28,19 @@ func CopyArgument(targetLocation string, field int, executableName string) {
copySubject = decryptedEntry[field] copySubject = decryptedEntry[field]
} else { // TOTP mode } else { // TOTP mode
var err error var err error
var currentSecond int // track current second for TOTP code refresh var copied bool // track whether the TOTP code has been copied to the clipboard for the first time
var copied bool // track whether the TOTP code has been copied to the clipboard for the first time for { // keep field copied to clipboard, refresh on 30-second intervals
for { // keep field copied to clipboard, refresh on 30-second intervals copySubject, err = totp.GenerateCode(decryptedEntry[5], time.Now())
currentSecond = time.Now().Second() if err != nil {
if currentSecond == 0 || currentSecond == 30 || !copied { fmt.Println(AnsiError + "Error generating TOTP code" + AnsiReset)
copySubject, err = totp.GenerateCode(decryptedEntry[5], time.Now()) os.Exit(1)
if err != nil {
fmt.Println(AnsiError + "Error generating TOTP code" + AnsiReset)
os.Exit(1)
}
copyField(copySubject, "")
} }
time.Sleep(1 * time.Second) copyField(copySubject, "")
if !copied {
copied = true
}
// sleep until next 30-second interval
time.Sleep(time.Duration(30-(time.Now().Second()%30)) * time.Second)
} }
} }
} else { } else {