util

package
v0.0.0-...-9ce4fc3 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package util contains miscellaneous globally available utility functions.

Index

Constants

View Source
const (
	// Represents filesystem nodes, including directories.
	MaximumVisits = 5000
)

Variables

View Source
var IgnoredPaths = []string{
	".bundle",
	".cache",
	".cask",
	".config",
	".git",
	".hg",
	".local",
	".next",
	".npm",
	".svn",
	".tox",
	"__generated__",
	"__pycache__",
	"__tests__",
	"doc",
	"docs",
	"documentation",
	"examples",
	"node_modules",
	"test",
	"tests",
	"vendor",
	"venv",
}

IgnoredPaths is a slice of file patterns that are totally ignored by UPM. This means primarily that regexp searches won't descend into directories by these names. Gotcha: external tools run by UPM won't necessarily respect this list, although UPM makes an effort to tell them to when possible.

Functions

func AddIngoredPaths

func AddIngoredPaths(paths []string)

AddIngoredPaths globally appends to the IngoredPaths list.

func ChdirToUPM

func ChdirToUPM()

ChdirToUPM traverses upwards in the filesystem from the current directory until it finds a directory entry named .upm, and changes to the directory containing it. If it doesn't find any such directory entry, ChdirToUPM doesn't do anything. If UPM_PROJECT is set, it is used as the directory to which to change.

func CutPrefixes

func CutPrefixes(line string, options ...string) (nextfile string, found bool)

cutPrefixes offers strings.CutPrefix but for however many choices we may have

func DieConsistency

func DieConsistency(format string, a ...interface{})

func DieIO

func DieIO(format string, a ...interface{})

func DieInitializationError

func DieInitializationError(format string, a ...interface{})

func DieNetwork

func DieNetwork(format string, a ...interface{})

func DieOverwrite

func DieOverwrite(format string, a ...interface{})

func DieProtocol

func DieProtocol(format string, a ...interface{})

func DieSubprocess

func DieSubprocess(format string, a ...interface{})

func DieUnimplemented

func DieUnimplemented(format string, a ...interface{})

func DownloadFile

func DownloadFile(filepath string, url string)

DownloadFile emulates wget, overwriting any existing file. See https://golangcode.com/download-a-file-from-a-url/.

func Exists

func Exists(filename string) bool

Exists returns true if a directory entry by the given filename exists. If an I/O error occurs, FileExists terminates the process.

func GetCmdOutput

func GetCmdOutput(cmd []string) []byte

GetCmdOutput prints and runs the given command, returning its stdout as a string. Stderr goes to the terminal. GetCmdOutput exits the process on error or command failure.

func GetCmdOutputFallible

func GetCmdOutputFallible(cmd []string) ([]byte, error)

GetCmdOutputFallible prints and runs the given command, returning its stdout as a string. Stderr goes to the terminal. GetCmdOutputFallible does not exit the process on error or command failure, but instead returns an error.

func GetExitCode

func GetExitCode(cmd []string, printStdout bool, printStderr bool) int

GetExitCode runs a commands, and optionally prints the output to stdout and/or stderr, and it returns the exit code afterwards.

func GetResource

func GetResource(url string) string

GetResource returns a static resource as a string. Resources are inside the resources/ directory of the UPM source repository. url is HTTP-style, e.g. "/nodejs/bare-imports.js". The return value is the file contents. If the resource does not exist, GetResource panics.

func GetResourceBytes

func GetResourceBytes(url string) []byte

GetResourceBytes is like GetResource but returns a []byte.

func GuessWithTreeSitter

func GuessWithTreeSitter(ctx context.Context, root string, lang *sitter.Language, queryImports string, pathSegmentPatterns []string, ignorePathSegments map[string]bool) ([]string, error)

GuessWithTreeSitter guesses the imports of a directory using tree-sitter. For every file in dir that matches a pattern in searchGlobPatterns, but not in ignoreGlobPatterns, it will parse the file using lang and queryImports. When there's a capture tagged as `@import`, it reports the capture as an import. If there's a capture tagged as `@pragma` that's on the same line as an import, it will include the pragma in the results.

func Log

func Log(a ...interface{})

Log is like fmt.Println, but writes to stderr and is inhibited by --quiet.

func NotImplemented

func NotImplemented()

NotImplemented terminates the process, indicating that the operation is not implemented. This should be used in language backends that do not implement all of UPM's API.

func Panicf

func Panicf(format string, a ...interface{})

Panicf is a composition of fmt.Sprintf and panic.

func PatternExists

func PatternExists(pattern string) bool

PatternExists returns true if the given glob matches any file in the current directory.

func ProgressMsg

func ProgressMsg(msg string)

ProgressMsg prints the given message to stderr with a prefix. The message is inhibited in --quiet mode, however.

func Regexps

func Regexps(patterns []string) []*regexp.Regexp

Regexps compiles each provided pattern into a regexp object, and returns a slice of them.

func RunCmd

func RunCmd(cmd []string)

RunCmd prints and runs the given command, exiting the process on error or command failure. Stdout and stderr go to the terminal.

func SearchRecursive

func SearchRecursive(r *regexp.Regexp, patterns []string) [][]string

SearchRecursive does a recursive regexp search in the current directory. Only files whose basenames match one of the globs in patterns will be searched. The return value is a list of matches as would be returned by regexp.FindAllStringSubmatch. Matches are returned in a deterministic order. If an I/O error occurs, SearchRecursive terminates the process.

func TempDir

func TempDir() string

TempDir creates and returns the name of temporary directory. If creation fails, it terminates the process. The caller is responsible for cleaning up the temporary directory afterwards.

func TryWriteAtomic

func TryWriteAtomic(filename string, contents []byte)

TryWriteAtomic tries to write contents to filename atomically, retrying non-atomically if it can't. If both attempts fail, TryWriteAtomic terminates the process.

func WriteResource

func WriteResource(url string, tempdir string) string

WriteResource writes a static resource to a temporary directory. url is as in GetResource. The file is put inside tempdir, with the same basename as from url. If the resource does not exist, WriteResource panics. If the write fails, it terminates the process. Otherwise, it returns the name of the newly created file.

Types

type AuthorInfo

type AuthorInfo struct {

	// Probably "Firstname Lastname", although no given format is
	// actually enforced.
	Name string

	// Probably "something@example.com", although no given format
	// is actually enforced.
	Email string

	// Home page or personal website. Probably
	// "https://example.com/somewhere", although no given format
	// is actually enforced.
	URL string
}

AuthorInfo represents the author of a package. (Packages may only have one author in our world.)

func (AuthorInfo) String

func (a AuthorInfo) String() string

String returns a string representation of an author. For example, "Radon Rosborough <radon.neon@gmail.com>".

Jump to

Keyboard shortcuts

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