util

package
v0.0.0-...-b94456c Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2024 License: BSD-2-Clause Imports: 32 Imported by: 0

Documentation

Index

Constants

View Source
const AppName = "DART"
View Source
const FileIconGeneric = `<i class="far fa-file"></i>`
View Source
const STDIN_ERROR = -10000

Variables

View Source
var FileIconMap = map[string]string{}/* 187 elements not displayed */

FileIconMap maps strings to font awesome icons.

View Source
var MultipartSuffix = regexp.MustCompile("\\.b\\d+\\.of\\d+$")

MultipartSuffix pattern describes what APTrust's multipart bag suffix looks like. This is for APTrust-specific legacy support.

The tar files that make up multipart bags include a suffix that follows this pattern. For example, after stripping off the .tar suffix, you'll have a name like "my_bag.b04.of12"

View Source
var TarSuffix = regexp.MustCompile("\\.tar$")

TarSuffix matches strings that end with .tar

Functions

func Add

func Add(a, b int) int

Add adds a to b and returns the result. This is for use in templates.

func AlgorithmFromManifestName

func AlgorithmFromManifestName(filename string) (string, error)

func BagFileType

func BagFileType(name string) string

func CleanBagName

func CleanBagName(bagName string) string

CleanBagName returns the clean bag name. That's the tar file name minus the tar extension and any ".bagN.ofN" suffix.

func ContainsControlCharacter

func ContainsControlCharacter(str string) bool

ContainsControlCharacter returns true if string str contains a Unicode control character. We use this to test file names, which should not contain control characters.

func ContainsEscapedControl

func ContainsEscapedControl(str string) bool

ContainsEscapedControl returns true if string str contains something that looks like an escaped UTF-8 control character. The Mac OS file system seems to silently escape UTF-8 control characters. That causes problems when we try to copy a file over to another file system that won't accept the control character in a file name. The bag validator looks for file names matching these patterns and rejects them.

func CopyFile

func CopyFile(dest, src string) (int64, error)

CopyFile copies a file from src to dest

func CopyMap

func CopyMap[K, V comparable](orig map[K]V) map[K]V

CopyMap returns a copy of map orig

func DateISO

func DateISO(date time.Time) string

DateISO returns a date in format "2006-01-02"

func DateTimeISO

func DateTimeISO(date time.Time) string

DateTimeISO returns a date in format "2006-01-02T15:04:05Z"

func DateTimeUS

func DateTimeUS(date time.Time) string

DateUS returns a date in format "Jan 2, 2006 15:04:05"

func DateUS

func DateUS(date time.Time) string

DateUS returns a date in format "Jan 2, 2006"

func Dict

func Dict(values ...interface{}) (map[string]interface{}, error)

Dict returns an interface map suitable for passing into sub templates.

func DisplayDate

func DisplayDate(ts time.Time) string

DisplayDate returns a datetime in human-readable format. This returns an empty string if time is empty.

func EscapeAttr

func EscapeAttr(s string) template.HTMLAttr

EscapeAttr escapes an HTML attribute value. This helps avoid the ZgotmplZ problem.

func EscapeHTML

func EscapeHTML(s string) template.HTML

EscapeHTML returns an escaped HTML string. This helps avoid the ZgotmplZ problem.

func EstimatedChunkSize

func EstimatedChunkSize(totalSize float64) uint64

EstimatedChunkSize returns the size we should use for each chunk in a multipart S3 upload. If we don't tell Minio what chunk size to use, and it doesn't know the size of the total upload, it tries to allocate 5 GB of RAM. This causes some restorations to fail with an out of memory error. https://trello.com/c/1hkP28x1

Param totalSize is the total size of the object to upload. When restoring entire objects, we only know the approximate size, which will be IntellectualObject.FileSize plus one or more payload manifests and tag manifests of unknown size that we'll have to generate on the fly. In practice, we can guesstimate that the total size of a restored object will be about 1.01 - 1.1 times IntellectualObject.FileSize.

Since S3 max upload size is 5 TB with 10k parts, the max this will return is 500MB for part size. Although we could return 5 GB, we don't want to because we can't allocate that much memory inside of memory-limited docker instances.

func ExecCommand

func ExecCommand(command string, args []string, env []string, stdinData []byte) (stdout, stderr []byte, exitCode int)

ExecCommand executes a command and returns the output of STDOUT and STDERR as well as the exit code.

os/exec has some problems when piping stdin. These show up on Travis/Linux, but not Mac. See https://github.com/golang/go/issues/9307

func ExpandTilde

func ExpandTilde(filePath string) (string, error)

Expands the tilde in a directory path to the current user's home directory. For example, on Linux, ~/data would expand to something like /home/josie/data

