Documentation
¶
Overview ¶
Package common contains general functions that are used by various packages and unit tests in ibmcloud-terratest-wrapper module
Package common provides utilities for working with Git repositories.
Index ¶
- Constants
- func ConditionalAdd(amap map[string]interface{}, key string, value string, compareValue string)
- func ConvertArrayToJsonString(arr interface{}) (string, error)
- func CopyDirectory(src string, dst string, fileFilter ...func(string) bool) error
- func CopyFile(source, destination string) error
- func DetermineAuthMethod(repoURL string) (transport.AuthMethod, error)
- func FormatJsonStringPretty(jsonString string) (string, error)
- func GenerateSshRsaPublicKey() (string, error)
- func GetBaseRepoAndBranch(repo string, branch string) (string, string)
- func GetBeforeAfterDiff(jsonString string) (string, error)
- func GetCurrentPrRepoAndBranch() (string, string, error)
- func GetRequiredEnvVars(t *testing.T, variableNames []string) map[string]string
- func GetTagsFromTravis() []string
- func GitRootPath(fromPath string) (string, error)
- func IntArrayContains(arr []int, val int) bool
- func IsArray(v interface{}) bool
- func IsJsonEqual(jsonFile1 string, jsonFile2 string) (bool, error)
- func LoadMapFromYaml(filePath string) (map[string]interface{}, error)
- func MergeMaps(maps ...map[string]interface{}) map[string]interface{}
- func SanitizeSensitiveData(inputJSON string, secureList map[string]interface{}) (string, error)
- func SortMap(m map[string]interface{})
- func SortMapKeys(m map[string]interface{}) []string
- func SortSlice(slice []interface{})
- func StrArrayContains(arr []string, val string) bool
- func StringContainsIgnoreCase(s, substr string) bool
Constants ¶
const SANITIZE_STRING = "SECURE_VALUE_HIDDEN_HASH:"
SANITIZE_STRING is the string used to replace sensitive values.
Variables ¶
This section is empty.
Functions ¶
func ConditionalAdd ¶
Adds value to map[key] only if value != compareValue
func ConvertArrayToJsonString ¶
ConvertArrayToJsonString is a helper function that will take an array of Golang data types, and return a string of the array formatted as a JSON array. Helpful to convert Golang arrays into a format that Terraform can consume.
func CopyDirectory ¶ added in v1.11.0
CopyDirectory copies a directory from source to destination, with optional file filtering. src Source directory to copy from dst Destination directory to copy to fileFilter Optional function to filter files to copy Returns an error if the operation fails.
func CopyFile ¶ added in v1.10.10
CopyFile copies a file from source to destination. Returns an error if the operation fails.
func DetermineAuthMethod ¶ added in v1.11.0
func DetermineAuthMethod(repoURL string) (transport.AuthMethod, error)
DetermineAuthMethod determines the appropriate authentication method for a given repository URL. The function supports both HTTPS and SSH-based repositories.
For HTTPS repositories: - It first checks if the GIT_TOKEN environment variable is set. If so, it uses this as the Personal Access Token (PAT). - If the GIT_TOKEN environment variable is not set, no authentication is used for HTTPS repositories.
For SSH repositories: - It first checks if the SSH_PRIVATE_KEY environment variable is set. If so, it uses this as the SSH private key. - If the SSH_PRIVATE_KEY environment variable is not set, it attempts to use the default SSH key located at ~/.ssh/id_rsa. - If neither the environment variable nor the default key is available, no authentication is used for SSH repositories.
Parameters: - repoURL: The URL of the Git repository.
Returns: - An appropriate AuthMethod based on the repository URL and available credentials. - An error if there's an issue parsing the SSH private key or if the private key cannot be cast to an ssh.Signer.
func FormatJsonStringPretty ¶ added in v1.23.6
func GenerateSshRsaPublicKey ¶ added in v1.10.0
Generate an SSH RSA Keypair (4096 bits), and return the PublicKey in OpenSSH Authorized Key format. Used for tests to generate unique throw-away (but valid) SSH key to supply to test inputs. SPECIAL NOTE: the newline character at end of key will be trimmed and not included!
func GetBaseRepoAndBranch ¶ added in v1.11.0
GetBaseRepoAndBranch determines the base repository URL and branch name based on a hierarchy of sources. The function first checks the provided arguments, then checks environment variables, and finally, if neither source provides the values, it uses Git logic to fetch the details. This function is useful in scenarios where you want to determine the base repository and branch details from multiple potential sources.
Parameters:
- repo: The initial repository URL to consider. This can be an empty string if you want the function to determine the repository URL from other sources.
- branch: The initial branch name to consider. This can be an empty string if you want the function to determine the branch name from other sources.
Returns: - A string representing the base repository URL. - A string representing the base branch name. - An error if any of the Git commands fail or if the repository/branch details cannot be determined.
func GetBeforeAfterDiff ¶
GetBeforeAfterDiff takes a JSON string as input and returns a string with the differences between the "before" and "after" objects in the JSON.
For example, given the JSON string:
{"before": {"a": 1, "b": 2}, "after": {"a": 2, "b": 3}}
the function would return the string:
"Before: {"b": 2}\nAfter: {"a": 2, "b": 3}"
func GetCurrentPrRepoAndBranch ¶ added in v1.11.0
GetCurrentPrRepoAndBranch returns the repository URL and branch name of the current PR.
Returns: - A string representing the repository URL of the current PR. - A string representing the branch name of the current PR. - An error if any of the Git commands fail or if the repository/branch details cannot be determined.
func GetRequiredEnvVars ¶
GetRequiredEnvVars returns a map containing required environment variables and their values Fails the test if any are missing
func GetTagsFromTravis ¶
func GetTagsFromTravis() []string
GetTagsFromTravis Generates a list of tags to add to resources if running in Travis. Returns empty list if not in Travis
func GitRootPath ¶
GitRootPath returns the root directory of the current Git repository.
Parameters: - fromPath: The directory from which the Git command should be executed.
Returns: - A string representing the path to the root directory of the Git repository. - An error if the command fails or the directory is not part of a Git repository.
func IntArrayContains ¶ added in v1.5.1
IntArrayContains is a helper function that will check an array and see if an int value is already present
func IsArray ¶
func IsArray(v interface{}) bool
IsArray is a simple helper function that will determine if a given Golang value is a slice or array.
func IsJsonEqual ¶ added in v1.23.0
IsJsonEqual validates whether the two JSON files are equal or not
func LoadMapFromYaml ¶ added in v1.8.0
LoadMapFromYaml loads a YAML file into a map[string]interface{}. It returns the resulting map and any error encountered.
func SanitizeSensitiveData ¶ added in v1.23.6
SanitizeSensitiveData takes a JSON string and a list of sensitive keys and replaces the values of the sensitive keys with a predefined string.
func SortMap ¶ added in v1.23.0
func SortMap(m map[string]interface{})
SortMap sorts the map and any nested structures inside it. It modifies the original map.
func SortMapKeys ¶ added in v1.23.0
SortMapKeys sorts the keys of a map and returns them as a slice.
func SortSlice ¶ added in v1.23.0
func SortSlice(slice []interface{})
SortSlice sorts the slices recursively. It also takes care of any nested slices or maps inside the slice.
func StrArrayContains ¶
StrArrayContains is a helper function that will check an array and see if a value is already present
func StringContainsIgnoreCase ¶ added in v1.28.1
StringContainsIgnoreCase checks if a string contains a substring, ignoring case. Returns true if the string contains the substring, false otherwise.
Types ¶
This section is empty.