A Go filesystem package for working with files and directories

Stowage

A Go filesystem package for working with files and directories, it features a simple API with support for the common files and directories operations such as copy, move, list files, file exists check, and more.

Build Status Test Status GoDoc reportcard Coverage Status

Install

To install stowage run the following command:

go get github.com/harranali/stowage

Getting Started

// get the absolute path to the root directory
rootFolder, _ := filepath.Abs("./my-base-folder")

// first create the package variable 
s := stowage.New()

// Initiate the storage with root directory
s.InitLocalStorage(stowage.LocalStorageOpts{
    RootFolder: rootFolder,
})

// get file information
info, _ := s.LocalStorage.FileInfo("testfile.txt")
fmt.Println(info.Name) // the file name with extension
fmt.Println(info.NameWithoutExtension) // the file name without extension
fmt.Println(info.Extension) // the file extension

// copy file 
err := s.LocalStorage.Copy("myfile.txt", "files/backup/txt")

Root folder

The rootFolder acts as the reference for all operations, it has to be a full absolute path here is how you can get the absolute path for your root folder:

// get the absolute path to the root directory
rootFolder, _ := filepath.Abs("./my-base-folder")

Package initiation

you need first to create the package variable by calling the method New s := stowage.New(), next you need to initiate the storage engine by calling the method s.InitLocalStorage(opts) and pass to it the options, the code below shows how you can create the variable and initiate the storage engine

// first create the package variable 
s := stowage.New()

rootFolder, _ := filepath.Abs("./my-base-folder")
// Initiate the storage enginey
s.InitLocalStorage(stowage.LocalStorageOpts{
    RootFolder: rootFolder,
})

Getting File information

Here is how you can get information about a file such as name, extension, size, and more.

rootFolder, _ := filepath.Abs("./my-base-folder")

s := stowage.New()
s.InitLocalStorage(stowage.LocalStorageOpts{
    RootFolder: rootFolder,
})

info, _ := s.LocalStorage.FileInfo("testfile.txt")
fmt.Println(info.Name) // the file name with extension
fmt.Println(info.NameWithoutExtension) // the file name without extension
fmt.Println(info.Extension) // the file extension
fmt.Println(info.Size) // the file size 
fmt.Println(info.Path) // the file full path

Example operations

All file operations are performed with respect to the root directory

rootFolder, _ := filepath.Abs("./my-base-folder")

s := stowage.New()
s.InitLocalStorage(stowage.LocalStorageOpts{
    RootFolder: rootFolder,
})

// copy file to the root directory from external directory
s.LocalStorage.Put(filePath string) error

// copy file to the root directory from external directory with the given name
s.LocalStorage.PutAs(filePath string, filename string) error

// copy file around within the root directory
s.LocalStorage.Copy(filePath string, destfolder string) error

// copy file around within the root directory with given name as third param
s.LocalStorage.CopyAs(filePath string, destfolder string, newFilePath string) error

// move file around within the root directory
s.LocalStorage.Move(filePath string, destfolder string) error

// rename a file 
s.LocalStorage.Rename(filePath string, newFilePath string) error

List of supported operations

Here is a list of all supported operations

FileInfo(filePath string) (fileinfo localstorage.FileInfo, err error)
Put(filePath string) error
PutAs(filePath string, filename string) error
Copy(filePath string, destfolder string) error
CopyAs(filePath string, destfolder string, newFilePath string) error
Move(filePath string, destfolder string) error
MoveAs(filePath string, destFolder string, newFilePath string) error
Rename(filePath string, newFilePath string) error
Delete(filePath string) error
DeleteMultiple(filePaths []string) error
Create(filePath string, content []byte) error
Append(filePath string, content []byte) error
Exists(filePath string) (bool, error)
Missing(filePath string) (bool, error)
Read(filePath string) ([]byte, error)
Files(DirectoryPath string) ([]localstorage.FileInfo, error)
AllFiles(DirectoryPath string) ([]localstorage.FileInfo, error)
Directories(DirectoryPath string) (directoryPaths []string, err error)
AllDirectories(DirectoryPath string) (directoryPaths []string, err error)
MakeDirectory(DirectoryPath string, perm int) error
RenameDirectory(DirectoryPath string, NewDirectoryPath string) (err error)
DeleteDirectory(DirectoryPath string) (err error)

docs

Here are the details of each operation

FileInfo(filePath string) (fileinfo localstorage.FileInfo, err error)

FileInfo returns information about the given file or an error incase there is any

info, _ := s.LocalStorage.FileInfo("testfile.txt")
info.Name // the file name with extension
info.NameWithoutExtension // the file name without extension
info.Extension // the file extension
info.Size // the file size 
info.Path // the file full path

Put(filePath string) error

