Skip to content

Commit f46ad52

Browse files
authored
feat: makecheck (#3)
* feat: makecheck * fix: fix version
1 parent 6b334e4 commit f46ad52

File tree

7 files changed

+190
-27
lines changed

7 files changed

+190
-27
lines changed

cmd/kcheck/main.go

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/littlecxm/kbinxml-go"
1111
"github.com/littlecxm/kcheck/configs"
1212
"github.com/littlecxm/kcheck/pkg/checksum"
13+
"github.com/littlecxm/kcheck/pkg/utils"
1314
"github.com/urfave/cli/v2"
1415
"io/ioutil"
1516
"log"
@@ -22,7 +23,6 @@ import (
2223

2324
var json = jsoniter.ConfigCompatibleWithStandardLibrary
2425
var (
25-
workDir string
2626
version, buildDate, commitID string
2727
listType, listPath string
2828
)
@@ -33,7 +33,7 @@ func main() {
3333
app := &cli.App{
3434
Name: "kcheck",
3535
Usage: "check files through list",
36-
Version: fmt.Sprintf("%s (built: %s)", commitID, buildDate),
36+
Version: fmt.Sprintf("%s %s (built: %s)", version, commitID, buildDate),
3737
Flags: []cli.Flag{
3838
&cli.StringFlag{
3939
Name: "type",
@@ -105,10 +105,10 @@ func main() {
105105
err,
106106
formatPath,
107107
}
108-
printStatus(false, formatPath)
108+
utils.PrintStatus(false, formatPath)
109109
} else {
110110
passCount++
111-
printStatus(true, formatPath)
111+
utils.PrintStatus(true, formatPath)
112112
}
113113
}
114114
close(res)
@@ -143,10 +143,10 @@ func main() {
143143
err,
144144
formatPath,
145145
}
146-
printStatus(false, formatPath)
146+
utils.PrintStatus(false, formatPath)
147147
} else {
148148
passCount++
149-
printStatus(true, formatPath)
149+
utils.PrintStatus(true, formatPath)
150150
}
151151
}
152152
case configs.KCheckType:
@@ -167,10 +167,10 @@ func main() {
167167
err,
168168
formatPath,
169169
}
170-
printStatus(false, formatPath)
170+
utils.PrintStatus(false, formatPath)
171171
} else {
172172
passCount++
173-
printStatus(true, formatPath)
173+
utils.PrintStatus(true, formatPath)
174174
}
175175
}
176176
default:
@@ -193,14 +193,3 @@ func main() {
193193
os.Exit(1)
194194
}
195195
}
196-
197-
func printStatus(isSuccess bool, path string) {
198-
successDisp := color.New(color.Bold, color.FgWhite, color.BgGreen).FprintfFunc()
199-
failedDisp := color.New(color.Bold, color.FgWhite, color.BgRed).FprintfFunc()
200-
if isSuccess {
201-
successDisp(color.Output, "[PASSED]")
202-
} else {
203-
failedDisp(color.Output, "[FAILED]")
204-
}
205-
fmt.Println(" ", path)
206-
}

