-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (124 loc) · 2.78 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"bufio"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"sort"
"strings"
"time"
)
func main() {
var outPath string
var dataPath string
flag.StringVar(&outPath, "outPath", "", "shovill output file path")
flag.StringVar(&dataPath, "dataPath", "", "shovill source file path")
flag.Parse()
if (len(outPath) == 0 || len(dataPath) == 0) {
logger("missing params !!")
return
}
var files []string
files, err := GetAllFile(dataPath, files)
if err != nil {
logger("read file error")
return
}
for _, file := range files {
length := strings.Index(file, "_")
outPutDirSeg := file[0:length]
desPath := outPath + "/" + outPutDirSeg
_, err := os.Stat(desPath)
if os.IsNotExist(err) {
ret := getGenArray(outPutDirSeg, files)
if len(ret) != 2 {
continue
}
file1 := ret[0]
file2 := ret[1]
cmd := "shovill --trim --outdir " + outPath + "/" + outPutDirSeg + " --force --R1 " + dataPath + "/" + file1 + " --R2 " + dataPath + "/" + file2
logger(cmd)
Command(cmd)
}
}
}
func Command(cmd string) error {
c := exec.Command("bash", "-c", cmd)
stdout, err := c.StdoutPipe()
if err != nil {
return err
}
go func() {
reader := bufio.NewReader(stdout)
for {
readString, err := reader.ReadString('\n')
if err != nil || err == io.EOF {
break
}
fmt.Print(readString)
}
}()
return c.Run()
}
func getGenArray(seg string, files []string) ([]string) {
var ret []string
for _, file := range files {
length := strings.Index(file, "_")
outPutDirSeg := file[0:length]
if seg == outPutDirSeg {
ret = append(ret, file)
}
}
sort.Sort(sort.StringSlice(ret))
return ret
}
func GetAllFile(pathname string, s []string) ([]string, error) {
rd, err := ioutil.ReadDir(pathname)
if err != nil {
fmt.Println("read dir fail:", err)
return s, err
}
for _, fi := range rd {
if fi.IsDir() {
fullDir := pathname + "/" + fi.Name()
s, err = GetAllFile(fullDir, s)
if err != nil {
fmt.Println("read dir fail:", err)
return s, err
}
} else {
//仅返回文件名
//fullName := pathname + "/" + fi.Name()
fullName := fi.Name()
s = append(s, fullName)
}
}
return s, nil
}
func logger(logthis string) {
filePath := "logfile.log"
fmt.Println(logthis)
_, err := os.Stat(filePath)
if os.IsNotExist(err) {
_, err := os.Create(filePath)
if err != nil {
fmt.Println(err)
}
}
file, err := os.OpenFile(filePath, os.O_WRONLY | os.O_APPEND, 0666)
// file, err := os.OpenFile(filePath, os.O_WRONLY | os.O_APPEND, 0666)
if err != nil {
fmt.Printf("open file err=%v\n", err)
return
}
defer file.Close()
now := time.Now()
time := now.Format("2006/01/02 15:04:05")
str := fmt.Sprintf("%v %v \n", time, logthis)
writer := bufio.NewWriter(file)
writer.WriteString(str)
writer.Flush()
}