-
Notifications
You must be signed in to change notification settings - Fork 31
/
utils.go
64 lines (53 loc) · 1.01 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package utils
import (
"fmt"
"github.com/wgpsec/lc/pkg/schema"
"gopkg.in/yaml.v3"
"os"
"strings"
)
type ErrNoSuchKey struct {
Name string
}
// 文本处理
func Contains(s []string, e string) bool {
for _, a := range s {
if strings.EqualFold(a, e) {
return true
}
}
return false
}
func (e *ErrNoSuchKey) Error() string {
return fmt.Sprintf("no such key: %s", e.Name)
}
func DivideList(list []string, n int) [][]string {
chunks := make([][]string, n)
chunkSize := len(list) / n
remaining := len(list) % n
for i := 0; i < n; i++ {
start := i * chunkSize
end := start + chunkSize
if i < remaining {
end++
}
if i == n-1 {
end = len(list)
}
chunks[i] = list[start:end]
}
return chunks
}
// 文件处理
func ReadConfig(configFile string) (schema.Options, error) {
var config schema.Options
file, err := os.Open(configFile)
if err != nil {
return nil, err
}
defer file.Close()
if err := yaml.NewDecoder(file).Decode(&config); err != nil {
return nil, err
}
return config, nil
}