-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (99 loc) · 2.36 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
package main
import (
"bufio"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/no-src/log"
"github.com/no-src/log/level"
)
func main() {
// init logger
defer log.Close()
log.InitDefaultLogger(log.NewConsoleLogger(level.DebugLevel))
currentExeFilePath, err := os.Executable()
if err != nil {
log.Error(err, "get current executable error")
return
}
workDir := filepath.Dir(currentExeFilePath)
if len(os.Args) > 1 {
workDir = os.Args[1]
}
workDir, err = filepath.Abs(workDir)
if err != nil {
log.Error(err, "parse work dir to abs error")
return
}
currentExeFile, err := os.Stat(currentExeFilePath)
if err != nil {
log.Error(err, "read current program stat error")
return
}
log.Info("current work dir:[%s]", workDir)
allFile, err := os.ReadDir(workDir)
if err != nil {
log.Error(err, "read dir error")
return
}
var combFileList CompFileList
combFilePrefix := "comb.file."
for _, f := range allFile {
if f.IsDir() {
continue
}
fName := f.Name()
currentExeFileName := currentExeFile.Name()
if fName == currentExeFileName || strings.HasPrefix(fName, combFilePrefix) {
log.Debug("ignore file [%s]", fName)
continue
}
combFileList = append(combFileList, f)
}
sort.Sort(combFileList)
combFilePath := filepath.Join(workDir, combFilePrefix+time.Now().Format("20060102150405"))
combFile, err := os.Create(combFilePath)
if err != nil {
log.Error(err, "open comb file error")
return
}
defer combFile.Close()
combWriter := bufio.NewWriter(combFile)
for _, item := range combFileList {
data, err := os.ReadFile(filepath.Join(workDir, item.Name()))
if err != nil {
log.Error(err, "read file error")
return
}
nn, err := combWriter.Write(data)
if err != nil {
log.Error(err, "write to comb file error")
return
} else {
log.Debug("write [%s] to comb file success with %d bytes", item.Name(), nn)
}
}
err = combWriter.Flush()
if err != nil {
log.Error(err, "flush data to comb file error")
} else {
log.Info("combine file success")
}
}
type CompFileList []os.DirEntry
func (list CompFileList) Len() int {
return len(list)
}
func (list CompFileList) Swap(i, j int) {
list[i], list[j] = list[j], list[i]
}
func (list CompFileList) Less(i, j int) bool {
lenI := len(list[i].Name())
lenJ := len(list[j].Name())
if lenI != lenJ {
return lenI < lenJ
}
return list[i].Name() < list[j].Name()
}