utils

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2023 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Examples: Vendor::PackName@x.y.z, Vendor.PackName.x.y.z
	ExactVersion int = 0

	// Example: Vendor::PackName@latest
	LatestVersion = 1

	// Examples: Vendor::PackName, Vendor.PackName
	AnyVersion = 2

	// Example: Vendor::PackName>=x.y.z
	GreaterVersion = 3

	// Example: Vendor::PackName@~x.y.z (the greatest version of the pack keeping the same major number)
	GreatestCompatibleVersion = 4

	// For the <package/requirements/packages> spec only. Example: Vendor.PackName.a.b.c:x.y.z
	RangeVersion = 5
)

The version modifiers below are helpers to determine how to interpret the version specified by the packID.

View Source
const DownloadBufferSize = 4096

DownloadBufferSize is the number of bytes to transfer from the stream to the downloaded file per iteration. It is 4kb

Variables

View Source
var (
	// File RO (ReadOnly) and RW (Read + Write) modes
	FileModeRO = fs.FileMode(0444)
	FileModeRW = fs.FileMode(0666)

	// Directory RO (ReadOnly + Traverse) and RW (Read + Write + Traverse) modes
	DirModeRO = fs.FileMode(0555)
	DirModeRW = fs.FileMode(0777)
)
View Source
var CacheDir string

CacheDir is used for cpackget to temporarily host downloaded pack files before moving it to CMSIS_PACK_ROOT

View Source
var HTTPClient *http.Client
View Source
var MaxDownloadSize = int64(20 * 1024 * 1024 * 1024)

MaxDownloadSize determines that the max file to be downloaded. Defaults to 20G It prevents malicious requests from providing infinite or very long files

View Source
var ShouldAbortFunction func() bool

ShouldAbortFunction is a function that determines whether early termination was requested by the user

Functions

func CheckConnection added in v1.1.0

func CheckConnection(url string, timeOut int) error

func CleanPath added in v0.5.0

func CleanPath(path string) string

func CopyFile

func CopyFile(source, destination string) error

CopyFile copies the contents of source into a new file in destination

func CountLines added in v0.2.0

func CountLines(content string) int

CountLines returns the number of lines in a string Ref: https://stackoverflow.com/a/24563853

func DirExists added in v0.2.0

func DirExists(dirPath string) bool

DirExists checks if dirPath is an actual directory in the local file system

func DownloadFile

func DownloadFile(URL string, timeout int) (string, error)

DownloadFile downloads a file from an URL and saves it locally under destionationFilePath

func EnsureDir

func EnsureDir(dirName string) error

EnsureDir recursevily creates a directory tree if it doesn't exist already

func FileExists

func FileExists(filePath string) bool

FileExists checks if filePath is an actual file in the local file system

func FilterPackID added in v0.5.1

func FilterPackID(content string, filter string) string

FilterPackId returns the original string if any of the received filter words are present - designed specifically to filter pack IDs

func FormatPackVersion added in v0.9.2

func FormatPackVersion(pack []string) string

FormatPackVersion returns a modern representation of an internally versioned pack (for dependencies). Example: CMSIS,ARM,5.6.0:_ -> ARM::CMSIS@>=5.6.0 Ref: https://github.com/Open-CMSIS-Pack/devtools/blob/main/tools/projmgr/docs/Manual/YML-Input-Format.md#pack-name-conventions

func GetEncodedProgress added in v1.0.0

func GetEncodedProgress() bool

func GetSkipTouch added in v1.0.0

func GetSkipTouch() bool

func IsBase64 added in v0.8.3

func IsBase64(s string) bool

IsBase64 tells whether a string is correctly b64 encoded.

func IsEmpty

func IsEmpty(dir string) bool

IsEmpty tells whether a directory specified by "dir" is empty or not

func IsPackNameValid

func IsPackNameValid(packName string) bool

IsPackNameValid checks whether a pack name string matches specified regular expression.

func IsPackVendorNameValid

func IsPackVendorNameValid(vendorName string) bool

IsVendorNameValid checks whether a pack vendor name string matches specified regular expression.

func IsPackVersionValid

func IsPackVersionValid(packVersion string) bool

IsPackVersion checks whether a pack version string matches specified regular expression

func IsTerminalInteractive added in v0.2.0

func IsTerminalInteractive() bool

IsTerminalInteractive tells whether or not the current terminal is capable of complex interactions

func ListDir

func ListDir(dir, pattern string) ([]string, error)

ListDir generates a list of files and directories in "dir". If pattern is specified, generates a list with matches only. It does NOT walk subdirectories

func MoveFile

func MoveFile(source, destination string) error

MoveFile moves a file from one source to destination

func RandStringBytes

func RandStringBytes(n int) string

