ctdo.de/file.go

51 lines
1008 B
Go
Raw Normal View History

package main
import (
2023-01-28 20:49:07 +00:00
"errors"
"io/ioutil"
"os"
)
func fileRead(src string) string {
content, err := ioutil.ReadFile(src)
errorPanic(err)
return string(content)
}
func fileAddLine(input string, filepath string) {
2023-01-28 20:49:07 +00:00
_, err := os.Stat(filepath)
if errors.Is(err, os.ErrNotExist) {
2023-01-28 20:53:09 +00:00
fileCreate(filepath)
2023-01-28 20:49:07 +00:00
}
var file *os.File
file, err = os.OpenFile(filepath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
errorPanic(err)
_, err = file.WriteString(input + "\n")
errorPanic(err)
}
2023-01-28 20:53:09 +00:00
func fileCreate(filepath string) {
2023-01-29 15:13:57 +00:00
if _, err := os.Stat(filepath); err == nil {
2023-01-29 15:09:39 +00:00
_, err := os.Create(filepath)
errorPanic(err)
2023-01-28 20:53:09 +00:00
2023-01-29 15:09:39 +00:00
logger("fileCreate : file created -> " + filepath)
2023-01-29 15:13:57 +00:00
} else if errors.Is(err, os.ErrNotExist) {
2023-01-29 15:09:39 +00:00
logger("fileCreate : file already exists -> " + filepath)
2023-01-29 15:13:57 +00:00
} else {
logger("fileCreate : unknown -> " + filepath)
2023-01-29 15:09:39 +00:00
}
}
2023-01-29 15:23:03 +00:00
func fileMkDir(folderpath string) {
_, err := os.Stat(folderpath)
if errors.Is(err, os.ErrNotExist) {
err = os.Mkdir(folderpath, 0755)
errorPanic(err)
}
}