Fileman is a go package that provides handy functions for files, directories, and symbolic links.
- Copy
- Paste
- Delete
- Cut
- Move
- Rename
- Search
- Duplicate
- Get Type
go get -u github.com/Hermitter/fileman
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"