Skip to content

Commit

Permalink
directory-support (#4)
Browse files Browse the repository at this point in the history
* WiP

* adding directory support
  • Loading branch information
agrimrules authored Jul 13, 2021
1 parent 3c7b7d9 commit 16d63bb
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 5 deletions.
11 changes: 11 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
"README.md",
"README.md",
]
},
{
"name": "test with dirs",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"args": [
"/home/agrim/Downloads/kubernetes-master",
"/home/agrim/Downloads/kubernetes-master-1",
]
}
]
}
23 changes: 19 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,25 @@ var rootCmd = &cobra.Command{
}
switch len(args) {
case 2:
data := utils.GetMD5ForFiles(args)
table := utils.CreateTable(data)
table.Render()
os.Exit(exitcode)
if utils.IsDirectory(args[0]) && utils.IsDirectory(args[1]) {
d1 := utils.GetMD5ForFiles(utils.ReturnFilesInFolder(args[0]))
d2 := utils.GetMD5ForFiles(utils.ReturnFilesInFolder(args[1]))
areEqual, diff := utils.HashesAreEqual(d1, d2)
if !areEqual {
table := utils.CreateDirTable(diff)
table.Render()
os.Exit(exitcode)
}
fmt.Println("Files match ✅")
os.Exit(exitcode)
} else if !utils.IsDirectory(args[0]) && !utils.IsDirectory(args[1]) {
data := utils.GetMD5ForFiles(args)
table := utils.CreateTable(data)
table.Render()
os.Exit(exitcode)
}
fmt.Println("Can only compare two folders or two files")

default:
fmt.Println("Invalid Arguments")
os.Exit(-1)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/agrimrules/hashmatch
go 1.16

require (
github.com/karrick/godirwalk v1.16.1 // indirect
github.com/olekukonko/tablewriter v0.0.5
github.com/spf13/cobra v1.1.3
github.com/thoas/go-funk v0.9.0 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/karrick/godirwalk v1.16.1 h1:DynhcF+bztK8gooS0+NDJFrdNZjJ3gzVzC545UNA9iw=
github.com/karrick/godirwalk v1.16.1/go.mod h1:j4mkqPuvaLI8mp1DroR3P6ad7cyYd4c1qeJ3RV7ULlk=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down Expand Up @@ -162,7 +164,10 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/thoas/go-funk v0.9.0 h1:Yzu8aTjTb1sqHZzSZLBt4qaZrFfjNizhA7IfnefjEzo=
github.com/thoas/go-funk v0.9.0/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
Expand Down Expand Up @@ -281,6 +286,7 @@ gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
30 changes: 30 additions & 0 deletions utils/files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package utils

import (
"fmt"
"os"

"github.com/karrick/godirwalk"
)

func IsDirectory(fileName string) bool {
fi, err := os.Stat(fileName)
if err != nil {
fmt.Println(err)
return false
}
return fi.IsDir()
}

func ReturnFilesInFolder(folder string) []string {
files := []string{}
godirwalk.Walk(folder, &godirwalk.Options{
Callback: func(osPathname string, directoryEntry *godirwalk.Dirent) error {
if directoryEntry.IsRegular() {
files = append(files, osPathname)
}
return nil
}, Unsorted: false,
})
return files
}
13 changes: 12 additions & 1 deletion utils/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,23 @@ func GetMD5ForFiles(files []string) []HashResults {
log.Fatal(err)
os.Exit(-1)
}
defer f.Close()
h := md5.New()
if _, err := io.Copy(h, f); err != nil {
log.Fatal(err)
os.Exit(-1)
}
results = append(results, HashResults{f.Name(), fmt.Sprintf("%x", h.Sum(nil))})
f.Close()
}
return results
}

func HashesAreEqual(hr1 []HashResults, hr2 []HashResults) (bool, []HashResults) {
mismatches := []HashResults{}
for i, v := range hr1 {
if v.hash != hr2[i].hash {
mismatches = append(mismatches, v, hr2[i])
}
}
return len(mismatches) == 0, mismatches
}
18 changes: 18 additions & 0 deletions utils/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/olekukonko/tablewriter"
"github.com/thoas/go-funk"
)

func CreateTable(rows []HashResults) *tablewriter.Table {
Expand All @@ -25,3 +26,20 @@ func CreateTable(rows []HashResults) *tablewriter.Table {
}
return table
}

func CreateDirTable(rows []HashResults) *tablewriter.Table {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Source File", "md5sum", "Destination File", "md5sum"})
table.SetHeaderColor(tablewriter.Colors{tablewriter.FgBlackColor, tablewriter.Bold, tablewriter.BgGreenColor},
tablewriter.Colors{tablewriter.FgBlackColor, tablewriter.Bold, tablewriter.BgGreenColor},
tablewriter.Colors{tablewriter.FgBlackColor, tablewriter.Bold, tablewriter.BgGreenColor},
tablewriter.Colors{tablewriter.FgBlackColor, tablewriter.Bold, tablewriter.BgGreenColor})
if len(rows) >= 1 {
table.SetFooter([]string{"Files don't match ❌", "", "", ""})
dualRows := funk.Chunk(rows, 2).([][]HashResults)
for _, v := range dualRows {
table.Append([]string{v[0].filename, v[0].hash, v[1].filename, v[1].hash})
}
}
return table
}

0 comments on commit 16d63bb

Please sign in to comment.