github.com/kubeshop/testkube@v1.17.23/cmd/tcl/testworkflow-init/data/utils.go (about)

     1  // Copyright 2024 Testkube.
     2  //
     3  // Licensed as a Testkube Pro file under the Testkube Community
     4  // License (the "License"); you may not use this file except in compliance with
     5  // the License. You may obtain a copy of the License at
     6  //
     7  //	https://github.com/kubeshop/testkube/blob/main/licenses/TCL.txt
     8  
     9  package data
    10  
    11  import (
    12  	"fmt"
    13  	"os"
    14  	"strconv"
    15  	"strings"
    16  )
    17  
    18  func getStr(config map[string]string, key string, defaultValue string) string {
    19  	val, ok := config[key]
    20  	if !ok {
    21  		return defaultValue
    22  	}
    23  	return val
    24  }
    25  
    26  func getInt(config map[string]string, key string, defaultValue int) int {
    27  	str := getStr(config, key, "")
    28  	if str == "" {
    29  		return defaultValue
    30  	}
    31  	val, err := strconv.Atoi(str)
    32  	if err != nil {
    33  		fmt.Printf("invalid '%s' provided: '%s': %v\n", key, str, err)
    34  		os.Exit(155)
    35  	}
    36  	return val
    37  }
    38  
    39  func getBool(config map[string]string, key string, defaultValue bool) bool {
    40  	str := getStr(config, key, "")
    41  	if str == "" {
    42  		return defaultValue
    43  	}
    44  	return strings.ToLower(str) == "true" || str == "1"
    45  }
    46  
    47  // Iterate over all items, all the time, until no more is done
    48  func Iterate[T any](v []T, fn func(T) bool) {
    49  	result := v
    50  	for {
    51  		l := len(result)
    52  		for i := 0; i < len(result); i++ {
    53  			if fn(result[i]) {
    54  				result = append(result[0:i], result[i+1:]...)
    55  			}
    56  		}
    57  		if len(result) == l {
    58  			return
    59  		}
    60  	}
    61  }