-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (58 loc) · 1.41 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
package main
import (
"bufio"
"flag"
"io"
"io/ioutil"
"os"
"strings"
"github.com/mylxsw/go-utils/diff"
"github.com/mylxsw/go-utils/file"
)
var dataDir, name, inputFile string
var contextLine, keepVersion uint
func main() {
flag.StringVar(&dataDir, "data-dir", "/tmp", "状态数据存储目录")
flag.StringVar(&name, "name", "default", "当前对比项目名称")
flag.UintVar(&contextLine, "context-line", 0, "输出上下文行数")
flag.StringVar(&inputFile, "file", "", "对比的文件名,留空则从标准输入读取")
flag.UintVar(&keepVersion, "keep-version", 100, "保留多少个版本的历史记录")
flag.Parse()
var target string
if inputFile != "" {
targetBytes, err := ioutil.ReadFile(inputFile)
if err != nil {
panic(err)
}
target = string(targetBytes)
} else {
target = readStdin(100000)
}
fs := file.LocalFS{}
if err := fs.MkDir(dataDir); err != nil {
panic(err)
}
differ := diff.NewDiffer(fs, dataDir, int(contextLine))
res := differ.DiffLatest(name, target)
if err := res.PrintAndSave(os.Stdout); err != nil {
panic(err)
}
_ = res.Clean(keepVersion)
}
func readStdin(maxLines int) string {
result := ""
reader := bufio.NewReader(os.Stdin)
lineNo := 0
for {
line, err := reader.ReadString('\n')
if err == io.EOF {
break
}
lineNo++
if lineNo > maxLines {
break
}
result += line
}
return strings.TrimSpace(strings.TrimSuffix(result, "\n"))
}