Put helps you copy files into the root directory from external locations, filePath is the full path to the file you would like to put, it returns error incase there is any

err := s.LocalStorage.Put("testfile.txt")

PutAs(filePath string, filename string) error

PutAs helps you copy files into the root directory from external directory, the first param 'filePath' is the full path to the file you would like to put, the second param 'fileName' is the name you would like to give to the file, it returns error incase there is any

err := s.LocalStorage.PutAs("testfile.txt", "newtestfile.txt")

Copy(filePath string, destPath string) error

Copy helps you copy files within the root folder, Please note that the reference of the paths of these files is the root folder, it accepts the source file starting from the root folder, and the destination folder starting from the root folder, it returns an error incase there is any

err := s.LocalStorage.Copy("testfile.txt", "/folder/subfolder")

CopyAs(filePath string, destfolder string, newFilePath string) error

CopyAs helps you copy files within the root folder, Please note that the reference of the paths of these files is the root folder, it accepts the source file starting from the root folder and the destination folder starting from the root folder, and the new file name, it returns an error incase there is any

err := s.LocalStorage.CopyAs("testfile.txt", "/folder/subfolder", "newtestfile.txt")

Move(filePath string, destFolder string) error

Move helps you Move files within the root folder, Please note that the reference of the paths of these files is the root folder, it accepts the source file starting from the root folder, and the destination folder starting from the root folder, it returns an error incase there any

err := s.LocalStorage.Move("testfile.txt", "/folder/subfolder")

MoveAs(filePath string, destFolder string, newFilePath string) error

MoveAs helps you Move files within the root folder, Please note that the reference of the paths of these files is the root folder, it accepts the source file starting from the root folder and the destination folder starting from the root folder, and the new file name, it returns an error incase there any

err := s.LocalStorage.Move("testfile.txt", "/folder/subfolder", "newtestfile.txt")

Rename(filePath string, newFilePath string) error

Rename renames the given file as first parameter to the name given as a second parameter, it returns error incase there is any

err := s.LocalStorage.Rename("testfile.txt",  "newtestfile.txt")

Delete(filePath string) error

Delete deletes the given file it returns error incase there is any

err := s.LocalStorage.Delete("testfile.txt")

DeleteMultiple(filePaths []string) (err error)

DeleteMultiple deltes multiple files given as slice of strings of file paths, it returns error incase there is any

files := []string{"testfile1.txt", "testfile1.txt"}
err := s.LocalStorage.DeleteMultiple(files)

Create(filePath string, content []byte) error

Create helps you create new a file and add content to it, it returns error incase there is any

err := s.LocalStorage.Create("newfile.txt", []byte("this is a sample text"))

Append(filePath string, content []byte) error

Append helps you append content to a file, it returns error incase there is any

err := s.LocalStorage.Append("newfile.txt",[]byte("this is a sample text"))

Exists(filePath string) (bool, error)

Exists checks if a file exists withn the root folder, it returns a bool and an error incase any

exists, err := s.LocalStorage.Exists("newfile.txt")

Missing(filePath string) (bool, error)

Missing checks if a file is missing in the root folder, it returns a bool and an error incase any

missing, err := s.LocalStorage.Missing("newfile.txt")

Read(filePath string) ([]byte, error)

Read helps you grap the content of a file, it returns the data in a slice of bytes and an error incase there is any

content, err := s.LocalStorage.Read("newfile.txt")

Files(DirectoryPath string) (files []FileInfo, err error)

Files returns a list of files in a given directory, the file type is LocalStorage.FileInfo NOT the standard library fs.FileInfo, and it returns an error incase any occurred, if you want a list of files including the files in sub directories, consider using the method AllFiles(DirectoryPath string)

files, err := s.LocalStorage.Files("mydir")

AllFiles(DirectoryPath string) (files []FileInfo, err error)

AllFiles returns a list of files in the given directory including files in sub directories, the file type in the list is LocalStorage.FileInfo NOT the standard library fs.FileInfo

files, err := s.LocalStorage.Files("mydir")

Directories(DirectoryPath string) (directoryPaths []string, err error)

Directories returns a slice of string containing the paths of the directories, if you want the list of directories including subdirectories, consider using the method "AllDirectories(DirectoryPath string)", it returns an error incase is any

directories, err := s.LocalStorage.Directories("mydir")

AllDirectories(DirectoryPath string) (SubDirectoryPath []string, err error)

AllDirectories returns a list of directories including sub directories, it returns an error incase is any

directories, err := s.LocalStorage.AllDirectories("mydir")

MakeDirectory(DirectoryPath string, perm int) (err error)

MakeDirectory creates a new directory and the necessary parent directories with the given permissions, permissions could be (example: 0777) or any linux based permissions, it returns an error incase is any

err := s.LocalStorage.MakeDirectory("mydir")

