2021-11-17 09:59:06 +00:00
|
|
|
package script
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func FileIfExist(filePath string) bool {
|
|
|
|
|
_, err := os.Stat(filePath)
|
|
|
|
|
if nil != err {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-18 08:27:28 +00:00
|
|
|
func ReadFile(filePath string) ([]byte, error) {
|
2021-11-17 09:59:06 +00:00
|
|
|
content, err := ioutil.ReadFile(filePath)
|
|
|
|
|
if nil != err {
|
2021-11-18 08:27:28 +00:00
|
|
|
LogError("Failed to read file (" + filePath + ")")
|
|
|
|
|
return content, err
|
2021-11-17 09:59:06 +00:00
|
|
|
}
|
2021-11-18 08:27:28 +00:00
|
|
|
LogInfo("Success read file (" + filePath + ")")
|
|
|
|
|
return content, nil
|
2021-11-17 09:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
2021-11-18 08:27:28 +00:00
|
|
|
func WriteFile(filePath string, content string) error {
|
|
|
|
|
err := ioutil.WriteFile(filePath, []byte(content), os.ModePerm)
|
|
|
|
|
if err != nil {
|
|
|
|
|
LogError("Failed to write file (" + filePath + ")")
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
LogInfo("Success write file (" + filePath + ")")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func CreateFile(filePath string) error {
|
2021-11-17 09:59:06 +00:00
|
|
|
f, err := os.Create(filePath)
|
|
|
|
|
if nil != err {
|
|
|
|
|
LogError(filePath + " create failed")
|
2021-11-18 08:27:28 +00:00
|
|
|
return err
|
2021-11-17 09:59:06 +00:00
|
|
|
}
|
|
|
|
|
defer f.Close()
|
2021-11-18 08:27:28 +00:00
|
|
|
LogInfo("Success create file " + filePath)
|
|
|
|
|
return nil
|
2021-11-17 09:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
2021-11-18 08:27:28 +00:00
|
|
|
func MkDir(dirPath string) error {
|
2021-11-17 09:59:06 +00:00
|
|
|
err := os.MkdirAll(dirPath, os.ModePerm)
|
|
|
|
|
if nil != err {
|
2021-11-18 08:27:28 +00:00
|
|
|
LogError("Failed mkdir " + dirPath)
|
|
|
|
|
return err
|
2021-11-17 09:59:06 +00:00
|
|
|
}
|
2021-11-18 08:27:28 +00:00
|
|
|
LogInfo("Success mkdir " + dirPath)
|
|
|
|
|
return nil
|
2021-11-17 09:59:06 +00:00
|
|
|
}
|