cmd/makecheck/handler.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"log"
7+
"os"
8+
)
9+
10+
type CheckResult struct {
11+
Success bool
12+
Error error
13+
Path string
14+
}
15+
16+
func handler(res chan *CheckResult) {
17+
file, err := os.OpenFile("failed_make.list", os.O_APPEND|os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
18+
if err != nil {
19+
log.Fatalf("failed creating result log: %s", err)
20+
}
21+
dataWriter := bufio.NewWriter(file)
22+
for r := range res {
23+
_, _ = dataWriter.WriteString(fmt.Sprintf("[%s]: %s\n", r.Error, r.Path))
24+
dataWriter.Flush()
25+
}
26+
file.Close()
27+
}

cmd/makecheck/main.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package main
2+
3+
import (
4+
"crypto/sha1"
5+
"encoding/hex"
6+
"fmt"
7+
jsoniter "github.com/json-iterator/go"
8+
"github.com/littlecxm/kcheck/configs"
9+
"github.com/littlecxm/kcheck/pkg/utils"
10+
"github.com/urfave/cli/v2"
11+
"io"
12+
"io/ioutil"
13+
"log"
14+
"os"
15+
"path/filepath"
16+
"sort"
17+
"strings"
18+
"time"
19+
)
20+
21+
var json = jsoniter.ConfigCompatibleWithStandardLibrary
22+
var (
23+
version, buildDate, commitID string
24+
srcDir, outFilename string
25+
isIndent bool
26+
)
27+
28+
func main() {
29+
fmt.Printf("makecheck %s\n", version)
30+
configs.WorkDir, _ = os.Getwd()
31+
app := &cli.App{
32+
Name: "makecheck",
33+
Usage: "make check list for kcheck",
34+
Version: fmt.Sprintf("%s %s (built: %s)", version, commitID, buildDate),
35+
Flags: []cli.Flag{
36+
&cli.StringFlag{
37+
Name: "src",
38+
Aliases: []string{"s"},
39+
Usage: "source `DIR`",
40+
Destination: &srcDir,
41+
},
42+
&cli.StringFlag{
43+
Name: "output",
44+
Aliases: []string{"o"},
45+
Value: "kcheck.json",
46+
Usage: "output filename for check list `FILE`",
47+
Destination: &outFilename,
48+
},
49+
&cli.BoolFlag{
50+
Name: "indent",
51+
Aliases: []string{"ind"},
52+
Value: false,
53+
Usage: "enable indent for output list",
54+
Destination: &isIndent,
55+
},
56+
},
57+
Action: func(c *cli.Context) error {
58+
if c.NArg() == 0 {
59+
cli.ShowAppHelpAndExit(c, 0)
60+
}
61+
if c.NArg() > 0 {
62+
srcDir = c.Args().Get(0)
63+
}
64+
65+
h := sha1.New()
66+
var kcheckList configs.KCheckList
67+
res := make(chan *CheckResult, 999)
68+
go handler(res)
69+
err := filepath.Walk(srcDir, func(filePath string, fi os.FileInfo, err error) error {
70+
if err != nil {
71+
return err
72+
}
73+
if strings.Contains(strings.ToLower(fi.Name()), ".ds_store") {
74+
return nil
75+
}
76+
srcPath := filepath.ToSlash(strings.TrimPrefix(strings.Replace(filePath, srcDir, "", -1), string(filepath.Separator)))
77+
if !fi.Mode().IsRegular() {
78+
return nil
79+
}
80+
f, err := os.Open(filePath)
81+
_, err = io.Copy(h, f)
82+
if err != nil {
83+
res <- &CheckResult{
84+
false,
85+
err,
86+
srcPath,
87+
}
88+
utils.PrintStatus(false, srcPath)
89+
return err
90+
} else {
91+
utils.PrintStatus(true, srcPath)
92+
}
93+
kcheckList.Files = append(kcheckList.Files, configs.KCheckFiles{Path: srcPath, SHA1: hex.EncodeToString(h.Sum(nil)), Size: fi.Size()})
94+
f.Close()
95+
return nil
96+
})
97+
if err != nil {
98+
log.Fatalln("filepath err:", err)
99+
}
100+
var outBytes []byte
101+
if isIndent {
102+
outBytes, err = json.Marshal(kcheckList)
103+
} else {
104+
outBytes, err = json.MarshalIndent(kcheckList, "", " ")
105+
}
106+
err = ioutil.WriteFile(filepath.Join(configs.WorkDir, outFilename), outBytes, os.ModePerm)
107+
if err != nil {
108+
log.Fatalln("save list err:", err)
109+
}
110+
fmt.Println("--------")
111+
fmt.Println("Check list saved:", outFilename)
112+
fmt.Println("Finished.")
113+
fmt.Println("Exit after 2 seconds...")
114+
time.Sleep(3 * time.Second)
115+
return nil
116+
},
117+
}
118+
119+
sort.Sort(cli.FlagsByName(app.Flags))
120+
sort.Sort(cli.CommandsByName(app.Commands))
121+
122+
err := app.Run(os.Args)
123+
if err != nil {
124+
log.Println(err)
125+
os.Exit(1)
126+
}
127+
}

cmd/makechk/main.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

configs/type.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ type MetaData struct {
1919
}
2020

2121
type KCheckList struct {
22-
CreatedAt int64 `json:"createdAt"`
23-
Files []struct {
24-
Path string `json:"path"`
25-
SHA1 string `json:"sha1"`
26-
Size int64 `json:"size"`
27-
} `json:"files"`
22+
CreatedAt int64 `json:"createdAt"`
23+
Files []KCheckFiles `json:"files"`
24+
}
25+
26+
type KCheckFiles struct {
27+
Path string `json:"path"`
28+
SHA1 string `json:"sha1"`
29+
Size int64 `json:"size"`
2830
}

makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ GOARCH ?= amd64
88
all: fmt build
99
build:
1010
@go mod download
11-
@echo build kcheck..
11+
@echo build kcheck...
1212
GOOS=${GOOS} GOARCH=${GOARCH} go build -o build/ -ldflags "-s -X \"main.version=${VERSION}\" -X \"main.buildDate=${BUILD_DATE}\" -X \"main.commitID=${SHA_SHORT}\"" -v ./cmd/kcheck
13+
@echo build makecheck...
14+
GOOS=${GOOS} GOARCH=${GOARCH} go build -o build/ -ldflags "-s -X \"main.version=${VERSION}\" -X \"main.buildDate=${BUILD_DATE}\" -X \"main.commitID=${SHA_SHORT}\"" -v ./cmd/makecheck
1315
windows: GOOS=windows
1416
windows: GOARCH=amd64
1517
windows: build

pkg/utils/color.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"github.com/fatih/color"
6+
)
7+
8+
func PrintStatus(isSuccess bool, path string) {
9+
successDisp := color.New(color.Bold, color.FgWhite, color.BgGreen).FprintfFunc()
10+
failedDisp := color.New(color.Bold, color.FgWhite, color.BgRed).FprintfFunc()
11+
if isSuccess {
12+
successDisp(color.Output, "[PASSED]")
13+
} else {
14+
failedDisp(color.Output, "[FAILED]")
15+
}
16+
fmt.Println(" ", path)
17+
}

0 commit comments

Comments
 (0)