RenameDirectory(DirectoryPath string, NewDirectoryPath string) (err error)

RenameDirectory changes the name of directory to new name, it returns an error incase there is any

err := s.LocalStorage.RenameDirectory("mydir",  "new-dir-name")

DeleteDirectory(DirectoryPath string) (err error)

DeleteDirectory deletes the given directory

err := s.LocalStorage.DeleteDirectory("mydir")
Owner
A Full Stack Developer who likes to solve people's problems with code
null
Similar Resources

go tool for working with /etc/hosts files

hostsfile This library, and the associated command line binary, will help you manipulate your /etc/hosts file. Both the library and the binary will le

Dec 30, 2022

Takes an input http.FileSystem (likely at go generate time) and generates Go code that statically implements it.

vfsgen Package vfsgen takes an http.FileSystem (likely at go generate time) and generates Go code that statically implements the provided http.FileSys

Dec 18, 2022

A set of io/fs filesystem abstractions and utilities for Go

A set of io/fs filesystem abstractions and utilities for Go

A set of io/fs filesystem abstractions and utilities for Go Please ⭐ this project Overview This package provides io/fs interfaces for: Cloud providers

Nov 19, 2022

s3fs provides a S3 implementation for Go1.16 filesystem interface.

S3 FileSystem (fs.FS) implementation.Since S3 is a flat structure, s3fs simulates directories by using prefixes and "/" delim. ModTime on directories is always zero value.

Nov 9, 2022

A FileSystem Abstraction System for Go

A FileSystem Abstraction System for Go

A FileSystem Abstraction System for Go Overview Afero is a filesystem framework providing a simple, uniform and universal API interacting with any fil

Jan 9, 2023

memfs: A simple in-memory io/fs.FS filesystem

memfs: A simple in-memory io/fs.FS filesystem memfs is an in-memory implementation of Go's io/fs.FS interface. The goal is to make it easy and quick t

Jan 8, 2023

A Small Virtual Filesystem in Go

This is a virtual filesystem I'm coding to teach myself Go in a fun way. I'm documenting it with a collection of Medium posts that you can find here.

Dec 11, 2022

CRFS: Container Registry Filesystem

CRFS: Container Registry Filesystem Discussion: https://github.com/golang/go/issues/30829 Overview CRFS is a read-only FUSE filesystem that lets you m

Dec 26, 2022

Encrypted overlay filesystem written in Go

Encrypted overlay filesystem written in Go

An encrypted overlay filesystem written in Go. Official website: https://nuetzlich.net/gocryptfs (markdown source). gocryptfs is built on top the exce

Jan 8, 2023
🌳 📂 The utility displays a tree of directories and files(symlinks in future).

dirTree The utility displays a tree of directories and files. usage: dirTree [-f] How it works with directory, where I wrote this project for example

Aug 12, 2021
This is a tool to extract TODOs, NOTEs etc or search user provided terms from given files and/or directories.

ado This is a tool to extract TODOs, NOTEs etc or user provided terms from given files and/or directories. DEPRECIATED: My project seek has cleaner co

Aug 11, 2022
A tool for moving files into directories by file extensions
A tool for moving files into directories by file extensions

The tool for moving files into directories by file extensions Example before moving structure: moving into same extension dir result: moving into diff

Dec 6, 2021
Grep archive search in any files on the filesystem, in archive and even inner archive.

grep-archive Grep archive search for string in any files on the filesystem, in archive and even inner archive. Supported archive format are : Tar Form

Jan 26, 2022
An implementation of the FileSystem interface for tar files.

TarFS A wrapper around tar.Reader. Implements the FileSystem interface for tar files. Adds an Open method, that enables reading of file according to i

Sep 26, 2022
A Go io/fs filesystem implementation for reading files in a Github gists.

GistFS GistFS is an io/fs implementation that enables to read files stored in a given Gist. Requirements This module depends on io/fs which is only av

Oct 14, 2022
Tarserv serves streaming tar files from filesystem snapshots.

tarserv A collection of tools that allow serving large datasets from local filesystem snapshots. It is meant for serving big amounts of data to shell

Jan 11, 2022
Yet another Go package for working with *.golden test files, with a focus on simplicity.

go-golden Yet another Go package for working with *.golden test files, with a focus on simplicity. Import import "github.com/jimeh/go-golden" Usage fu

Aug 3, 2022
A package to allow one to concurrently go through a filesystem with ease

skywalker Skywalker is a package to allow one to concurrently go through a filesystem with ease. Features Concurrency BlackList filtering WhiteList fi

Nov 14, 2022
Utility for working with files and folders stored on Google Drive

skicka Utility for working with files and folders stored on Google Drive. Note: skicka is not an official Google product! Intro skicka makes it easy t

Nov 15, 2021