8000 argo-cd/util/util.go at v1.7.11 · dominikbraun/argo-cd · GitHub
[go: up one dir, main page]

Skip to content
< 8000 div class="prc-PageLayout-Content-xWL-A" data-width="full" style="--spacing:var(--spacing-none)">

Latest commit

 

History

History
43 lines (38 loc) · 1.06 KB

util.go

File metadata and controls

43 lines (38 loc) · 1.06 KB
package util
import (
"crypto/rand"
"encoding/base64"
"time"
)
// Wait takes a check interval and timeout and waits for a function to return `true`.
// Wait will return `true` on success and `false` on timeout.
// The passed function, in turn, should pass `true` (or anything, really) to the channel when it's done.
// Pass `0` as the timeout to run infinitely until completion.
func Wait(timeout uint, f func(chan<- bool)) bool {
done := make(chan bool)
go f(done)
// infinite
if timeout == 0 {
return <-done
}
timedOut := time.After(time.Duration(timeout) * time.Second)
for {
select {
case <-done:
return true
case <-timedOut:
return false
}
}
}
// MakeSignature generates a cryptographically-secure pseudo-random token, based on a given number of random bytes, for signing purposes.
func MakeSignature(size int) ([]byte, error) {
b := make([]byte, size)
_, err := rand.Read(b)
if err != nil {
b = nil
}
// base64 encode it so signing key can be typed into validation utilities
b = []byte(base64.StdEncoding.EncodeToString(b))
return b, err
}
0