-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathauditfix.go
90 lines (85 loc) · 1.94 KB
/
auditfix.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package action
import (
"context"
"strings"
"github.com/pkg/errors"
"github.com/samber/lo"
"projectforge.dev/projectforge/app/file"
"projectforge.dev/projectforge/app/util"
)
func auditRemove(ctx context.Context, fn string, pm *PrjAndMods, ret *Result) error {
if fn == "" {
paths := ret.Modules.Paths(false)
if len(paths) == 0 {
x := &Result{}
err := auditRun(pm, x)
if err != nil {
return err
}
paths = x.Modules.Paths(false)
}
for _, path := range paths {
err := auditRemove(ctx, path, pm, ret)
if err != nil {
return errors.Wrapf(err, "can't fix audit of [%s]", path)
}
}
return nil
}
tgt, err := pm.PSvc.GetFilesystem(pm.Prj)
if err != nil {
return err
}
ret.AddLog("removed [%s]", fn)
return tgt.Remove(fn, pm.Logger)
}
func auditHeader(ctx context.Context, fn string, pm *PrjAndMods, ret *Result) error {
if fn == "" {
x := &Result{}
err := auditRun(pm, x)
if err != nil {
return err
}
for _, path := range x.Modules.Paths(false) {
err = auditHeader(ctx, path, pm, ret)
if err != nil {
return errors.Wrapf(err, "can't fix audit of [%s]", path)
}
}
return nil
}
tgt, err := pm.PSvc.GetFilesystem(pm.Prj)
if err != nil {
return err
}
stat, err := tgt.Stat(fn)
if err != nil {
return err
}
content, err := tgt.ReadFile(fn)
if err != nil {
return err
}
c := string(content)
lines := util.StringSplitLines(c)
var hit bool
fixed := make([]string, 0, len(lines))
lo.ForEach(lines, func(line string, _ int) {
if strings.Contains(line, file.HeaderContent) {
hit = true
} else {
fixed = append(fixed, line)
}
})
if hit {
final := strings.Join(fixed, util.StringDetectLinebreak(c))
err = tgt.WriteFile(fn, []byte(final), stat.Mode, true)
if err != nil {
return errors.Wrapf(err, "can't overwrite file at path [%s]", fn)
}
ret.AddLog("removed header from file [%s]", fn)
} else {
ret.AddLog("no header to remove from file [%s]", fn)
}
return nil
}