RandStringBytes returns a random string with n bytes long Ref: https://stackoverflow.com/a/31832326/3908350

func ReadXML

func ReadXML(path string, targetStruct interface{}) error

ReadXML reads in a file into an XML struct

func SecureCopy

func SecureCopy(dst io.Writer, src io.Reader) (int64, error)

SecureCopy avoids potential DoS vulnerabilities when downloading a stream from a remote origin or decompressing a file. Ref: G110: Potential DoS vulnerability via decompression bomb (https://cwe.mitre.org/data/definitions/409.html)

func SecureInflateFile

func SecureInflateFile(file *zip.File, destinationDir, stripPrefix string) error

SecureInflateFile avoids potentions file traversal vulnerabilities when inflating compressed files. It avoids extracting files with "../" if stripPrefix is provided, use that to strip file.Name files

func SemverCompare added in v0.6.0

func SemverCompare(version1, version2 string) int

SemverCompare extends `semver.Compare` to work with leading zeros

func SemverMajor added in v0.6.0

func SemverMajor(version string) string

SemverMajor extends `semver.Major` to work with leading zeros

func SetEncodedProgress added in v1.0.0

func SetEncodedProgress(encodedProgress bool)

func SetReadOnly added in v0.7.0

func SetReadOnly(path string)

SetReadOnly takes in a file or directory and set it to read-only mode. Should work on both Windows and Linux.

func SetReadOnlyR added in v0.7.0

func SetReadOnlyR(path string)

SetReadOnlyR works the same as SetReadOnly, except that it is recursive

func SetSkipTouch added in v1.0.0

func SetSkipTouch(skipTouch bool)

func StartSignalWatcher added in v0.3.0

func StartSignalWatcher()

startSignalWatcher spins off a thread monitoring termination signals and retuns a function that returns whether termination was requested

func StopSignalWatcher added in v0.3.0

func StopSignalWatcher()

stopSignalWatcher sends a fake signal to the monitoring thread making it terminate

func TouchFile

func TouchFile(filePath string) error

TouchFile touches the file specified by filePath. If the file does not exist, create it. Touch also updates the modified timestamp of the file.

func UnsetReadOnly added in v0.7.0

func UnsetReadOnly(path string)

UnsetReadOnly takes in a file or directory and set it to read-only mode. Should work on both Windows and Linux.

func UnsetReadOnlyR added in v0.7.0

func UnsetReadOnlyR(path string)

UnsetReadOnlyR works the same as UnsetReadOnly, but recursive

func WriteXML

func WriteXML(path string, targetStruct interface{}) error

WriteXML writes an XML struct to a file

Types

type EncodedProgress added in v1.0.0

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

func NewEncodedProgress added in v1.0.0

func NewEncodedProgress(max int64, instNo int, filename string) *EncodedProgress

func (*EncodedProgress) Add added in v1.0.0

func (p *EncodedProgress) Add(count int) int

func (*EncodedProgress) Print added in v1.0.0

func (p *EncodedProgress) Print()

Encodes information to show progress when called by GUI or other tools * I: Instance number (always counts up), connected to the filename * F: Filename currently processed * T: Total bytes of file or numbers of files * P: Currently processed percentage * C: Currently processed bytes or numbers of files * J: Total number of files beeing processed * L: License file follows * O: Online connection Status [offline|online]

func (*EncodedProgress) Write added in v1.0.0

func (p *EncodedProgress) Write(bs []byte) (int, error)

type PackInfo

type PackInfo struct {
	Location, Vendor, Pack, Version, Extension string
	IsPackID                                   bool
	VersionModifier                            int
}

PackInfo defines a basic pack information set

func ExtractPackInfo

func ExtractPackInfo(packPath string) (PackInfo, error)

ExtractPackInfo takes in a path to a pack and extracts the needed information. It returns an error if any information is wrong Valid packPath's are: - /path/to/dev/Vendor.Pack.pdsc - /path/to/local/Vendor.Pack.Version.pack (or .zip) - https://web.com/Vendor.Pack.Version.pack (or .zip) If short is true, then prepare it considering that path is in the simpler form of Vendor.Pack[.x.y.z], used when removing packs/pdscs. NOTE: a malformed packPath e.g. "my.pack" DOES look like a valid

pack name, with "my" for vendor and "pack" for pack name.

type TimeoutTransport added in v0.7.1

type TimeoutTransport struct {
	http.Transport
	RoundTripTimeout time.Duration
}

func (*TimeoutTransport) RoundTrip added in v0.7.1

func (t *TimeoutTransport) RoundTrip(req *http.Request) (*http.Response, error)

Helper function to set timeouts on HTTP connections that use keep-alive connections (the most common one)

Jump to

Keyboard shortcuts

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