utils

package
v1.67.0-rc.5 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2024 License: Apache-2.0, BSD-3-Clause, Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExpandPath

func ExpandPath(path string) string

ExpandPath expands a file path that starts with '~' to the user's home directory. If the path does not start with '~', it is returned unchanged.

Parameters:

path - The file path to be expanded.

Returns:

The expanded file path, with '~' replaced by the user's home directory, if applicable.

func GetCITags

func GetCITags() map[string]string

GetCITags retrieves and caches the CI/CD tags from environment variables. It initializes the ciTags map if it is not already initialized. This function is thread-safe due to the use of a mutex.

Returns:

A map[string]string containing the CI/CD tags.

func GetModuleAndSuiteName

func GetModuleAndSuiteName(pc uintptr) (module string, suite string)

GetModuleAndSuiteName extracts the module name and suite name from a given program counter (pc). This function utilizes runtime.FuncForPC to retrieve the full function name associated with the program counter, then splits the string to separate the package name from the function name.

Example 1:

Input:  github.com/DataDog/dd-sdk-go-testing.TestRun
Output:
   module: github.com/DataDog/dd-sdk-go-testing
   suite:  testing_test.go

Example 2:

Input:  github.com/DataDog/dd-sdk-go-testing.TestRun.func1
Output:
   module: github.com/DataDog/dd-sdk-go-testing
   suite:  testing_test.go

Parameters:

pc - The program counter for which the module and suite name should be retrieved.

Returns:

module - The module name extracted from the full function name.
suite  - The base name of the file where the function is located.

func GetRelativePathFromCITagsSourceRoot

func GetRelativePathFromCITagsSourceRoot(path string) string

GetRelativePathFromCITagsSourceRoot calculates the relative path from the CI workspace root to the specified path. If the CI workspace root is not available in the tags, it returns the original path.

Parameters:

path - The absolute or relative file path for which the relative path should be calculated.

Returns:

The relative path from the CI workspace root to the specified path, or the original path if an error occurs.

func GetStacktrace

func GetStacktrace(skip int) string

GetStacktrace retrieves the current stack trace, skipping a specified number of frames.

This function captures the stack trace of the current goroutine, formats it, and returns it as a string. It uses runtime.Callers to capture the program counters of the stack frames and runtime.CallersFrames to convert these program counters into readable frames. The stack trace is formatted to include the function name, file name, and line number of each frame.

Parameters:

skip - The number of stack frames to skip before capturing the stack trace.

Returns:

A string representation of the current stack trace, with each frame on a new line.

Types

type CodeOwners

type CodeOwners struct {
	Sections []*Section
}

CodeOwners represents a structured data type that holds sections of code owners. Each section maps to a slice of entries, where each entry includes a pattern and a list of owners.

func GetCodeOwners

func GetCodeOwners() *CodeOwners

GetCodeOwners retrieves and caches the CODEOWNERS data. It looks for the CODEOWNERS file in various standard locations within the CI workspace. This function is thread-safe due to the use of a mutex.

Returns:

A pointer to a CodeOwners struct containing the parsed CODEOWNERS data, or nil if not found.

func NewCodeOwners

func NewCodeOwners(filePath string) (*CodeOwners, error)

NewCodeOwners creates a new instance of CodeOwners by parsing a CODEOWNERS file located at the given filePath. It returns an error if the file cannot be read or parsed properly.

func (*CodeOwners) GetSection

func (co *CodeOwners) GetSection(section string) *Section

GetSection gets the first Section entry in the CodeOwners that matches the section name. It returns a pointer to the matched entry, or nil if no match is found

func (*CodeOwners) Match

func (co *CodeOwners) Match(value string) (*Entry, bool)

Match finds the first entry in the CodeOwners that matches the given value. It returns a pointer to the matched entry, or nil if no match is found.

type Entry

type Entry struct {
	Pattern string
	Owners  []string
	Section string
}

Entry represents a single entry in a CODEOWNERS file. It includes the pattern for matching files, the list of owners, and the section to which it belongs.

func (Entry) GetOwnersString

func (e Entry) GetOwnersString() string

GetOwnersString returns a formatted string of the owners list in an Entry. It returns an empty string if there are no owners.

type Section

type Section struct {
	Name    string
	Entries []Entry
}

Section represents a block of structured data of multiple entries in a single section

Jump to

Keyboard shortcuts

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