fileboy/util.go

88 lines
1.5 KiB
Go
Raw Normal View History

2018-09-10 08:23:27 +00:00
package main
import (
"io/ioutil"
2018-09-19 03:26:57 +00:00
"strconv"
2018-09-10 08:23:27 +00:00
"strings"
)
func inStringArray(value string, arr []string) bool {
for _, v := range arr {
if value == v {
return true
}
}
return false
}
2018-09-19 03:26:57 +00:00
func cmdParse2Array(s string, cf *changeFile) []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
}
2018-09-19 03:26:57 +00:00
func strParseRealStr(s string, cf *changeFile) string {
return strings.Replace(
strings.Replace(
strings.Replace(s, "{{file}}", cf.Name, -1),
"{{ext}}", cf.Ext, -1,
),
"{{changed}}", strconv.FormatInt(cf.Changed, 10), -1,
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)
}
}
}
func arrayUniqueAdd(a []string, add string) []string {
if inStringArray(add, a) {
return a
}
return append(a, add)
}
func arrayRemoveElement(a []string, r string) []string {
2018-09-11 02:12:52 +00:00
i := -1
2018-09-10 08:23:27 +00:00
for k, v := range a {
if v == r {
i = k
break
}
}
2018-09-11 02:12:52 +00:00
if i == -1 {
return a
}
if len(a) == 1 && i == 0 {
return []string{}
}
2018-09-10 08:23:27 +00:00
return append(a[:i], a[i+1:]...)
}
2018-09-19 03:26:57 +00:00
func relativePath(folder, p string) string {
return strings.TrimPrefix(p, folder)
}