utils

package
v0.0.0-...-e472556 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2023 License: Apache-2.0 Imports: 28 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrShortWrite = errors.New("short write")

Functions

func AsyncMultiWriter

func AsyncMultiWriter(writers ...io.Writer) io.Writer

AsyncMultiWriter creates a writer that duplicates its writes to all the provided writers asynchronous

func CalcChecksums

func CalcChecksums(reader io.Reader, checksumType ...Algorithm) (map[Algorithm]string, error)

CalcChecksums calculates all hashes at once using AsyncMultiWriter. The file is therefore read only once.

func CleanOldDirs

func CleanOldDirs() error

Old runs/tests may leave junk at temp dir. Each temp file/Dir is named with prefix+timestamp, search for all temp files/dirs that match the common prefix and validate their timestamp.

func CopyDir

func CopyDir(fromPath, toPath string, includeDirs bool, excludeNames []string) error

Copy directory content from one path to another. includeDirs means to copy also the dirs if presented in the src folder. excludeNames - Skip files/dirs in the src folder that match names in provided slice. ONLY excludes first layer (only in src folder).

func CopyFile

func CopyFile(dst, src string) (err error)

func CreateDirIfNotExist

func CreateDirIfNotExist(path string) error

func CreateExtractorPropsFile

func CreateExtractorPropsFile(extractorConfPath, buildInfoPath, buildName, buildNumber, project string, configProperties map[string]string) (string, error)

Save all the extractor's properties into a local file. extractorConfPath - Path to a file to which all the extractor's properties will be written. buildInfoPath - Path to a file to which the build-info data will be written. buildName - Build name of the current build. buildNumber - Build number of the current build. project - JFrog Project key of the current build configProperties - Data of the actual extractor's properties. Returns the extractor Config file path.

func CreateFilePath

func CreateFilePath(localPath, fileName string) (string, error)

func CreateTempBuildFile

func CreateTempBuildFile(buildName, buildNumber, projectKey, buildsDirPath string, logger Log) (*os.File, error)

Create a temp file of build-info.

func CreateTempDir

func CreateTempDir() (string, error)

CreateTempDir creates a temporary directory and returns its path.

func DoubleWinPathSeparator

func DoubleWinPathSeparator(filePath string) string

func DownloadDependencies

func DownloadDependencies(downloadTo, filename, relativeFilePath string, downloadExtractorFunc func(downloadTo, downloadPath string) error, logger Log) error

func DownloadFile

func DownloadFile(downloadTo string, fromUrl string) (err error)

func FindFileInDirAndParents

func FindFileInDirAndParents(dirPath, fileName string) (string, error)

FindFileInDirAndParents looks for a file named fileName in dirPath and its parents, and returns the path of the directory where it was found. dirPath must be a full path.

func GetBuildDir

func GetBuildDir(buildName, buildNumber, projectKey, buildsDirPath string) (string, error)

func GetCachePath

func GetCachePath() (string, error)

GetCachePath returns the location of downloads dir inside the GOMODCACHE

func GetDependenciesGraph

func GetDependenciesGraph(projectDir string, log Log) (map[string][]string, error)

Runs 'go mod graph' command and returns map that maps dependencies to their child dependencies slice

func GetDependenciesList

func GetDependenciesList(projectDir string, log Log) (map[string]bool, error)

Runs go list -f {{with .Module}}{{.Path}}:{{.Version}}{{end}} all command and returns map of the dependencies

func GetFileAndDirFromPath

func GetFileAndDirFromPath(path string) (fileName, dir string)

Return the file's name and dir of a given path by finding the index of the last separator in the path. Support separators : "/" , "\\" and "\\\\"

func GetFileChecksums

func GetFileChecksums(filePath string) (md5, sha1, sha2 string, err error)

func GetFileContentAndInfo

func GetFileContentAndInfo(filePath string) (fileContent []byte, fileInfo os.FileInfo, err error)

func GetFileInfo

func GetFileInfo(path string, followSymlink bool) (fileInfo os.FileInfo, err error)

Get the file info of the file in path. If path points at a symlink and `followSymlink == false`, return the file info of the symlink instead

func GetFileSeparator

func GetFileSeparator() string

func GetGoModCachePath

func GetGoModCachePath() (string, error)

GetGoModCachePath returns the location of the go module cache

func GetModuleNameByDir

func GetModuleNameByDir(projectDir string, log Log) (string, error)

Runs 'go list -m' command and returns module name

func GetParsedGoVersion

func GetParsedGoVersion() (*version.Version, error)

