|
| 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 | +} |
0 commit comments