Documentation ¶
Index ¶
- func CheckDigest(digest string, path string, log Log) error
- func Close(f io.Closer)
- func CombineErrors(errs ...error) error
- func ComputeEtag(path string) (string, error)
- func CopyFile(sourcePath string, destinationPath string, log Log) error
- func Digest(r io.Reader) (string, error)
- func DigestForFileAtPath(path string) (string, error)
- func DiscardAndCloseBody(resp *http.Response) error
- func DiscardAndCloseBodyIgnoreError(resp *http.Response)
- func DownloadURL(urlString string, destinationPath string, options DownloadURLOptions) error
- func EnvBool(envVar string, defaultValue bool) bool
- func EnvDuration(envVar string, defaultValue time.Duration) time.Duration
- func FileExists(path string) (bool, error)
- func FileModTime(path string) (time.Time, error)
- func IsDirReal(path string) (bool, error)
- func JoinPredicate(arr []string, delimeter string, f func(s string) bool) string
- func MakeDirs(dir string, mode os.FileMode, log Log) error
- func MakeParentDirs(path string, mode os.FileMode, log Log) error
- func MakeTempDir(prefix string, mode os.FileMode) (string, error)
- func MoveFile(sourcePath string, destinationPath string, tmpDir string, log Log) error
- func PathFromURL(u *url.URL) string
- func RandBytes(length int) ([]byte, error)
- func RandomID(prefix string) (string, error)
- func ReadFile(path string) ([]byte, error)
- func RemoveFileAtPath(path string)
- func SaveHTTPResponse(resp *http.Response, savePath string, mode os.FileMode, log Log) error
- func Semver(version string) string
- func TempPath(tempDir string, prefix string) string
- func Touch(path string) error
- func URLExists(urlString string, timeout time.Duration, log Log) (bool, error)
- func URLStringForPath(path string) string
- func URLValueForBool(b bool) string
- func Unzip(sourcePath, destinationPath string, log Log) error
- func UnzipOver(sourcePath string, path string, destinationPath string, ...) error
- func UnzipPath(sourcePath string, log Log) (string, error)
- func WriteTempFile(prefix string, data []byte, mode os.FileMode) (string, error)
- type DownloadURLOptions
- type File
- type Log
- type SafeWriter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckDigest ¶
CheckDigest returns no error if digest matches file
func Close ¶
Close closes a file and ignores the error. This satisfies lint checks when using with defer and you don't care if there is an error, so instead of:
defer func() { _ = f.Close() }() defer Close(f)
func CombineErrors ¶
CombineErrors returns a single error for multiple errors, or nil if none
func ComputeEtag ¶
ComputeEtag returns etag for a file at path
func CopyFile ¶
CopyFile copies a file safely. It will create parent directories for destinationPath if they don't exist. It will overwrite an existing destinationPath.
func DigestForFileAtPath ¶
DigestForFileAtPath returns a SHA256 digest for file at specified path
func DiscardAndCloseBody ¶
DiscardAndCloseBody reads as much as possible from the body of the given response, and then closes it.
This is because, in order to free up the current connection for re-use, a response body must be read from before being closed; see http://stackoverflow.com/a/17953506 .
Instead of doing:
res, _ := ... defer res.Body.Close()
do
res, _ := ... defer DiscardAndCloseBody(res)
instead.
func DiscardAndCloseBodyIgnoreError ¶
DiscardAndCloseBodyIgnoreError calls DiscardAndCloseBody. This satisfies lint checks when using with defer and you don't care if there is an error, so instead of:
defer func() { _ = DiscardAndCloseBody(resp) }() defer DiscardAndCloseBodyIgnoreError(resp)
func DownloadURL ¶
func DownloadURL(urlString string, destinationPath string, options DownloadURLOptions) error
DownloadURL downloads a URL to a path.
func EnvBool ¶
EnvBool returns a bool from an environment variable or default if invalid or not specified
func EnvDuration ¶
EnvDuration returns a duration from an environment variable or default if invalid or not specified
func FileExists ¶
FileExists returns whether the given file or directory exists or not
func FileModTime ¶
FileModTime returns modification time for file. If file doesn't exist returns error.
func IsDirReal ¶
IsDirReal returns true if directory exists and is a real directory (not a symlink). If it returns false, an error will be set explaining why.
func JoinPredicate ¶
JoinPredicate joins strings with predicate
func MakeParentDirs ¶
MakeParentDirs ensures parent directory exist for path
func MakeTempDir ¶
MakeTempDir creates a unique temp directory.
For example:
MakeTempDir("Test.", 0700)
func MoveFile ¶
MoveFile moves a file safely. It will create parent directories for destinationPath if they don't exist. If the destination already exists and you specify a tmpDir, it will move it there, otherwise it will be removed.
func PathFromURL ¶
PathFromURL returns path for file URL scheme For example,
file:///usr/local/go/bin => /usr/local/go/bin file:///C:/Go/bin => C:\Go\bin
func RandomID ¶
RandomID returns random ID (base32) string with prefix, using 256 bits as recommended by tptacek: https://gist.github.com/tqbf/be58d2d39690c3b366ad
func RemoveFileAtPath ¶
func RemoveFileAtPath(path string)
RemoveFileAtPath removes a file at path (and any children) ignoring any error. We do nothing if path == "". This satisfies lint checks when using with defer and you don't care if there is an error, so instead of:
defer func() { _ = os.Remove(path) }() defer RemoveFileAtPath(path)
func SaveHTTPResponse ¶
SaveHTTPResponse saves an http.Response to path
func TempPath ¶
TempPath returns a temporary unique file path. If for some reason we can't obtain random data, we still return a valid path, which may not be as unique. If tempDir is "", then os.TempDir() is used.
func URLStringForPath ¶
URLStringForPath returns an URL as string with file scheme for path. For example,
/usr/local/go/bin => file:///usr/local/go/bin C:\Go\bin => file:///C:/Go/bin
func URLValueForBool ¶
URLValueForBool returns "1" for true, otherwise "0"
func Unzip ¶
Unzip unpacks a zip file to a destination. This unpacks files using the current user and time (it doesn't preserve). This code was modified from https://stackoverflow.com/questions/20357223/easy-way-to-unzip-file-with-golang/20357902
func UnzipOver ¶
func UnzipOver(sourcePath string, path string, destinationPath string, check func(sourcePath, destinationPath string) error, tmpDir string, log Log) error
UnzipOver safely unzips a file and copies it contents to a destination path. If destination path exists, it will be removed first. The filename must have a ".zip" extension. You can specify a check function, which will run before moving the unzipped directory into place. If you specify a tmpDir and destination path exists, it will be moved there instead of being removed.
To unzip Keybase-1.2.3.zip and move the contents Keybase.app to /Applications/Keybase.app
UnzipOver("/tmp/Keybase-1.2.3.zip", "Keybase.app", "/Applications/Keybase.app", check, "", log)
Types ¶
type DownloadURLOptions ¶
type DownloadURLOptions struct { Digest string RequireDigest bool UseETag bool Timeout time.Duration Log Log }
DownloadURLOptions are options for DownloadURL
type File ¶
type File struct {
// contains filtered or unexported fields
}
File uses a safer file API
func (File) GetFilename ¶
GetFilename returns the file name for SafeWriter