Categorygithub.com/coreybutler/go-fsutil
modulepackage
1.2.2
Repository: https://github.com/coreybutler/go-fsutil.git
Documentation: pkg.go.dev

# 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.

# Functions

Returns the fully resolved path, even if the path does not exist.
ByteSize returns the number of bytes (size) of a file/directory.
Clean will ensure the specified directory exists.
Copy a file/directory.
Exists is a helper method to quickly determine whether a directory or file exists.
FormatSize returns a nicely formatted representation of a number of bytes, such as `3.14MB`.
IsDirectory determines whether the specified path represents a directory.
IsExecutable determines whether the file/directory is executable for the active system user.
IsFile determines whether the specified path represents a file.
IsReadable determines whether the file/directory is readable for the active system user.
IsSymlink determines whether the path is a symbolic link.
IsWritable determines whether the file/directory is writable for the active system user.
LastModified identies the last time the path was modified.
Generate a list of path names for the given directory.
ListDirectories provides absolute paths of directories only, ignoring files.
ListFiles provides absolute paths of files only, ignoring directories.
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.
Move a file/directory to another location.
ReadTextFile reads a text file and converts results from bytes to a string.
Size returns a "pretty" version of the size, such as "3.12MB".
Symlink creates a symbolic link.
Similar to the touch command on *nix, where the file or directory will be created if it does not already exist.
Unzip a file.
WriteTextFile writes text to a file (automatically converts string to a byte array).
Zip a file or directory.

# Constants

GB represents the size of a giggabyte.
KB represents the size of a kilobyte.
MB represents the size of a megabyte.
PB represents the size of a petabyte.
TB represents the size of a terabyte.