mirror of
https://github.com/rwinkhart/libmutton.git
synced 2026-08-27 20:36:29 -04:00
Return errors from TOTPCopier so it can optionally be called on the main thread
This commit is contained in:
+6
-3
@@ -30,7 +30,8 @@ func GenTOTP(secret string, time time.Time, forSteam bool) (string, error) {
|
|||||||
// TOTPCopier is meant to be run as a goroutine to keep
|
// TOTPCopier is meant to be run as a goroutine to keep
|
||||||
// the clipboard up-to-date with the latest TOTP token.
|
// the clipboard up-to-date with the latest TOTP token.
|
||||||
// Set oneTime to -1 for a one-time (non-continuous) TOTP copy.
|
// Set oneTime to -1 for a one-time (non-continuous) TOTP copy.
|
||||||
func TOTPCopier(secret string, oneTime int, errorChan chan<- error, done <-chan bool) {
|
// For oneTime mode, this function can be used normally (not as a goroutine).
|
||||||
|
func TOTPCopier(secret string, oneTime int, errorChan chan<- error, done <-chan bool) error {
|
||||||
var forSteam bool
|
var forSteam bool
|
||||||
if strings.HasPrefix(secret, "steam@") {
|
if strings.HasPrefix(secret, "steam@") {
|
||||||
secret = secret[6:]
|
secret = secret[6:]
|
||||||
@@ -42,23 +43,25 @@ func TOTPCopier(secret string, oneTime int, errorChan chan<- error, done <-chan
|
|||||||
token, err := GenTOTP(secret, currentTime, forSteam)
|
token, err := GenTOTP(secret, currentTime, forSteam)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errorChan <- err
|
errorChan <- err
|
||||||
|
return err // return for when not used as goroutine; should exit on error regardless
|
||||||
}
|
}
|
||||||
err = CopyString(true, token)
|
err = CopyString(true, token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errorChan <- err
|
errorChan <- err
|
||||||
|
return err // return for when not used as goroutine; should exit on error regardless
|
||||||
}
|
}
|
||||||
if oneTime != -1 {
|
if oneTime != -1 {
|
||||||
errorChan <- nil
|
errorChan <- nil
|
||||||
time.Sleep(time.Duration(30-(currentTime.Second()%30)) * time.Second)
|
time.Sleep(time.Duration(30-(currentTime.Second()%30)) * time.Second)
|
||||||
} else {
|
} else {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// exit after sleep if indicated (will not update clipboard again)
|
// exit after sleep if indicated (will not update clipboard again)
|
||||||
select {
|
select {
|
||||||
case <-done:
|
case <-done:
|
||||||
errorChan <- nil
|
errorChan <- nil
|
||||||
return
|
return nil
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user