util

package
v2.7.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2024 License: MIT Imports: 35 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CanonicalPath

func CanonicalPath(path string, basePath string) (string, error)

CanonicalPath returns the canonical version of the given path, relative to the given base path. That is, if the given path is a relative path, assume it is relative to the given base path. A canonical path is an absolute path with all relative components (e.g. "../") fully resolved, which makes it safe to compare paths as strings.

func CanonicalPaths

func CanonicalPaths(paths []string, basePath string) ([]string, error)

CanonicalPaths returns the canonical version of the given paths, relative to the given base path. That is, if a given path is a relative path, assume it is relative to the given base path. A canonical path is an absolute path with all relative components (e.g. "../") fully resolved, which makes it safe to compare paths as strings.

func CleanPath

func CleanPath(path string) string

CleanPath is used to clean paths to ensure the returned path uses / as the path separator to improve cross-platform compatibility.

func CommaSeparatedStrings

func CommaSeparatedStrings(list []string) string

CommaSeparatedStrings returns an HCL compliant formated list of strings (each string within double quote)

func ConvertToMap

func ConvertToMap(value interface{}, keys ...string) interface{}

ConvertToMap builds a map with the keys supplied

func CopyFile

func CopyFile(source string, destination string) error

CopyFile copies a file from source to destination

func CopyFolderContents

func CopyFolderContents(source, destination string, excluded ...string) error

CopyFolderContents copies the files and folders within the source folder into the destination folder. Note that hidden files and folders (those starting with a dot) will be skipped.

func DeleteFiles

func DeleteFiles(files []string) error

DeleteFiles deletes the given list of files. Note: this function ONLY deletes files and will return an error if you pass in a folder path.

func EncodeBase64Sha1

func EncodeBase64Sha1(str string) string

EncodeBase64Sha1 returns the base 64 encoded sha1 hash of the given string

func ExpandArguments

func ExpandArguments(args []interface{}, folder string) (result []interface{})

ExpandArguments expands the list of arguments like x shell do

func FileExists

func FileExists(path string) bool

FileExists returns true if the given file exists.

func FileStat

func FileStat(path string) (result os.FileInfo, err error)

FileStat calls os.Stat with retries When multiple projects are running in parallel, the os.Stat() function returns 'bad file descriptor' if the file is being overwritten while being read.

func FilterList

func FilterList(list []string, test func(string) bool) (ret []string)

FilterList returns a copy of the given list with elements that don't match the test function filtered out

func FromCtyValue

func FromCtyValue(ctyValue cty.Value, out interface{}) error

