mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 11:37:01 -07:00
36 lines
675 B
Go
36 lines
675 B
Go
package util
|
|
|
|
import (
|
|
"fmt"
|
|
"os/user"
|
|
"time"
|
|
)
|
|
|
|
// CheckUntil regularly check a predicate until it's true or time out is reached.
|
|
func CheckUntil(interval time.Duration, timeout time.Duration, predicate func() (bool, error)) error {
|
|
timeoutCh := time.After(timeout)
|
|
|
|
for {
|
|
select {
|
|
case <-time.After(interval):
|
|
predTrue, err := predicate()
|
|
if predTrue {
|
|
return nil
|
|
}
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
case <-timeoutCh:
|
|
return fmt.Errorf("timeout of %ds reached", int64(timeout/time.Second))
|
|
}
|
|
}
|
|
}
|
|
|
|
func GetCurrentUsername() string {
|
|
currentUser, _ := user.Current()
|
|
if currentUser != nil {
|
|
return currentUser.Username
|
|
}
|
|
return ""
|
|
}
|