-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
73 lines (62 loc) · 1.12 KB
/
main.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"secret/crypt"
"sync"
)
var wg sync.WaitGroup
var decrypt bool
var secret string
func init() {
flag.BoolVar(&decrypt, "decrypt", false, "-decrypt")
flag.StringVar(&secret, "secret", "", "-secret=your_secret")
flag.Parse()
}
func exec(name string) {
defer wg.Done()
b, _ := ioutil.ReadFile(name)
var newText string
if decrypt {
newText = crypt.Decrypt(b, secret)
} else {
newText = crypt.Encrypt(b, secret)
}
ioutil.WriteFile(name, []byte(newText), 0644)
}
func interate(name string) {
defer wg.Done()
fi, err := os.Stat(name)
if err != nil {
fmt.Println(err)
return
}
switch mode := fi.Mode(); {
case mode.IsDir():
var i = 0
filepath.Walk(name, func(path string, f os.FileInfo, err error) error {
if i > 0 {
wg.Add(1)
go interate(path)
}
i++
return nil
})
case mode.IsRegular():
fmt.Println("Running at:", name)
wg.Add(1)
go exec(name)
}
}
func main() {
name := os.Args[len(os.Args)-1]
if secret == "" {
panic("Invalid secret, try: -secret=your_secret")
}
wg.Add(1)
go interate(name)
wg.Wait()
}