github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/util/util.go (about) 1 /* 2 Copyright 2018 The OpenEBS Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package util 18 19 import ( 20 "crypto/md5" 21 "encoding/hex" 22 "fmt" 23 "os" 24 "strconv" 25 "strings" 26 27 "k8s.io/klog/v2" 28 ) 29 30 // truthyValues maps a set of values which are considered as true 31 var truthyValues = map[string]bool{ 32 "1": true, 33 "YES": true, 34 "TRUE": true, 35 "OK": true, 36 } 37 38 // CheckTruthy checks for truthiness of the passed argument. 39 func CheckTruthy(truth string) bool { 40 return truthyValues[strings.ToUpper(truth)] 41 } 42 43 // falsyValues maps a set of values which are considered as false 44 var falsyValues = map[string]bool{ 45 "0": true, 46 "NO": true, 47 "FALSE": true, 48 "BLANK": true, 49 } 50 51 // CheckFalsy checks for non-truthiness of the passed argument. 52 func CheckFalsy(falsy string) bool { 53 if len(falsy) == 0 { 54 falsy = "blank" 55 } 56 return falsyValues[strings.ToUpper(falsy)] 57 } 58 59 // CheckErr to handle command errors 60 func CheckErr(err error, handleErr func(string)) { 61 if err == nil { 62 return 63 } 64 handleErr(err.Error()) 65 } 66 67 // Fatal prints the message (if provided) and then exits. If V(2) or greater, 68 // klog.Fatal is invoked for extended information. 69 func Fatal(msg string) { 70 if klog.V(2).Enabled() { 71 klog.FatalDepth(2, msg) 72 } 73 if len(msg) > 0 { 74 // add newline if needed 75 if !strings.HasSuffix(msg, "\n") { 76 msg += "\n" 77 } 78 fmt.Fprint(os.Stderr, msg) 79 } 80 os.Exit(1) 81 } 82 83 // StringToInt32 converts a string type to corresponding 84 // *int32 type 85 func StringToInt32(val string) (*int32, error) { 86 if len(val) == 0 { 87 return nil, fmt.Errorf("Nil value to convert") 88 } 89 90 n, err := strconv.ParseInt(val, 10, 32) 91 if err != nil { 92 return nil, err 93 } 94 n32 := int32(n) 95 return &n32, nil 96 } 97 98 // StrToInt32 converts a string type to corresponding 99 // *int32 type 100 // 101 // NOTE: 102 // This swallows the error if any 103 func StrToInt32(val string) *int32 { 104 n32, _ := StringToInt32(val) 105 return n32 106 } 107 108 // Hash retrieves an encoded string using the md5 encrypt 109 func Hash(s string) string { 110 hasher := md5.New() 111 hasher.Write([]byte(s)) 112 return hex.EncodeToString(hasher.Sum(nil)) 113 } 114 115 // StateStatus returns enable if state is true and disable if state is false 116 func StateStatus(state bool) string { 117 var status = map[bool]string{ 118 true: "enable", 119 false: "disable", 120 } 121 return status[state] 122 }