func FileExists

func FileExists(path string) bool

Returns true if the file at path exists, false if not.

func FileIconFor

func FileIconFor(filepath string) template.HTML

FileIconFor returns a FontAwesome icon for the specified file type, as defined in util.FileIconMap. If the icon map has no entry for this type, this returns util.FileIconGeneric.

func FindCommonPrefix

func FindCommonPrefix(paths []string) string

FindCommonPrefix finds the common prefix in a list of strings. This is used during bagging to trim off extraneous characters from file paths to help determine what their path should be inside the bag.

For example, if we're bagging 100 files that all look like this:

/user/linus/photos/image1.jpg /user/linus/photos/image2.jpg etc.

We don't want the bag's payload directory to look like this:

data/user/linus/photos/image1.jpg data/user/linus/photos/image2.jpg etc.

We want it to look like this:

data/image1.jpg data/image2.jpg etc.

Note that this function has the side effect of sorting the list of paths. While side effects are generally undesirable, this one is OK in the bagging context as it makes the manifests easy to read and tar file structure predictable.

Also note that there may be cases where the list has no common prefix. This will happen, for example, if the user is bagging two directories like "/var/www" and "images".

func GetHashes

func GetHashes(algs []string) map[string]hash.Hash

func GetLogger

func GetLogger(level logging.Level) *logging.Logger

func GetWindowsDrives

func GetWindowsDrives() []string

GetWindowsDrives returns a list of Windows drives.

func HasValidExtensionForMimeType

func HasValidExtensionForMimeType(filename, mimeType string) (bool, error)

func HumanSize

func HumanSize(size int64) string

HumanSize returns a number of bytes in a human-readable format. Note that we use base 1024, not base 1000, because AWS uses 1024 to calculate the storage size of the objects we're reporting on.

func IntListContains

func IntListContains(list []int, item int) bool

IntListContains returns true if list contains item.

func IsDirectory

func IsDirectory(path string) bool

Returns true if path is a directory.

func IsEmpty

func IsEmpty(s string) bool

IsEmpty returns true if string s is empty. Strings that are all whitespace are considered empty.

func IsEmptyStringList

func IsEmptyStringList(list []string) bool

IsEmptyStringList returns true if list contains no items or if all items in list are empty strings.

func IsListType

func IsListType(obj interface{}) bool

IsListType returns true if obj is a slice or array.

func IsMapType

func IsMapType(obj interface{}) bool

IsMapType returns true if obj is a map.

func LooksLikeHypertextURL

func LooksLikeHypertextURL(str string) bool

LooksLikeHypertextURL returns true if str looks like an HTTP or HTTPS URL.

func LooksLikeManifest

func LooksLikeManifest(name string) bool

func LooksLikePayloadFile

func LooksLikePayloadFile(name string) bool

func LooksLikeTagManifest

func LooksLikeTagManifest(name string) bool

func LooksLikeURL

func LooksLikeURL(url string) bool

LooksLikeURL returns true if url looks like a URL.

func LooksLikeUUID

func LooksLikeUUID(uuid string) bool

LooksLikeUUID returns true if uuid looks like a valid UUID.

func LooksSafeToDelete

func LooksSafeToDelete(dir string, minLength, minSeparators int) bool

Returns true if the path specified by dir has at least minLength characters and at least minSeparators path separators. This is for testing paths you want pass into os.RemoveAll(), so you don't wind up deleting "/" or "/etc" or something catastrophic like that.

func MakeTempCSVFileWithValidPaths

func MakeTempCSVFileWithValidPaths(t *testing.T, pathToCSVFile string) string

Paths in pastbuild_test_batch.csv file are relative. Create a temp file with absolute paths.

func Min

func Min(x, y int) int

Min returns the minimum of x or y without all the casting required by the math package.

func Mod

func Mod(a, b int) bool

Mod returns true of a modulus b equals zero.

func NewLine

func NewLine() string

NewLine returns the correct newline character for the current system, either "\r\n" or "\n".

func PathTo

func PathTo(program string) (string, error)

PathTo returns the path to the specified program. Note that this can be spotty on Windows. We'll need to watch this, log "not found" errors and work from there.

func PathToTestData

func PathToTestData() string

PathToTestData returns the path to the directory containing test data. This is used only in testing.

func PathToUnitTestBag

func PathToUnitTestBag(bagName string) string

func PrintAndExit

func PrintAndExit(message string)

PrintAndExit prints a message to STDERR and exits

func ProjectRoot

func ProjectRoot() string

ProjectRoot returns the project root.

func ReadFile

func ReadFile(filepath string) ([]byte, error)