FromCtyValue converts a cty Value to a go object This is a hacky workaround to convert a cty Value to a Go map[string]interface{}. cty does not support this directly (https://github.com/hashicorp/hcl2/issues/108) and doing it with gocty.FromCtyValue is nearly impossible, as cty requires you to specify all the output types and will error out when it hits interface{}. So, as an ugly workaround, we convert the given value to JSON using cty's JSON library and then convert the JSON back to a map[string]interface{} using the Go json library.

func GetCopy

func GetCopy(dst, src, fileRegex string) error

GetCopy is the same as Get except that it downloads a copy of the module represented by source.

This copy will omit and dot-prefixed files (such as .git/, .hg/) and can't be updated on its own.

func GetPathRelativeTo

func GetPathRelativeTo(path, basePath string) (string, error)

GetPathRelativeTo returns the relative path you would have to take to get from basePath to path

func GetPathRelativeToMax

func GetPathRelativeToMax(path, basePath string, maxLevel uint) (result string)

GetPathRelativeToMax returns either an absolute path or a relative path if it is not too far from relatively to the base path

func GetPathRelativeToWorkingDir

func GetPathRelativeToWorkingDir(path string) (result string)

GetPathRelativeToWorkingDir returns the path relative to the current working directory

func GetPathRelativeToWorkingDirMax

func GetPathRelativeToWorkingDirMax(path string, maxLevel uint) (result string)

GetPathRelativeToWorkingDirMax returns either an absolute path or a relative path if it is not too far from relatively to the current directory

func GetRandomTime

func GetRandomTime(lowerBound, upperBound time.Duration) time.Duration

GetRandomTime returns random time duration between the lower bound and upper bound. This is useful because some of our

automated tests wound up flooding the AWS API all at once, leading to a "Subscriber limit exceeded" error.

TODO: Some of the more exotic test cases fail, but it's not worth catching them given the intended use of this function.

func GetSource

func GetSource(source, pwd string, logger *multilogger.Logger, fileRegex string) (string, error)

GetSource gets the content of the source in a temporary folder and returns the local path. The function manages a cache to avoid multiple remote calls if the content has not changed. When given a local path, it is directly returned

func GetTempDownloadFolder

func GetTempDownloadFolder(folders ...string) string

GetTempDownloadFolder returns the fo

func Grep

func Grep(regex *regexp.Regexp, folder string, patterns ...string) (bool, error)

Grep returns true if the given regex can be found in any of the files matched by the given glob

func IndexOrDefault

func IndexOrDefault(list []string, index int, defVal string) string

IndexOrDefault returns the item at index position or default value if the array is shorter than index

func JoinPath

func JoinPath(elem ...string) string

JoinPath always use / as the separator. Windows systems use \ as the path separator *nix uses / Use this function when joining paths to force the returned path to use / as the path separator This will improve cross-platform compatibility

func KindOf

func KindOf(value interface{}) reflect.Kind

KindOf returns the kind of the type or Invalid if value is nil

func ListContainsElement

func ListContainsElement(list []string, element string) bool

ListContainsElement returns true if the given list contains the given element

func ListContainsElementInterface

func ListContainsElementInterface(list []interface{}, element interface{}) bool

ListContainsElementInterface returns true if the given list contains the given element

func LoadDefaultValues

func LoadDefaultValues(folder string, logger *multilogger.Logger, keepInCache bool) (importedVariables map[string]interface{}, allVariables map[string]*configs.Variable, err error)

LoadDefaultValues returns a map of the variables defined in the tfvars file

func LoadVariables

func LoadVariables(content string, cwd string, applyTemplate bool, context ...interface{}) (map[string]interface{}, error)

LoadVariables returns a map of the variables defined in the content provider

func LoadVariablesFromFile

func LoadVariablesFromFile(path, cwd string, applyTemplate bool, context ...interface{}) (map[string]interface{}, error)

LoadVariablesFromFile returns a map of the variables defined in the tfvars file

func LoadVariablesFromHcl added in v2.2.1

func LoadVariablesFromHcl(filename string, bytes []byte) (map[string]interface{}, error)

LoadVariablesFromHcl parses HCL content to get a map of the attributes

func LoadVariablesFromSource

func LoadVariablesFromSource(content, fileName, cwd string, applyTemplate bool, context ...interface{}) (result map[string]interface{}, err error)

LoadVariablesFromSource returns a map of the variables defined in the content provider

func PathContainsHiddenFileOrFolder

func PathContainsHiddenFileOrFolder(path string) bool

PathContainsHiddenFileOrFolder returns true if folder contains files starting with . (except . and ..)

func ReadFileAsString

func ReadFileAsString(path string) (string, error)

ReadFileAsString returns the contents of the file at the given path as a string

func ReadFileAsStringFromSource

func ReadFileAsStringFromSource(source, path string, logger *multilogger.Logger) (localFile, content string, err error)

ReadFileAsStringFromSource returns the contents of the file at the given path from an external source (github, s3, etc.) as a string It uses terraform to execute its command

func RemoveDuplicatesFromList

func RemoveDuplicatesFromList(list []string, keepLast bool, getKey func(key string) string) []string

RemoveDuplicatesFromList returns a copy of the given list with all duplicates removed (keeping the last encountereds) Params:

  list: The list to filter
  keepLast: Indicates whether the last or first encountered duplicate element should be kept
	 getKey: Function used to extract the actual key from the string

func RemoveDuplicatesFromListKeepFirst

func RemoveDuplicatesFromListKeepFirst(list []string) []string

RemoveDuplicatesFromListKeepFirst returns a copy of the given list with all duplicates removed (keeping the first encountereds)

func RemoveDuplicatesFromListKeepLast

func RemoveDuplicatesFromListKeepLast(list []string) []string

RemoveDuplicatesFromListKeepLast returns a copy of the given list with all duplicates removed (keeping the last encountereds)

func RemoveElementFromList

func RemoveElementFromList(list []string, element string) []string

RemoveElementFromList returns a copy of the given list with all instances of the given element removed

func SplitEnvVariable

func SplitEnvVariable(str string) (key, value string, err error)

SplitEnvVariable returns the two parts of an environment variable

func ToCtyValue

func ToCtyValue(value interface{}) (*cty.Value, error)

ToCtyValue converts a go object to a cty Value This is a hacky workaround to convert a cty Value to a Go map[string]interface{}. cty does not support this directly (https://github.com/hashicorp/hcl2/issues/108) and doing it with gocty.FromCtyValue is nearly impossible, as cty requires you to specify all the output types and will error out when it hits interface{}. So, as an ugly workaround, we convert the given value to JSON using cty's JSON library and then convert the JSON back to a map[string]interface{} using the Go json library.

func WriteFileWithSamePermissions

func WriteFileWithSamePermissions(source string, destination string, contents []byte) error

WriteFileWithSamePermissions writes a file to the given destination with the given contents using the same permissions as the file at source

Types

type GetMode

type GetMode byte

GetMode is an enum that describes how modules are loaded.

GetModeLoad says that modules will not be downloaded or updated, they will only be loaded from the storage.

GetModeGet says that modules can be initially downloaded if they don't exist, but otherwise to just load from the current version in storage.

GetModeUpdate says that modules should be checked for updates and downloaded prior to loading. If there are no updates, we load the version from disk, otherwise we download first and then load.

const (
	GetModeNone GetMode = iota
	GetModeGet
	GetModeUpdate
)

GetMode values

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL