Reglue Go

Filesystem

tests linux macos windows

Table Of Contents

Introduction

The filesystem package provides an expressive abstraction for working with local files.

package main

import "github.com/reglue4go/filesystem"

func main() {
	if filesystem.Exists("./app.yml") {
		fmt.Printf("%v\n", "File found")
	} else {
		fmt.Printf("%v\n", "File missing")
	}
}

Available Methods

AllFiles Append Basename
Chmod CleanDirectory Copy
CopyDirectory CurrentDirectory CurrentExecutable
Delete DeleteDirectories DeleteDirectory
Directories Dirname EnsureDirectoryExists
Exists Extension Files
Get Glob GuessExtension
Hash HasSameHash IsDirectory
IsEmptyDirectory IsFile IsReadable
IsWritable LastModified Lines
Link MakeDirectory MimeType
Missing Move MoveDirectory
Name Prepend Put
Realpath RealpathSystem Replace
ReplaceInFile Size TempDirectory
Tempnam Type UserConfigDirectory
UserHomeDirectory    

Method Listing

UserHomeDirectory

The UserHomeDirectory method will return the path to operating system user home directory.

path := filesystem.UserHomeDirectory()

UserConfigDirectory

The UserConfigDirectory method will return the path to operating system user config directory.

path := filesystem.UserConfigDirectory()

Type

The Type method returns the file type of a given file. The returned value is either file or dir.

value := filesystem.Type("./temp.log")

Tempnam

The Tempnam method creates a file with a unique filename with access permission set to 0600 in the specified directory. The second parameter is a prefix to the temporal name.

path := filesystem.Tempnam("./public/temp", "log")

TempDirectory

The TempDirectory method will return the path to operating system temporal directory.

path := filesystem.TempDirectory()

Size

The Size method will gets the file size of a given file.

size := filesystem.Size("./temp.log")

ReplaceInFile

The ReplaceInFile method will replace a given string within a given file.

ok := filesystem.ReplaceInFile("long text", "article", "./temp.log")

Replace

The Replace method writes the contents of a file, replacing it atomically if it already exists.

ok := filesystem.Replace("./temp.log", "Some long text contents")

RealpathSystem

The RealpathSystem method expands all symbolic links and returns canonicalized absolute pathname based on the given path prifix. ~C~ prefix will expand from operating system user config directory. ~T~ prefix will expand from operating system temp directory. ~H~ prefix will expand from operating system user home directory.

log1 := filesystem.RealpathSystem("~C~/data/temp.log")

log2 := filesystem.RealpathSystem("~T~/data/temp.log")

log3 := filesystem.RealpathSystem("~H~/data/temp.log")

log4 := filesystem.RealpathSystem("./data/temp.log")

Realpath

The Realpath method expands all symbolic links and returns canonicalized absolute pathname.

path := filesystem.Realpath("./temp.log")

Put

The Put method may be used to write the contents of a file. If the put method is unable to write the file to disk, false will be returned.

success := filesystem.Put("./temp.log", "Some long text contents")

Prepend

The Prepend method allows you to write to the beginning of a file:

success := filesystem.Prepend("./temp.log", "Prepended Text")

Name

The Name method may be used to extract the file name from a file path.

name := filesystem.Name("./public/images/cat.jpg") // cat

MoveDirectory

The MoveDirectory method may be used to move an existing directory to a new location. You can overide any existing directory by providing true as a third argument.

success := filesystem.MoveDirectory("./from/temp", "./to/backup/temp")
success := filesystem.MoveDirectory("./from/temp", "./to/backup/temp", true)

Move

The Move method may be used to rename or move an existing file to a new location.

success := filesystem.Move("./from/file.jpg", "./to/file.jpg")

Missing

The Missing method may be used to determine if a file or directory is missing.

ok := filesystem.Missing("./cat.jpg")
ok := filesystem.Missing("./backup")

MimeType

The MimeType method will obtain the MIME type of a given file.

mime := filesystem.MimeType("./dog.png")

MakeDirectory

The MakeDirectory method will create the given directory, including any needed subdirectories.

success := filesystem.MakeDirectory("./backup/temp")

The Link method will create a symlink to the target file or directory.

success := filesystem.Link("./temp.log", "./backup/temp.log")

Lines

The Lines method gets the contents of a file one line at a time returning an array of lines

var list []string = filesystem.Lines("./temp.log")

LastModified

The LastModified method gets the file’s last modification time.

modifiedAt := filesystem.LastModified("./backup")

IsWritable

The IsWritable method determines if the given path is writable.

ok := filesystem.IsWritable("./backup")

IsReadable

The IsReadable method determines if the given path is readable.

ok := filesystem.IsReadable("./backup")

IsFile

The IsFile method determines if the given path is a file.

ok := filesystem.IsFile("./backup")

IsEmptyDirectory

The IsEmptyDirectory method determines if the given path is a directory that does not contain any other files or directories.

ok := filesystem.IsEmptyDirectory("./backup")

IsDirectory

The IsDirectory method determines if the given path is a directory.

ok := filesystem.IsDirectory("./temp.log")

HasSameHash

The HasSameHash method determines if two files are the same by comparing their hashes.

ok := filesystem.HasSameHash("./temp.log", "./backup/temp.log")

Hash

The Hash method generates a hash value using the contents of a given file.

value := filesystem.Hash("./temp.log")

GuessExtension

The GuessExtension method returns the file extension from the mime-type of a given file.

path := filesystem.GuessExtension("./public/images") // ""

path := filesystem.GuessExtension("./public/images/cat.jpg") // "jpg"

Glob

The Glob method returns a list of all path names matching a given pattern:

items := filesystem.Glob("*.jpg")

Get

The Get method may be used to retrieve the contents of a file.

contents := filesystem.Get("./temp.log")

Files

The Files method returns a list of all of the files in a given directory:

items := filesystem.Files("./public")

Extension

The Extension method returns the file extension of the given path.

path := filesystem.Extension("./public/images") // ""

path := filesystem.Extension("./public/images/cat.jpg") // "jpg"

Exists

The Exists method may be used to determine if a file exists on the disk.

success := filesystem.Exists("./temp")

EnsureDirectoryExists

The EnsureDirectoryExists method determines if a directory exists or creates the directory if it does not exists.

success := filesystem.EnsureDirectoryExists("./temp")

Dirname

The Dirname method returns the parent directory from a path.

path := filesystem.Dirname("./public/images")
// "./public"

path := filesystem.Dirname("./public/images/cat.jpg")
// "./public/images"

Directories

The Directories method returns a list of all the directories within a given directory.

items := filesystem.Directories("./public")

DeleteDirectory

The DeleteDirectory method may be used to remove a directory and all of its files.

success := filesystem.DeleteDirectory("./temp")

DeleteDirectories

The DeleteDirectories method may be used to remove all of the directories within a given directory.

success := filesystem.DeleteDirectories("./temp")

Delete

The Delete method may be used to remove the file at a given path.

success := filesystem.Delete("./public/temp.log")

CurrentExecutable

The CurrentExecutable method may be used to get the path of the executable that started the current process.

path := filesystem.CurrentExecutable()

CurrentDirectory

The CurrentDirectory method may be used to get path for the current working directory.

path := filesystem.CurrentDirectory()

CopyDirectory

The CopyDirectory method may be used to copy a directory from one location to another.

success := filesystem.CopyDirectory("./from/public", "./to/backup")

Copy

The Copy method may be used to copy an existing file to a new location on the disk.

success := filesystem.Copy("./from/file.jpg", "./to/file.jpg")

CleanDirectory

The CleanDirectory method will empty the specified directory of all files and folders.

success := filesystem.CleanDirectory("./temp")

Chmod

The Chmod method will get or set UNIX mode of a file or directory

success := filesystem.Chmod("./public/temp.log", 775)

Basename

The Basename method will return the trailing name component of the given path.

name := filesystem.Basename("./public/temp.log")
// temp.log

Append

The Append method allows you to write to the end of a file:

success := filesystem.Append("./temp.log", "Appended Text")

AllFiles

The AllFiles method retrieves a list of all files within a given directory including all subdirectories:

items := filesystem.AllFiles("./public")

Unit Tests Matrix

This package has been tested on the following platforms.

golang ubuntu macos windows
1.18.x
1.20.x
1.22.x
Go logo