Error Subcriber
This commit is contained in:
217
file/file.go
Normal file
217
file/file.go
Normal file
@@ -0,0 +1,217 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var Debug int = 0
|
||||
|
||||
//List function
|
||||
|
||||
// Thao tac file
|
||||
func Create_file(path string) {
|
||||
// detect if file exists
|
||||
var _, err = os.Stat(path)
|
||||
|
||||
// create file if not exists
|
||||
if os.IsNotExist(err) {
|
||||
var file, err = os.Create(path)
|
||||
if IsError(err) {
|
||||
//return
|
||||
}
|
||||
defer file.Close()
|
||||
}
|
||||
|
||||
//Println("==> done creating file", path)
|
||||
}
|
||||
func Write_file(msg string, path string) {
|
||||
// open file using READ & WRITE permission
|
||||
//Check_dir(path)
|
||||
// Println("==>begin writting ")
|
||||
//var file, err = os.OpenFile(path, os.O_RDWR|os.O_APPEND, 0777)
|
||||
var file, err = os.OpenFile(path, os.O_RDWR, 0777)
|
||||
if IsError(err) {
|
||||
//return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = file.WriteString(msg)
|
||||
if IsError(err) {
|
||||
//return
|
||||
}
|
||||
// save changes
|
||||
err = file.Sync()
|
||||
if IsError(err) {
|
||||
//return
|
||||
}
|
||||
//Println("==> done writing to file")
|
||||
}
|
||||
|
||||
func Write_log(msg string, path string) {
|
||||
Check_path(path)
|
||||
year, month, day := time.Now().Date()
|
||||
new_path := strconv.Itoa(year) + "-" + strconv.Itoa(int(month)) + "-" + strconv.Itoa(day) + ".txt"
|
||||
path = path + "/" + new_path
|
||||
Check_dir(path)
|
||||
var file, err = os.OpenFile(path, os.O_RDWR|os.O_APPEND, 0777)
|
||||
if IsError(err) {
|
||||
////return
|
||||
if Debug == 1 {
|
||||
fmt.Printf("%v", err)
|
||||
}
|
||||
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = file.WriteString(time.Now().String())
|
||||
if IsError(err) {
|
||||
////return
|
||||
}
|
||||
// write some text line-by-line to file
|
||||
_, err = file.WriteString("\n")
|
||||
if IsError(err) {
|
||||
////return
|
||||
}
|
||||
_, err = file.WriteString(msg)
|
||||
if IsError(err) {
|
||||
////return
|
||||
}
|
||||
_, err = file.WriteString("\n")
|
||||
if IsError(err) {
|
||||
////return
|
||||
}
|
||||
|
||||
// save changes
|
||||
err = file.Sync()
|
||||
if IsError(err) {
|
||||
////return
|
||||
}
|
||||
|
||||
//Println("==> done writing to file")
|
||||
}
|
||||
func Println(msg string) {
|
||||
if Debug == 1 {
|
||||
fmt.Println(msg)
|
||||
}
|
||||
}
|
||||
func DeleteFile(path string) {
|
||||
// delete file
|
||||
var err = os.Remove(path)
|
||||
if IsError(err) {
|
||||
//return
|
||||
}
|
||||
|
||||
Println("==> done deleting file")
|
||||
|
||||
}
|
||||
func DeleteForder(path string) {
|
||||
var cmd = "rm -rf " + path
|
||||
var log = "/admin/monitor/log/log_launcher"
|
||||
_, err := exec.Command("sh", "-c", cmd).Output()
|
||||
if err != nil {
|
||||
fmt.Println("Delete forder error,Path:" + path)
|
||||
Write_log("Delete forder file error,Path:"+path, log)
|
||||
} else {
|
||||
Println("Delete forder Done ")
|
||||
}
|
||||
}
|
||||
func CoppyFile(path string, id int) {
|
||||
var cmd = "mv " + path + " /admin/monitor/backup/Config_" + strconv.Itoa(id) + ".txt"
|
||||
var log = "/admin/monitor/log/log_launcher"
|
||||
_, err := exec.Command("sh", "-c", cmd).Output()
|
||||
if err != nil {
|
||||
fmt.Println("Move file error,Path:" + path)
|
||||
Write_log("Move file error,Path:"+path, log)
|
||||
} else {
|
||||
fmt.Println("Move file error,Path:" + path)
|
||||
Write_log("Move file error,Path:"+path, log)
|
||||
}
|
||||
}
|
||||
func IsError(err error) bool {
|
||||
if err != nil {
|
||||
if Debug == 1 {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return (err != nil)
|
||||
}
|
||||
func Check_path(path string) {
|
||||
// Check directory ?
|
||||
_, err := exec.Command("sh", "-c", "cd "+path).Output()
|
||||
if err != nil {
|
||||
|
||||
fmt.Println(path + " not Exits\n")
|
||||
|
||||
_, err1 := exec.Command("sh", "-c", "mkdir "+path).Output()
|
||||
if err1 != nil {
|
||||
|
||||
Println(" Cant create " + path)
|
||||
|
||||
} else {
|
||||
|
||||
Println(" Create forder " + path)
|
||||
|
||||
}
|
||||
} else {
|
||||
|
||||
Println(path + " ok \n")
|
||||
|
||||
}
|
||||
}
|
||||
func Check_dir(path string) {
|
||||
|
||||
Println("Function Check_dir ()")
|
||||
|
||||
//Println("------------>Check_dir <------------")
|
||||
var file, err = os.OpenFile(path, os.O_RDWR|os.O_APPEND, 0777)
|
||||
//Printf("Kieu file : %T\t", file)
|
||||
//Printf("Kieu file : %v\n", file)
|
||||
if IsError(err) {
|
||||
if Debug == 1 {
|
||||
fmt.Printf("%v", err)
|
||||
}
|
||||
|
||||
}
|
||||
if file == nil {
|
||||
Create_file(path)
|
||||
}
|
||||
defer file.Close()
|
||||
}
|
||||
|
||||
func ReadFile(path string) string {
|
||||
// re-open file
|
||||
var msg string = ""
|
||||
var file, err = os.OpenFile(path, os.O_RDWR, 0777)
|
||||
if err != nil {
|
||||
if Debug == 1 {
|
||||
Println(" Open file error")
|
||||
}
|
||||
|
||||
} else {
|
||||
// read file, line by line
|
||||
var text = make([]byte, 2048)
|
||||
for {
|
||||
_, err = file.Read(text)
|
||||
// break if finally arrived at end of file
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
|
||||
// break if error occured
|
||||
if err != nil && err != io.EOF {
|
||||
IsError(err)
|
||||
break
|
||||
}
|
||||
}
|
||||
msg = string(text)
|
||||
}
|
||||
defer file.Close()
|
||||
return msg
|
||||
}
|
||||
Reference in New Issue
Block a user