-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove_gen_files.go
66 lines (57 loc) · 1.16 KB
/
move_gen_files.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
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"sync"
)
const source = "proto/gen/"
func main() {
cmd := exec.Command("ls")
cmd.Dir = source
bytes, err := cmd.Output()
if err != nil {
panic(err)
}
data := strings.Fields(string(bytes))
wg := sync.WaitGroup{}
for _, file := range data {
if strings.Contains(file, "admin") {
wg.Add(1)
go moveFile(file, "cmd/admin/gen/", &wg)
} else if strings.Contains(file, "user") {
wg.Add(1)
go moveFile(file, "cmd/user/gen/", &wg)
} else if strings.Contains(file, "books") {
wg.Add(1)
go moveFile(file, "cmd/books/gen/", &wg)
}
}
wg.Wait()
// err = os.RemoveAll(source)
// if err != nil {
// panic(err)
// }
}
func moveFile(filename, path string, wg *sync.WaitGroup) {
file, err := os.ReadFile(source + filename)
fmt.Println("file read: ", source + filename)
if err != nil {
panic(err)
}
newFile, err := os.OpenFile(path + filename, os.O_CREATE | os.O_RDWR, os.ModeAppend)
if err != nil {
panic(err)
}
defer newFile.Close()
err = os.Chmod(newFile.Name(), 0644)
if err != nil {
panic(err)
}
_, err = newFile.Write(file)
if err != nil {
panic(err)
}
defer wg.Done()
}