Skip to content

Instantly share code, notes, and snippets.

@yudppp yudppp/main.go
Created May 8, 2017

Embed
What would you like to do?
goduplication check
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"github.com/urfave/cli"
)
func main() {
var dir string
cli.HelpFlag = cli.BoolFlag{
Name: "help",
Usage: "To show help for the tool",
}
app := cli.NewApp()
app.Name = "goduplicheck"
app.Usage = "duplication check tool"
app.Version = "0.0.1"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "dir, d",
Usage: "check directory",
Destination: &dir,
},
}
app.Action = func(c *cli.Context) error {
fmt.Println("search dir:", dir)
return exec(dir)
}
app.Run(os.Args)
}
func exec(dir string) error {
fileInfos, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
checklist := make([]string, 0, len(fileInfos))
for _, file := range fileInfos {
if file.IsDir() {
fmt.Printf("[warn] %s is directory.\n", file.Name())
continue
}
checklist = append(checklist, path.Join(dir, file.Name()))
}
fmt.Println(checklist)
existMap := make(map[string]bool, 1000)
for _, fileName := range checklist {
fmt.Println("search start : ", fileName)
err := func(fileName string) error {
fp, err := os.Open(fileName)
if err != nil {
return err
}
defer fp.Close()
reader := bufio.NewReaderSize(fp, 4096)
for {
line, _, err := reader.ReadLine()
if err == io.EOF {
break
} else if err != nil {
return err
}
_, exist := existMap[string(line)]
if exist {
fmt.Println("already exist:", string(line))
}
existMap[string(line)] = true
}
return nil
}(fileName)
if err != nil {
return err
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.