fsutil

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: MIT Imports: 12 Imported by: 4

README

fsutil

This cross-platform go module provides a lightweight abstraction of common file system methods:

  • Touch(path string): Like the Unix touch command. Returns a string with the absolute path of the file/directory.
  • Mkdirp(path string): Like the Unix mkdir -p command. Returns a string with the absolute path of the directory.
  • Exists(path string): Returns a boolean indicating true if the path exists and false if it does not.
  • Abs(path string): Returns the absolute path as a string. Unlike the native filepath.Abs, this method always returns a string (and only a string, no error). This method does not depend on the existence of the directory. Relative paths are always resolved from the current working directory.
  • Clean(path string): This method ensures an empty directory exists at the specified path.
  • IsFile(path string): Returns a boolean value indicating true if the path resolves to a file and false if it does not.
  • IsDirectory(path string): Returns a boolean value indicating true if the path resolves to a directory and false if it does not.
  • IsSymlink(path string) bool: Determines if a path is a symbolic link.
  • ReadTextFile(path string): Reads a text file and returns a string.
  • WriteTextFile(path string, content string, <os.FileMode>): Writes a text file from a string. Optionally accepts a file mode.
  • IsReadable(path string) bool: Determines whether the path is readable.
  • IsWritable(path string) bool: Determines whether the path is writable.
  • IsExecutable(path string) bool: Determines whether the path has execute permissions.
  • ByteSize(path string): Determines the size (in bytes) of a file or directory.
  • Size(path string, decimalPlaces int): A "pretty" label for the size of a file or directory. For example, 3.14MB.
  • FormatSize(size int64, decimalPlaces int): Pretty-print the byte size, i.e. 3.14MB.
  • Copy(source string, target string, ignoreErrors ...bool) error: Copy a file/directory contents. Ignores symlinks. Optionally specify true as the last argument to ignore errors.
  • Move(source string, target string, ignoreErrors ...bool) error: Move a file/directory contents. Ignores symlinks. Optionally specify true as the last argument to ignore errors.
  • Unzip(source string, target string) error: Unzip a file into the target directory.
  • Zip(source string, target string) error: Zip a file/directory into the target directory/filename.

Example

package main

import (
  "github.com/coreybutler/go-fsutil"
)

func main() {
  fsutil.Touch("./path/to/test.txt")
}

The code above would create an empty file called test.txt in <<current working directory>>/path/to. If the directory does not exist, it will be created.


For complete details, see the Godoc. Examples are available in the test files.

Notice

This API is stable, but subject to additions. I work on it whenever a "common file system need" comes up in other projects. As a result, the 1.0.X release cycle will continue to receive new feature additions until I consider the API "well-defined". This deviates a tiny bit from traditional semantic versioning, because new "features" are being added in patch releases.

Upon the release of a 1.1.0 version, the API will be considered "well-defined" and will adhere more strictly to semantic versioning practices.

Documentation

Index

Constants

View Source
const GB float64 = 1024 * MB

GB represents the size of a giggabyte.

View Source
const KB float64 = 1024

KB represents the size of a kilobyte.

View Source
const MB float64 = 1024 * KB

MB represents the size of a megabyte.

View Source
const PB float64 = 1024 * TB

PB represents the size of a petabyte.

View Source
const TB float64 = 1024 * GB

TB represents the size of a terabyte.

Variables

This section is empty.

Functions

func Abs

func Abs(path string) string

Returns the fully resolved path, even if the path does not exist.

``` fsutil.Abs("./does/not/exist") ``` If the code above was run within `/home/user`, the result would be `/home/user/does/not/exist`.

func ByteSize added in v1.0.8

func ByteSize(path string) (int64, error)

ByteSize returns the number of bytes (size) of a file/directory.

func Clean

func Clean(path string)

Clean will ensure the specified directory exists. If the directory already exists, all of contents are deleted. If the directory does not exist, it is automatically created.

func Copy added in v1.0.9

func Copy(source string, dest string, ignoreErrors ...bool) error

Copy a file/directory

func Exists

func Exists(path string) bool

Exists is a helper method to quickly determine whether a directory or file exists.

func FormatSize added in v1.0.8

func FormatSize(bytesize int64, sigfig ...int) string

FormatSize returns a nicely formatted representation of a number of bytes, such as `3.14MB`

func IsDirectory

func IsDirectory(path string) bool

IsDirectory determines whether the specified path represents a directory.

func IsExecutable added in v1.0.9

func IsExecutable(filepath string) bool

IsExecutable determines whether the file/directory is executable for the active system user.

func IsFile

func IsFile(path string) bool

IsFile determines whether the specified path represents a file.

func IsReadable

func IsReadable(path string) bool

IsReadable determines whether the file/directory is readable for the active system user.

func IsSymlink(path string) bool

IsSymlink determines whether the path is a symbolic link.

func IsWritable

func IsWritable(path string) bool

IsWritable determines whether the file/directory is writable for the active system user.

func LastModified added in v1.0.8

func LastModified(path string) (time.Time, error)

LastModified identies the last time the path was modified.

func List

func List(directory string, recursive bool, ignore ...string) ([]string, error)

Generate a list of path names for the given directory. Optionally provide a list of ignored paths, using [glob](https://en.wikipedia.org/wiki/Glob_%28programming%29) syntax.

func ListDirectories

func ListDirectories(directory string, recursive bool, ignore ...string) ([]string, error)

ListDirectories provides absolute paths of directories only, ignoring files.

func ListFiles

func ListFiles(directory string, recursive bool, ignore ...string) ([]string, error)

ListFiles provides absolute paths of files only, ignoring directories.

func Mkdirp

func Mkdirp(path string) string

Mkdirp is the equivalent of [mkdir -p](https://en.wikipedia.org/wiki/Mkdir) It will generate the full directory path if it does not already exist.

func Move added in v1.0.9

func Move(source string, dest string, ignoreErrors ...bool) error

Move a file/directory to another location

func ReadTextFile

func ReadTextFile(path string) (string, error)

ReadTextFile reads a text file and converts results from bytes to a string.

func Size added in v1.0.8

func Size(path string, sigfig ...int) (string, error)

Size returns a "pretty" version of the size, such as "3.12MB"

func Symlink(target string, name string) error

Symlink creates a symbolic link. This just runs `os.Symlink()`.

func Touch

func Touch(path string, flags ...interface{}) string

Similar to the touch command on *nix, where the file or directory will be created if it does not already exist. Returns the absolute path. The optional second boolean argument will force the method to treat the path as a file instead of a directory (useful when the filename has not extension). An optional 3rd boolean argument will force the method to treat the path as a directory even if a file extension is present.

For example: `fsutil.Touch("./path/to/archive.old", false, true)`

Normally, any file path with an extension is determined to be a file. However; the second argument (`false`) instructs the command to **not** force a file. The third argument (`true`) instructs the command to **treat the path like a directory**.

func Unzip added in v1.0.9

func Unzip(src string, dest string) error

Unzip a file

func WriteTextFile

func WriteTextFile(path string, content string, args ...interface{}) error

WriteTextFile writes text to a file (automatically converts string to a byte array). If the path does not exist, it will be created automatically. This is the equivalent of using the Touch() method first, then writing text content to the file.

It is also possible to pass a third argument, a custom permission. By default, os.ModePerm is used.

func Zip added in v1.0.9

func Zip(src string, target ...string) error

Zip a file or directory. Does not follow symlinks.

Types

This section is empty.

Jump to

Keyboard shortcuts

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