Documentation ¶
Overview ¶
Package util contains miscellaneous globally available utility functions.
Index ¶
- Constants
- Variables
- func AddIngoredPaths(paths []string)
- func ChdirToUPM()
- func CutPrefixes(line string, options ...string) (nextfile string, found bool)
- func DieConsistency(format string, a ...interface{})
- func DieIO(format string, a ...interface{})
- func DieInitializationError(format string, a ...interface{})
- func DieNetwork(format string, a ...interface{})
- func DieOverwrite(format string, a ...interface{})
- func DieProtocol(format string, a ...interface{})
- func DieSubprocess(format string, a ...interface{})
- func DieUnimplemented(format string, a ...interface{})
- func DownloadFile(filepath string, url string)
- func Exists(filename string) bool
- func GetCmdOutput(cmd []string) []byte
- func GetCmdOutputFallible(cmd []string) ([]byte, error)
- func GetExitCode(cmd []string, printStdout bool, printStderr bool) int
- func GetResource(url string) string
- func GetResourceBytes(url string) []byte
- func GuessWithTreeSitter(ctx context.Context, root string, lang *sitter.Language, queryImports string, ...) ([]string, error)
- func Log(a ...interface{})
- func NotImplemented()
- func Panicf(format string, a ...interface{})
- func PatternExists(pattern string) bool
- func ProgressMsg(msg string)
- func Regexps(patterns []string) []*regexp.Regexp
- func RunCmd(cmd []string)
- func SearchRecursive(r *regexp.Regexp, patterns []string) [][]string
- func TempDir() string
- func TryWriteAtomic(filename string, contents []byte)
- func WriteResource(url string, tempdir string) string
- type AuthorInfo
Constants ¶
const (
// Represents filesystem nodes, including directories.
MaximumVisits = 5000
)
Variables ¶
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 ¶
cutPrefixes offers strings.CutPrefix but for however many choices we may have
func DieConsistency ¶
func DieConsistency(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 ¶
DownloadFile emulates wget, overwriting any existing file. See https://golangcode.com/download-a-file-from-a-url/.
func Exists ¶
Exists returns true if a directory entry by the given filename exists. If an I/O error occurs, FileExists terminates the process.
func GetCmdOutput ¶
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 ¶
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 ¶
GetExitCode runs a commands, and optionally prints the output to stdout and/or stderr, and it returns the exit code afterwards.
func GetResource ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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>".