github.com/in4it/ecs-deploy@v0.0.42-0.20240508120354-ed77ff16df25/util/common.go (about) 1 package util 2 3 import ( 4 "bufio" 5 "fmt" 6 "math/rand" 7 "os" 8 "strings" 9 "time" 10 ) 11 12 // askForConfirmation asks the user for confirmation. A user must type in "yes" or "no" and 13 // then press enter. It has fuzzy matching, so "y", "Y", "yes", "YES", and "Yes" all count as 14 // confirmations. If the input is not recognized, it will ask again. The function does not return 15 // until it gets a valid response from the user. 16 func AskForConfirmation(s string) (bool, error) { 17 reader := bufio.NewReader(os.Stdin) 18 19 for { 20 fmt.Printf("%s [y/n]: ", s) 21 22 response, err := reader.ReadString('\n') 23 if err != nil { 24 return false, err 25 } 26 27 response = strings.ToLower(strings.TrimSpace(response)) 28 29 if response == "y" || response == "yes" { 30 return true, nil 31 } else if response == "n" || response == "no" { 32 return false, nil 33 } 34 } 35 } 36 37 // stackoverflow 38 var randSrc = rand.NewSource(time.Now().UnixNano()) 39 40 const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" 41 const ( 42 letterIdxBits = 6 // 6 bits to represent a letter index 43 letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits 44 letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits 45 ) 46 47 func RandStringBytesMaskImprSrc(n int) string { 48 b := make([]byte, n) 49 // A randSrc.Int63() generates 63 random bits, enough for letterIdxMax characters! 50 for i, cache, remain := n-1, randSrc.Int63(), letterIdxMax; i >= 0; { 51 if remain == 0 { 52 cache, remain = randSrc.Int63(), letterIdxMax 53 } 54 if idx := int(cache & letterIdxMask); idx < len(letterBytes) { 55 b[i] = letterBytes[idx] 56 i-- 57 } 58 cache >>= letterIdxBits 59 remain-- 60 } 61 62 return string(b) 63 } 64 65 func YesNoToBool(s string) bool { 66 if strings.ToLower(s) == "yes" { 67 return true 68 } else { 69 return false 70 } 71 } 72 73 func TruncateString(str string, n int) string { 74 if n > 0 && len(str) > n { 75 return str[0:n] 76 } else { 77 return str 78 } 79 } 80 81 /* 82 * RemoveCommonElements removes the common elements and returns the first array 83 */ 84 func RemoveCommonElements(a, b []string) []string { 85 var c = []string{} 86 var d = []string{} 87 88 for _, item1 := range a { 89 for _, item2 := range b { 90 if item1 == item2 { 91 c = append(c, item2) 92 } 93 } 94 } 95 96 for _, item1 := range a { 97 found := false 98 for _, item2 := range c { 99 if item1 == item2 { 100 found = true 101 } 102 } 103 if !found { 104 d = append(d, item1) 105 } 106 } 107 return d 108 } 109 110 /* 111 * IsBoolArrayTrue checks whether the array contains only true elements 112 */ 113 func IsBoolArrayTrue(array []bool) bool { 114 if len(array) == 0 { 115 return false 116 } 117 for _, v := range array { 118 if !v { 119 return false 120 } 121 } 122 return true 123 } 124 125 /* 126 * InArray returns true if the value exists in the array 127 */ 128 func InArray(a []string, v string) (ret bool, i int) { 129 for i = range a { 130 if ret = a[i] == v; ret { 131 return ret, i 132 } 133 } 134 return false, -1 135 }