fileman

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2019 License: MIT Imports: 6 Imported by: 0

README

Fileman

Build Status codecov GoDoc Go Report Card

Fileman is a go package that provides handy functions for files, directories, and symbolic links.

Current Functionality

  • Copy
  • Paste
  • Delete
  • Cut
  • Move
  • Rename
  • Search
  • Duplicate
  • Get Type

Installation

go get -u github.com/Hermitter/fileman

Usage

Copy & Paste
// Copy: returns a struct that allows you to edit/view the name & contents of an item
copiedDir, err := fileman.CopyDir("/home/john/Desktop/someDirectory")
if err != nil {
  return err
}

copiedFile, err := fileman.CopyFile("/home/john/Desktop/someFile.txt")
if err != nil {
  return err
}

copiedSymLink, err := fileman.CopySymLink("/home/john/Desktop/someSymLink.txt")
if err != nil {
  return err
}

// Paste: will not overwrite existing items
// the names of the pasted items are copiedDir.Name, copiedFile.Name, copiedSymLink.Name
if err := copiedDir.Paste("/home/john/documents", false); err != nil {
  return err
}

if err := copiedFile.Paste("/home/john/documents", false); err != nil {
  return err
}

if err := copiedSymLink.Paste("/home/john/documents"); err != nil {
  return err
}
Rename, Move & Delete
// Rename
err := fileman.Rename("./old.txt", "new.txt")
if err != nil {
  fmt.Println(err)
  return
}

// Move
err = fileman.Move("./new.txt", "/home/john/documents")
if err != nil {
fmt.Println(err)
  return
}

// Delete
err = fileman.Delete("/home/john/documents/new.txt")
if err != nil {
  fmt.Println(err)
  return
}
Search
found, path := fileman.Search("needle.txt", "/home/john/haystack", 1)
if !found {
  fmt.Println("Search Failed!")
  return
}

fmt.Println("Found it here: " + path)
Duplicate
// The last part of the destination path will be the new name of the pasted item.
err := fileman.Duplicate("/home/john/media", "/home/john/backups/media", false)

if err != nil {
  return err
}
Get Type
// Boolean value determines if "symLink" should be included as a result.
itemType, err := fileman.GetType("/home/john/media", false)

if err != nil {
  fmt.Println(err)
  return
}

fmt.Println(itemType)// Can print "dir" or "file"

Documentation

Overview

Package fileman contains file explorer-like functions for Directories, Files, and Symbolic Links.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(path string) error

Delete will remove a specified item.

func Duplicate

func Duplicate(path, newPath string, sync bool) error

Duplicate will clone a specified item and place it in the newPath given. newPath should include the new name of what's being duplicated. Sync parameter does not affect symlinks.

func GetType

func GetType(path string, includeSymLinks bool) (string, error)

GetType returns "dir" or "file" from the path given. Including symbolic Links will allow for "symLink" as a return value. If no file is found, an error will be returned

func Move

func Move(path, dirPath string) error

Move an item to a specified directory.

func Rename

func Rename(path, newName string) error

Rename a specified item.

func Search(itemName string, searchDir string, searchDepth int) (itemFound bool, path string)

Search will look inside searchDir for the item you want to find. SearchDepth determines how far the search will look inside each directory.

Types

type Dir

type Dir struct {
	Name     string
	Dirs     []Dir
	Files    []File
	SymLinks []SymLink
}

Dir is a structure representing a single Directory. 4 slices will represent any files, directories, & symbolic links inside.

func CopyDir

func CopyDir(path string) (Dir, error)

CopyDir returns a Directory struct from a specified path.

func CutDir

func CutDir(path string) (Dir, error)

CutDir will simultaneously Copy() & Delete() a specified directory

func (*Dir) Paste

func (d *Dir) Paste(path string, sync bool) error

Paste will paste a Directory inside a specified path. This will not overwrite a Directory with the same name.

type File

type File struct {
	Name     string
	Contents []byte
}

File is a structure representing a single file.

func CopyFile

func CopyFile(path string) (File, error)

CopyFile returns a File struct from a specified file path.

func CutFile

func CutFile(path string) (File, error)

CutFile will simultaneously Copy() & Delete() a specified file

func (*File) Paste

func (f *File) Paste(path string, sync bool) error

Paste will paste a file inside a specified path. This will not overwrite any existing paths.

func (*File) ToString

func (f *File) ToString() (string, error)

ToString returns the string value of a File's contents. Will return an error if string is not utf8 valid

type SymLink struct {
	Name string
	Link string
	Type string
}

SymLink is a structure representing a single symbolic link

func CopySymLink(path string) (SymLink, error)

CopySymLink returns a SymLink struct from a specified path.

func CutSymLink(path string) (SymLink, error)

CutSymLink will simultaneously Copy() & Delete() a specified symlink.

func (*SymLink) Paste

func (s *SymLink) Paste(path string) error

Paste will paste a symLink inside a specified path. This will overwrite any symLink with the same name.

Jump to

Keyboard shortcuts

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