func GetPartialsBuildDir

func GetPartialsBuildDir(buildName, buildNumber, projectKey, buildsDirPath string) (string, error)

func GetProjectRoot

func GetProjectRoot() (string, error)

Returns the root dir where the go.mod located.

func GetRegExp

func GetRegExp(regex string) (*regexp.Regexp, error)

func IsDirExists

func IsDirExists(path string, followSymlink bool) (bool, error)

Check if path points at a directory. If path points at a symlink and `followSymlink == false`, function will return `false` regardless of the symlink target

func IsFileExists

func IsFileExists(path string, followSymlink bool) (bool, error)

Check if path points at a file. If path points at a symlink and `followSymlink == false`, function will return `true` regardless of the symlink target

func IsFileSymlink(file os.FileInfo) bool

func IsLinux

func IsLinux() bool

func IsPathExists

func IsPathExists(path string) bool

IsPathExists checks if a path exists.

func IsPathSymlink(path string) bool

func IsWindows

func IsWindows() bool

func ListFiles

func ListFiles(path string, includeDirs bool) ([]string, error)

Return the list of files and directories in the specified path

func ListFilesByFilterFunc

func ListFilesByFilterFunc(path string, filterFunc func(filePath string) (bool, error)) ([]string, error)

Return all files in the specified path who satisfy the filter func. Not recursive.

func MoveDir

func MoveDir(fromPath, toPath string) error

Move directory content from one path to another.

func MoveFile

func MoveFile(sourcePath, destPath string) (err error)

GoLang: os.Rename() give error "invalid cross-device link" for Docker container with Volumes. MoveFile(source, destination) will work moving file between folders Therefore, we are using our own implementation (MoveFile) in order to rename files.

func NewDefaultLogger

func NewDefaultLogger(logLevel LevelType) *defaultLogger

NewDefaultLogger creates a new logger with a given LogLevel. All logs are written to Stderr and output is written to Stdout.

func ReadNLines

func ReadNLines(path string, total int) (lines []string, err error)

strip '\n' or read until EOF, return error if read error readNLines reads up to 'total' number of lines separated by \n.

func RemoveTempDir

func RemoveTempDir(dirPath string) error

func RunGo

func RunGo(goArg []string, repoUrl string) error

func UnixToWinPathSeparator

func UnixToWinPathSeparator(filePath string) string

func Unmarshal

func Unmarshal(filePath string, loadTarget interface{}) (err error)

Parses the JSON-encoded data and stores the result in the value pointed to by 'loadTarget'. filePath - Path to json file. loadTarget - Pointer to a struct

func WinToUnixPathSeparator

func WinToUnixPathSeparator(filePath string) string

Types

type Algorithm

type Algorithm int
const (
	MD5 Algorithm = iota
	SHA1
	SHA256
)

type Command

type Command struct {
	Executable string
	CmdName    string
	CmdArgs    []string
	Dir        string
	StrWriter  io.WriteCloser
	ErrWriter  io.WriteCloser
}

func NewCommand

func NewCommand(executable, cmdName string, cmdArgs []string) *Command

func (*Command) GetCmd

func (config *Command) GetCmd() (cmd *exec.Cmd)

func (*Command) GetEnv

func (config *Command) GetEnv() map[string]string

func (*Command) GetErrWriter

func (config *Command) GetErrWriter() io.WriteCloser

func (*Command) GetStdWriter

func (config *Command) GetStdWriter() io.WriteCloser

func (*Command) RunWithOutput

func (config *Command) RunWithOutput() (data []byte, err error)

type FileDetails

type FileDetails struct {
	Checksum entities.Checksum
	Size     int64
}

func GetFileDetails

func GetFileDetails(filePath string, includeChecksums bool) (details *FileDetails, err error)

type LevelType

type LevelType int
const (
	ERROR LevelType = iota
	WARN
	INFO
	DEBUG
)

type Log

type Log interface {
	Debug(a ...interface{})
	Info(a ...interface{})
	Warn(a ...interface{})
	Error(a ...interface{})
	Output(a ...interface{})
}

type NullLog

type NullLog struct {
}

NullLog is a logger that does nothing

func (*NullLog) Debug

func (nl *NullLog) Debug(...interface{})

func (*NullLog) Error

func (nl *NullLog) Error(...interface{})

func (*NullLog) Info

func (nl *NullLog) Info(...interface{})

func (*NullLog) Output

func (nl *NullLog) Output(...interface{})

func (*NullLog) Warn

func (nl *NullLog) Warn(...interface{})

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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