fileboy/util.go

114 lines
2.0 KiB
Go
Raw Normal View History

2018-09-10 08:23:27 +00:00
package main
import (
"io/ioutil"
2019-06-04 03:12:41 +00:00
"log"
"os"
2018-09-19 03:26:57 +00:00
"strconv"
2018-09-10 08:23:27 +00:00
"strings"
)
2019-04-01 07:32:27 +00:00
func keyInMonitorTypesMap(k string, cfg *FileGirl) bool {
_, ok := cfg.Monitor.TypesMap[k]
return ok
}
2020-01-02 08:38:26 +00:00
func keyInInstruction(k string) bool {
2020-03-03 03:37:20 +00:00
_, ok := cfg.InstructionMap[k]
2020-01-02 08:38:26 +00:00
return ok
}
2019-01-03 01:56:26 +00:00
func cmdParse2Array(s string, cf *changedFile) []string {
2018-09-10 08:23:27 +00:00
a := strings.Split(s, " ")
r := make([]string, 0)
for i := 0; i < len(a); i++ {
if ss := strings.Trim(a[i], " "); ss != "" {
2018-09-19 03:26:57 +00:00
r = append(r, strParseRealStr(ss, cf))
2018-09-10 08:23:27 +00:00
}
}
return r
}
2019-01-03 01:56:26 +00:00
func strParseRealStr(s string, cf *changedFile) string {
2019-12-04 03:50:11 +00:00
return strings.ReplaceAll(
strings.ReplaceAll(
strings.ReplaceAll(
strings.ReplaceAll(s, "{{file}}", cf.Name),
"{{ext}}", cf.Ext,
),
"{{changed}}", strconv.FormatInt(cf.Changed, 10),
2018-09-19 03:26:57 +00:00
),
2019-12-04 03:50:11 +00:00
"{{event}}", cf.Event,
2018-09-19 03:26:57 +00:00
)
}
2018-09-10 08:23:27 +00:00
func dirParse2Array(s string) []string {
a := strings.Split(s, ",")
r := make([]string, 0)
for i := 0; i < len(a); i++ {
if ss := strings.Trim(a[i], " "); ss != "" {
r = append(r, ss)
}
}
return r
}
func listFile(folder string, fun func(string)) {
files, _ := ioutil.ReadDir(folder)
for _, file := range files {
if file.IsDir() {
d := folder + "/" + file.Name()
fun(d)
listFile(d, fun)
}
}
}
2018-09-19 03:26:57 +00:00
func relativePath(folder, p string) string {
2019-04-04 02:46:52 +00:00
s := strings.ReplaceAll(strings.TrimPrefix(p, folder), "\\", "/")
if strings.HasPrefix(s, "/") && len(s) > 1 {
s = s[1:]
}
return s
2018-09-19 03:26:57 +00:00
}
2019-06-04 03:12:41 +00:00
2019-12-04 03:50:11 +00:00
func inStrArray(s string, arr []string) bool {
for _, v := range arr {
if s == v {
return true
}
}
return false
}
2020-01-02 09:25:02 +00:00
func logInfo(v ...interface{}) {
2020-01-02 09:38:52 +00:00
if keyInInstruction(InstIgnoreInfo) {
return
}
2020-01-02 10:22:16 +00:00
logUInfo(v...)
}
func logUInfo(v ...interface{}) {
2020-01-02 09:25:02 +00:00
v = append([]interface{}{"I:"}, v...)
log.Println(v...)
}
func logWarn(v ...interface{}) {
2020-01-02 09:38:52 +00:00
if keyInInstruction(InstIgnoreWarn) {
return
}
2020-01-02 09:25:02 +00:00
v = append([]interface{}{"W:"}, v...)
log.Println(v...)
}
func logError(v ...interface{}) {
v = append([]interface{}{"E:"}, v...)
log.Println(v...)
}
2019-06-04 03:12:41 +00:00
func logAndExit(v ...interface{}) {
2020-01-02 09:25:02 +00:00
v = append([]interface{}{"O:"}, v...)
2019-06-04 03:19:24 +00:00
log.Println(v...)
2019-12-17 08:10:33 +00:00
os.Exit(15)
2019-06-04 03:12:41 +00:00
}