ReadFile reads an entire file into a byte array.

func RemoveFromSlice

func RemoveFromSlice[T any](list []T, index int) []T

RemoveFromSlice removes the item at index index from slice, returning a new slice.

func RunningInCI

func RunningInCI() bool

RunningInCI returns true when code is running in the Travis CI environment.

func SetBoolValue

func SetBoolValue(obj interface{}, fieldName string, value bool) error

SetBoolValue sets the value of obj.fieldName to value. Returns an error if the field does not exist, or is the wrong type, or cannot be set.

func SetFloatValue

func SetFloatValue(obj interface{}, fieldName string, value float64) error

SetFloatValue sets the value of obj.fieldName to value. Returns an error if the field does not exist, or is the wrong type, or cannot be set.

func SetIntValue

func SetIntValue(obj interface{}, fieldName string, value int64) error

SetIntValue sets the value of obj.fieldName to value. Returns an error if the field does not exist, or is the wrong type, or cannot be set.

func SetStringValue

func SetStringValue(obj interface{}, fieldName, value string) error

SetStringValue sets the value of obj.fieldName to value. Returns an error if the field does not exist, or is the wrong type, or cannot be set.

func SplitAndTrim

func SplitAndTrim(s, sep string) []string

SlitAndTrim splits string s on the specified separator, then trims leading and trailing whitespace from each item in the resulting slice. Returns a slice of trimmed strings.

func StrEq

func StrEq(val1, val2 interface{}) bool

StrEq compares the string representation of two values and returns true if they are equal.

func StrStartsWith

func StrStartsWith(s, prefix string) bool

StrStartsWith returns true if string s starts with the specified prefix.

func StringIsShellSafe

func StringIsShellSafe(s string) bool

StringIsShellSafe returns true if string looks safe to pass to shell.

func StringListContains

func StringListContains(list []string, item string) bool

StringListContains returns true if the list of strings contains item.

func StringListContainsAll

func StringListContainsAll(masterList []string, listToCheck []string) bool

StringListContainsAll returns true if all items in listToCheck are also in masterList. Be sure you pass the params in the right order. Note that this can get expensive if the lists are long.

func StringToBool

func StringToBool(value string) (bool, error)

StringToBool converts a string to its boolean value, or returns an error if it can't convert the string. In addition to the values supported by strconv.ParseBool, this also supports "yes", "y", "no" and "n". Those strings are case-insensitive.

func StripFileExtension

func StripFileExtension(filename string) string

StripFileExtension returns filename, minus the extension. For example, "my_bag.tar" returns "my_bag".

func StripNonPrintable

func StripNonPrintable(str string) string

StripNonPrintable returns a copy of str with non-printable characters removed.

func TarPathToBagPath

func TarPathToBagPath(name string) (string, error)

TarPathToBagPath, given the path of a file inside a tarball, returns the path of the file in a bag. The name param generally comes from the Name property of a tar file header. For example, in a tar file called my_bag.tar the paths would translate as follows:

Input -> Output my_bag/bagit.txt -> bagit.txt my_bag/data/file.docx -> data/file.docx my_bag/data/img/photo.jpg -> data/img/photo.jpg

This function assumes (perhaps dangerously) that tarred bags follow the recommdation of pre-1.0 versions of the BagIt spec that say a tarred bag should deserialize to a single top-level directory. This function does not assume that the directory will match the bag name.

func TestsAreRunning

func TestsAreRunning() bool

TestsAreRunning returns true when code is running under "go test"

func ToHumanSize

func ToHumanSize(size, unit int64) string

ToHumanSize converts a raw byte count (size) to a human-friendly representation.

func Truncate

func Truncate(value string, length int) string

Truncate truncates the value to the given length, appending an ellipses to the end. If value contains HTML tags, they will be stripped because truncating HTML can result in unclosed tags that will ruin the page layout.

func TruncateMiddle

func TruncateMiddle(str string, maxLen int) string

TruncateMiddle trims str to maxLen by removing them from the middle of the string. It adds dots to the middle of the string to indicate text was trimmed.

func TruncateStart

func TruncateStart(str string, maxLen int) string

TruncateStart trims str to maxLen by removing them from the start of the string. It adds leading dots to indicate some text was trimmed.

func UCFirst

func UCFirst(str string) string

UCFirst returns string str with the first letter capitalized and all others lower case.

func UnixToISO

func UnixToISO(ts int64) string

UnixToISO converts a Unix timestamp to ISO format.

func YesNo

func YesNo(value bool) string

YesNo returns "Yes" if value is true, "No" if value is false.

func YesOrNo

func YesOrNo(value bool) string

YesOrNo returns "Yes" if value is true, "No" if value is false.

