utils

package
v0.0.0-...-bae184c Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const DIR_PERMISSION = 0755

0755 is the default permission for directories, it means the owner can read, write, and execute, and everyone else can read and execute but not write.

Variables

This section is empty.

Functions

func Contains

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

Contains checks if a given string is in a given list of strings. Returns true if the element was found, false otherwise.

Example:

	list := []string{"a", "b", "c"}
	element := "b"
 contains := Contains(list, element)  // contains is true

func ContainsPrefix

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

func CreateAllDirectories

func CreateAllDirectories(baseCacheDirectory string, versionNumber string) error

func ErrorOf

func ErrorOf(_ any, err error) error

ErrorOf is used to wrap a function call and extract the error out of it For example, in this code function Foo returns an error,

func Bar() (string, error){
 // ...
}

The Foo function can use ErrorOf to return the error from Bar:

func Foo() error{
	return ErrorOf(Bar())
}

func FindKeyCaseInsensitive

func FindKeyCaseInsensitive(input map[string]string, key string) (string, bool)

This method tries to find the given key is in the map. It searches different cases of the key:

  1. the exact match
  2. all lower case letters
  3. all upper case letters

If the key in any of these versions was found, it'll be returned alongside with a boolean indicating whether or not it was found.

Example:

map := {"a": "b", "c": "d"}
key := "A"
key, found = FindKeyCaseInsensitive(map, key)  // key is "a" and found is true

func FindValueCaseInsensitive

func FindValueCaseInsensitive(input map[string]string, key string) (string, bool)

This method tries to find the given key is in the map and return its value. It searches different cases of the key:

  1. the exact match
  2. all lower case letters
  3. all upper case letters

If the key in any of these versions was found, its value will be returned alongside with a boolean indicating whether or not it was found.

Example:

map := {"a": "b", "c": "d"}
key := "A"
value, found = FindValueCaseInsensitive(map, key)  // value is "b" and found is true

func GetTemporaryDirectory

func GetTemporaryDirectory(baseCacheDirectory string, versionNumber string) string

func IsSnykIde

func IsSnykIde(environmentName string) bool

func Merge

func Merge(input1 []string, input2 []string) []string

Merge merges two lists of strings and returns the result. The result will contain all elements from the first list and all elements from the second list which are not already in the first list.

Example:

list1 := []string{"a", "b", "c"}
list2 := []string{"b", "c", "d"}
mergedList := Merge(list1, list2)  // mergedList is ["a", "b", "c", "d"]

func Remove

func Remove(input map[string]string, key string) map[string]string

Removes a given key from the input map and uses FindKeyCaseInsensitive() for this. The resulting map is being returned. If the key was not found, the input map will be returned.

Example:

map := {"a": "b", "c": "d"}
key := "A"
map = Remove(map, key)  // map is {"c": "d"}

func RemoveSimilar

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

RemoveSimilar removes all elements from the list which contain the given element. Returns the filtered list.

Example:

list := []string{"a", "b", "c"}
element := "b"
filteredList := RemoveSimilar(list, element)  // filteredList is ["a", "c"]

func ToKeyValueMap

func ToKeyValueMap(input []string, splitBy string) map[string]string

ToKeyValueMap converts a list of strings to a map of strings. The input list will be converted based on the delimiter, 'splitBy'. The resulting map will contain the keys and values of the input list.

Example:

list := []string{"a=b", "c=d"}
splitBy := "="
keyValueMap := ToKeyValueMap(list, splitBy)  // keyValueMap is {"a": "b", "c": "d"}

func ToSlice

func ToSlice(input map[string]string, combineBy string) []string

ToSlice converts a map of strings to a list of strings. The keys and values will be combined by the given delimiter, 'combineBy'. The resulting list will contain the keys and values of the input map.

Example:

map := {"a": "b", "c": "d"}
combineBy := "="
slice := ToSlice(map, combineBy)  // slice is ["a=b", "c=d"]

func ValueOf

func ValueOf[T any](value T, _ error) T

Types

type FileFilter

type FileFilter struct {
	// contains filtered or unexported fields
}

func NewFileFilter

func NewFileFilter(path string, logger *zerolog.Logger, options ...FileFilterOption) *FileFilter

func (*FileFilter) GetAllFiles

func (fw *FileFilter) GetAllFiles() chan string

GetAllFiles traverses a given dir path and fetches all filesToFilter in the directory

func (*FileFilter) GetFilteredFiles

func (fw *FileFilter) GetFilteredFiles(filesCh chan string, globs []string) chan string

GetFilteredFiles returns a filtered channel of filepaths from a given channel of filespaths and glob patterns to filter on

func (*FileFilter) GetRules

func (fw *FileFilter) GetRules(ruleFiles []string) ([]string, error)

GetRules builds a list of glob patterns that can be used to filter filesToFilter

type FileFilterOption

type FileFilterOption func(*FileFilter) error

func WithThreadNumber

func WithThreadNumber(maxThreadCount int) FileFilterOption

type ToLog

type ToLog struct {
	Logger *log.Logger
}

ToLog is an io.Writer that can be used to write into a log.Logger, for example into an existing zerolog.Logger

func (*ToLog) Write

func (w *ToLog) Write(p []byte) (n int, err error)

type ToZeroLogDebug

type ToZeroLogDebug struct {
	Logger *zerolog.Logger
}

ToZeroLogDebug is an io.Writer that can be used for example with log.Logger to write to an existing zerolog writer

func (*ToZeroLogDebug) Write

func (w *ToZeroLogDebug) Write(p []byte) (n int, err error)

Write writes debug messages to the given zerolog logger

Jump to

Keyboard shortcuts

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