78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
/*
|
|
Copyright 2018 The Jetstack cert-manager contributors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package util
|
|
|
|
import (
|
|
"math/rand"
|
|
"sort"
|
|
"time"
|
|
)
|
|
|
|
func OnlyOneNotNil(items ...interface{}) (any bool, one bool) {
|
|
oneNotNil := false
|
|
for _, i := range items {
|
|
if i != nil {
|
|
if oneNotNil {
|
|
return true, false
|
|
}
|
|
oneNotNil = true
|
|
}
|
|
}
|
|
return oneNotNil, oneNotNil
|
|
}
|
|
|
|
func EqualUnsorted(s1 []string, s2 []string) bool {
|
|
if len(s1) != len(s2) {
|
|
return false
|
|
}
|
|
s1_2, s2_2 := make([]string, len(s1)), make([]string, len(s2))
|
|
copy(s1_2, s1)
|
|
copy(s2_2, s2)
|
|
sort.Strings(s1_2)
|
|
sort.Strings(s2_2)
|
|
for i, s := range s1_2 {
|
|
if s != s2_2[i] {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func init() {
|
|
rand.Seed(time.Now().UnixNano())
|
|
}
|
|
|
|
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz")
|
|
|
|
func RandStringRunes(n int) string {
|
|
b := make([]rune, n)
|
|
for i := range b {
|
|
b[i] = letterRunes[rand.Intn(len(letterRunes))]
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
// Contains returns true if a string is contained in a string slice
|
|
func Contains(ss []string, s string) bool {
|
|
for _, v := range ss {
|
|
if v == s {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|