Types

type DirectoryStats

type DirectoryStats struct {
	RootIsFile bool
	FullPath   string
	BaseName   string
	DirCount   int
	Error      string
	FileCount  int
	TotalBytes int64
}

func DirStats

func DirStats(dir string) *DirectoryStats

DirStats returns stats about a directory. This is used in the job files display to show things like total file count, total number of bytes, etc.

func GetDirectoryStats

func GetDirectoryStats(dir string) *DirectoryStats

GetDirectoryStats returns the file count, directory count and total number of bytes found recursively under directory dir.

type ExtendedFileInfo

type ExtendedFileInfo struct {
	os.FileInfo
	FullPath string
}

ExtendedFileInfo adds some data to os.FileInfo including FullPath, Owner() and Group(). The latter two are platform specific. They return zero on Windows and actual ids on posix systems.

func ListDirectory

func ListDirectory(dir string) ([]*ExtendedFileInfo, error)

ListDirectory returns a list of directory contents one level deep. It does not recurse. The list will contain directories first, followed by files.

func ListDirectoryWithSort

func ListDirectoryWithSort(dir string) ([]*ExtendedFileInfo, error)

ListDirectoriesWithSort returns a list of all items in the specified directory, showing folders first, then files. Folders and files are sorted in alpha order, case insensitive.

func NewExtendedFileInfo

func NewExtendedFileInfo(path string, fileInfo os.FileInfo) *ExtendedFileInfo

NewExtendedFileInfo creates a new ExtendedFileInfo object. This takes two params because RecursiveFileList stats the files for us. Otherwise, we'd do it in the constructor.

func RecursiveFileList

func RecursiveFileList(dir string, includeIrregulars bool) ([]*ExtendedFileInfo, error)

RecursiveFileList a list of all items inside of dir. If includeIrregulars is false, this will NOT return links, pipes, devices, or anything else besides regular files and directories.

We generally do want to omit items like symlinks, pipes, etc. when bagging because we cannot bag them.

func (*ExtendedFileInfo) OwnerAndGroup

func (fi *ExtendedFileInfo) OwnerAndGroup() (uid int, gid int)

OwnerAndGrooup returns the file's owner id and group id on posix systems. Returns zero, zero on Windows.

type NameValuePair

type NameValuePair struct {
	Name  string
	Value string
}

NameValuePair contains a name and value. Use this to keep ordered lists of pairs, and lists that contain multiple values for the same named key. Go's maps can't do either of those things.

type NameValuePairList

type NameValuePairList struct {
	Items []NameValuePair
}

func NewNameValuePairList

func NewNameValuePairList() *NameValuePairList

func ParseCSV

func ParseCSV(pathToCSVFile string) ([]string, []*NameValuePairList, error)

ParseCSV parses the CSV file at pathToCSVFile and returns the following:

1. A slice of strings containing the entries in the first line of the file. These are assumed to be headers / field names.

2. A slice of url.Values objects in which names are column headers and values are the values parsed from one line of the file. We use url.Values because, unlike a map, it preserves the order of the fields and allows us to have multiple values per key. This is essential as the BagIt spec allows a tag value to be specified multiple times.

3. An error, which will typically be one of: "file does not exist", "file can't be read (permissions)" or "csv parse error".

This will not parse correctly if the first line of the file does not contain headers.

func (*NameValuePairList) Add

func (list *NameValuePairList) Add(name, value string)

func (*NameValuePairList) AllMatching

func (list *NameValuePairList) AllMatching(name string) []NameValuePair

func (*NameValuePairList) FirstMatching

func (list *NameValuePairList) FirstMatching(name string) (NameValuePair, bool)

type Paths

type Paths struct {
	Cache     string
	Config    string
	DataDir   string
	Desktop   string
	Documents string
	Downloads string
	Music     string
	Photos    string
	Videos    string
	Home      string
	Public    string
	LogDir    string
	TempDir   string
	Root      string
	UserMount string
	// contains filtered or unexported fields
}

Paths contains paths to common directories used by DART. This was ported from https://github.com/sindresorhus/env-paths so that paths in DART3 will match paths used in prior versions of DART.

func NewPaths

func NewPaths() *Paths

NewPaths returns a Paths struct appropriate to the current operating system. Currently supports only Windows, Mac and Linux.

func (*Paths) DefaultPaths

func (p *Paths) DefaultPaths() ([]*ExtendedFileInfo, error)

DefaultPaths returns a list of ExtendedFileInfo objects describing which directories we should should by default in our file browser.

func (*Paths) LogFile

func (p *Paths) LogFile() (string, error)

Jump to

Keyboard shortcuts

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