github.com/kubeshop/testkube@v1.17.23/pkg/utils/utils.go (about)

     1  package utils
     2  
     3  import (
     4  	"bufio"
     5  	"crypto/rand"
     6  	"encoding/base64"
     7  	"math/big"
     8  	"os"
     9  	"path/filepath"
    10  	"regexp"
    11  	"strings"
    12  	"text/template"
    13  	"time"
    14  
    15  	sprig "github.com/go-task/slim-sprig"
    16  	"github.com/pkg/errors"
    17  )
    18  
    19  func ContainsTag(tags []string, tag string) bool {
    20  	for _, t := range tags {
    21  		if t == tag {
    22  			return true
    23  		}
    24  	}
    25  	return false
    26  }
    27  
    28  func RemoveDuplicates(s []string) []string {
    29  	m := make(map[string]struct{})
    30  	result := []string{}
    31  
    32  	for _, v := range s {
    33  		if _, value := m[v]; !value {
    34  			m[v] = struct{}{}
    35  			result = append(result, v)
    36  		}
    37  	}
    38  	return result
    39  }
    40  
    41  // RoundDuration rounds duration to default value if no round passed
    42  func RoundDuration(duration time.Duration, to ...time.Duration) time.Duration {
    43  	roundTo := 10 * time.Millisecond
    44  	if len(to) > 0 {
    45  		roundTo = to[0]
    46  	}
    47  	return duration.Round(roundTo)
    48  }
    49  
    50  // ReadLongLine reads long line
    51  func ReadLongLine(r *bufio.Reader) (line []byte, err error) {
    52  	var buffer []byte
    53  	var isPrefix bool
    54  
    55  	for {
    56  		buffer, isPrefix, err = r.ReadLine()
    57  		line = append(line, buffer...)
    58  		if err != nil {
    59  			break
    60  		}
    61  
    62  		if !isPrefix {
    63  			break
    64  		}
    65  	}
    66  
    67  	return line, err
    68  }
    69  
    70  func RandAlphanum(n int) string {
    71  	letters := []rune("abcdefghijklmnopqrstuvwxyz0123456789")
    72  	b := make([]rune, n)
    73  	for i := range b {
    74  		nBig, err := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
    75  		if err != nil {
    76  			panic(err)
    77  		}
    78  		b[i] = letters[nBig.Int64()]
    79  	}
    80  	return string(b)
    81  }
    82  
    83  func CheckStringKey(m map[string]any, key string) error {
    84  	if _, ok := m[key]; !ok {
    85  		return errors.New(key + " is missing")
    86  	}
    87  	if _, ok := m[key].(string); !ok {
    88  		return errors.New(key + " is not a string")
    89  	}
    90  	return nil
    91  }
    92  
    93  func GetStringKey(m map[string]any, key string) (string, error) {
    94  	if _, ok := m[key]; !ok {
    95  		return "", errors.New(key + " is missing")
    96  	}
    97  	s, ok := m[key].(string)
    98  	if !ok {
    99  		return "", errors.New(key + " is not a string")
   100  	}
   101  	return s, nil
   102  }
   103  
   104  // SanitizeName sanitizes test name
   105  func SanitizeName(path string) string {
   106  	path = strings.TrimSuffix(path, filepath.Ext(path))
   107  
   108  	reg := regexp.MustCompile("[^a-zA-Z0-9-]+")
   109  	path = reg.ReplaceAllString(path, "-")
   110  	path = strings.TrimLeft(path, "-")
   111  	path = strings.TrimRight(path, "-")
   112  	path = strings.ToLower(path)
   113  
   114  	if len(path) > 63 {
   115  		return path[:63]
   116  	}
   117  
   118  	return path
   119  }
   120  
   121  // EscapeDots escapes dots for MongoDB fields
   122  func EscapeDots(source string) string {
   123  	return strings.ReplaceAll(source, ".", string([]rune{0xFF0E}))
   124  }
   125  
   126  // UnescapeDots unescapes dots from MongoDB fields
   127  func UnescapeDots(source string) string {
   128  	return strings.ReplaceAll(source, string([]rune{0xFF0E}), ".")
   129  }
   130  
   131  func NewTemplate(name string) *template.Template {
   132  	return template.New(name).Funcs(sprig.FuncMap())
   133  }
   134  
   135  // IsBase64Encoded check if string is base84 encoded
   136  func IsBase64Encoded(base64Val string) bool {
   137  	decoded, err := base64.StdEncoding.DecodeString(base64Val)
   138  	if err != nil {
   139  		return false
   140  	}
   141  
   142  	encoded := base64.StdEncoding.EncodeToString(decoded)
   143  	return base64Val == encoded
   144  }
   145  
   146  // GetEnvVarWithDeprecation returns the value of the environment variable with the given key,
   147  // or the value of the environment variable with the given deprecated key, or the default value
   148  // if neither is set
   149  func GetEnvVarWithDeprecation(key, deprecatedKey, defaultVal string) string {
   150  	if val, ok := os.LookupEnv(key); ok {
   151  		return val
   152  	}
   153  	if val, ok := os.LookupEnv(deprecatedKey); ok {
   154  		return val
   155  	}
   156  	return defaultVal
   157  }