Initial commit
This commit is contained in:
+318
@@ -0,0 +1,318 @@
|
||||
package gofile
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"io/fs"
|
||||
"time"
|
||||
)
|
||||
|
||||
type FileStats struct {
|
||||
Name string
|
||||
Size int64
|
||||
Mode fs.FileMode
|
||||
ModTime time.Time
|
||||
IsDir bool
|
||||
}
|
||||
|
||||
/*
|
||||
* * Delete a file
|
||||
*/
|
||||
func DeleteFile(file string) (int64,error) {
|
||||
if file == "" {
|
||||
panic("DeleteFile(): file parameter empty. Aborting.")
|
||||
}
|
||||
|
||||
err := os.Remove(file)
|
||||
if err != nil {
|
||||
return 1, fmt.Errorf("DeleteFile(): %v", err)
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* * Delete a directory AND subdirectories
|
||||
*/
|
||||
func DeleteDir(dir string) (int64,error) {
|
||||
if dir == "" {
|
||||
panic("DeleteDir(): file parameter empty. Aborting.")
|
||||
}
|
||||
|
||||
err := os.RemoveAll(dir)
|
||||
if err != nil {
|
||||
return 1, fmt.Errorf("DeleteFile(): %v", err)
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* * Move a file
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* * Create an empty file
|
||||
*/
|
||||
func CreateFile(file string, mode os.FileMode) (int64,error) {
|
||||
if file == "" {
|
||||
panic("Write(): file parameter empty. Aborting.")
|
||||
}
|
||||
|
||||
f, err := os.Create(file)
|
||||
defer f.Close()
|
||||
if err != nil {
|
||||
return 1, fmt.Errorf("CreateFile(): %v", err)
|
||||
}
|
||||
|
||||
err = os.Chmod(file, mode)
|
||||
if err != nil {
|
||||
return 1, fmt.Errorf("CreateFile(): %v", err)
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* * Create a directory but do not and silently ignore if the directory already exists
|
||||
*/
|
||||
func CreateDir(dir string, mode os.FileMode) (int64,error) {
|
||||
if dir == "" {
|
||||
panic("CreateDir(): dir parameter empty. Aborting.")
|
||||
}
|
||||
|
||||
//blacklist = []string{
|
||||
// "/", "/bin", "/boot", "/dev", "/etc", "/home", "/lib", "/lib64", "/media", "/mnt", "/opt", "/root", "/run", "/sbin", "/srv", "/sys", "/tmp", "/usr", "/var"
|
||||
//}
|
||||
|
||||
_, err := os.Stat(dir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
fmt.Printf("CreateDir(): directory %s does not exist\n", dir)
|
||||
//return 1, fmt.Errorf("CreateDir(): %v", err)
|
||||
} else {
|
||||
return 1, fmt.Errorf("CreateDir(): %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
err = os.Mkdir(dir, mode)
|
||||
if err != nil {
|
||||
return 1, fmt.Errorf("CreateDir(): %v", err)
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
|
||||
/*if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||
fmt.Println("Directory ", dir, "already exists")
|
||||
err := os.Mkdir(dir, mode)
|
||||
check(err)
|
||||
} else {
|
||||
fmt.Println(dir, "does exist!")
|
||||
}*/
|
||||
}
|
||||
|
||||
/*
|
||||
* * Create a directory but do not and silently ignore if the directory already exists
|
||||
*/
|
||||
func CreateDirAll(dir string, mode os.FileMode) {
|
||||
if dir == "" {
|
||||
panic("CreateDir(): dir parameter empty. Aborting.")
|
||||
}
|
||||
|
||||
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||
err := os.MkdirAll(dir, mode)
|
||||
check(err)
|
||||
} else {
|
||||
fmt.Println(dir, "already exist!")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* * Read data from a file and return it as []byte
|
||||
*/
|
||||
func Read(file string) []byte {
|
||||
if file == "" {
|
||||
panic("Write(): file parameter empty. Aborting.")
|
||||
}
|
||||
|
||||
text, err := os.ReadFile(file)
|
||||
check(err)
|
||||
|
||||
//fmt.Println(string(text))
|
||||
|
||||
return text
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* * Create a new file or overwrite an existing file
|
||||
*/
|
||||
func Write(file string, text string, mode os.FileMode) (int64,error) {
|
||||
if file == "" {
|
||||
panic("Write(): file parameter empty. Aborting.")
|
||||
}
|
||||
|
||||
if len(text) <= 0 {
|
||||
panic("Write(): text parameter empty. Aborting.")
|
||||
}
|
||||
|
||||
text_with_newline := fmt.Sprintf("%s\n", text)
|
||||
text_byte := []byte(text_with_newline)
|
||||
|
||||
err := os.WriteFile(file, text_byte, mode)
|
||||
if err != nil {
|
||||
return 1, fmt.Errorf("Write(): %v", err)
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* * Append data to a file and return err
|
||||
*/
|
||||
func Append(file string, text string, mode os.FileMode) (int64,error) {
|
||||
if file == "" {
|
||||
panic("Write(): file parameter empty. Aborting.")
|
||||
}
|
||||
|
||||
if len(text) <= 0 {
|
||||
panic("Write(): text parameter empty. Aborting.")
|
||||
}
|
||||
|
||||
//f, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, mode)
|
||||
f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, mode)
|
||||
defer f.Close()
|
||||
if err != nil {
|
||||
return 1, fmt.Errorf("Append(): %v", err)
|
||||
}
|
||||
|
||||
_, err = f.WriteString(text + "\n")
|
||||
if err != nil {
|
||||
return 1, fmt.Errorf("Append(): %v", err)
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* * Get information about a file and return as a struct
|
||||
*/
|
||||
func Stats(dir string) FileStats {
|
||||
if dir == "" {
|
||||
panic("Stats(): dir parameter empty. Aborting.")
|
||||
}
|
||||
|
||||
fi, err := os.Stat(dir)
|
||||
check(err)
|
||||
|
||||
return FileStats {
|
||||
Name: fi.Name(),
|
||||
Size: fi.Size(),
|
||||
Mode: fi.Mode(),
|
||||
ModTime: fi.ModTime(),
|
||||
IsDir: fi.IsDir(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* * Read files in a directory and return a list
|
||||
*/
|
||||
func ReadDir(dir string) []string {
|
||||
parsed_files := []string{}
|
||||
|
||||
if dir == "" {
|
||||
panic("ReadDir(): dir parameter empty. Aborting.")
|
||||
}
|
||||
|
||||
f, err := os.Open(dir)
|
||||
check(err)
|
||||
|
||||
files, err := f.Readdir(0)
|
||||
check(err)
|
||||
|
||||
for _, v := range files {
|
||||
parsed_files = append(parsed_files, v.Name())
|
||||
}
|
||||
|
||||
//fmt.Println(parsed_files)
|
||||
|
||||
return parsed_files
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* * Receives error as a parameter and panics if error is not null
|
||||
*/
|
||||
func check(e error) {
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
}
|
||||
|
||||
//var db *sql.DB
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//var src_dir = "/etc"
|
||||
|
||||
//func mysort(names []string) []string {
|
||||
//names := []string{"paul","john","michaels"
|
||||
// sorted_names := []string{}
|
||||
|
||||
// if len(names) > 1 {
|
||||
// for index, name := range names {
|
||||
// fmt.Printf("Index: %v, Value: %v\n",index,name)
|
||||
// sorted_names = append(sorted_names, names[index])
|
||||
// }
|
||||
// } else {
|
||||
// fmt.Println("names is empty!")
|
||||
// }
|
||||
|
||||
//fmt.Println(10)
|
||||
// return sorted_names
|
||||
//}
|
||||
|
||||
|
||||
//func main() {
|
||||
|
||||
//dbcfg := mysql.Config{
|
||||
// User: "root",
|
||||
// Passwd: "",
|
||||
// Net: "tcp",
|
||||
// Addr: "localhost:3306",
|
||||
// DBName: "mysql"
|
||||
//}
|
||||
|
||||
//greeting := "hello there friends!"
|
||||
|
||||
// fmt.Println(strings.Contains(greeting, "hello!"))
|
||||
// fmt.Println(strings.ReplaceAll(greeting, "hello!", "hi"))
|
||||
// fmt.Println(strings.ToUpper(greeting))
|
||||
// fmt.Println(strings.Index(greeting, "th"))
|
||||
// fmt.Println(strings.Splt(greeting, " "))
|
||||
//ages := []int{1,2,3,i4,5,6,7,8,9};
|
||||
//names := []string{"paul","john","michaels"}
|
||||
// sort.Ints(ages)
|
||||
// sort.Strings(names)
|
||||
//fmt.Println(sort.SearchStrings(names,"paul"))
|
||||
//https://www.youtube.com/redirect?event=video_description&redir_token=QUFFLUhqazNFaDR4R1RPQVZoUVpyZUlwcThaRmc4OXZFZ3xBQ3Jtc0tscFJDQW5pWjZGa0QzZHZnRy1fbWpCYXF4UUNFOEoyNjJlcGRQaWl2WVhpRGktbG9tdnFlVUVqVkczSS0zSnVXZFBzTGktUHJaaUx6eUVxWmpORkVfRldTY1hxTVQ4aXdLa3lVWnVJZkNOSlpXYnZNMA&q=https%3A%2F%2Fgolang.org%2Fpkg%2F&v=BoooHY57RXw
|
||||
// fmt.Println(mysort(names))
|
||||
//}
|
||||
|
||||
Reference in New Issue
Block a user