Last active
December 17, 2015 04:39
-
-
Save harshavardhana/5551899 to your computer and use it in GitHub Desktop.
Trivial dircmp in Golang - [Continue to enhance..]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"path/filepath" | |
"os" | |
"flag" | |
"fmt" | |
) | |
type fileattr struct { | |
fileinfo os.FileInfo | |
path string | |
} | |
var left_list []fileattr | |
var right_list []fileattr | |
func Append(slice []fileattr, data fileattr) []fileattr { | |
l := len(slice) | |
if data.path != "" { | |
newSlice := make([]fileattr, l+1) | |
copy(newSlice, slice) | |
slice = newSlice | |
slice[l] = data | |
} | |
return slice | |
} | |
func left_visit(path string, f os.FileInfo, _ error) error { | |
var attr fileattr | |
attr.path = path | |
attr.fileinfo = f | |
left_list = Append(left_list, attr) | |
return nil | |
} | |
func right_visit(path string, f os.FileInfo, _ error) error { | |
var attr fileattr | |
attr.path = path | |
attr.fileinfo = f | |
right_list = Append(right_list, attr) | |
return nil | |
} | |
func cmp(dir1 string, dir2 string, list1 []fileattr, list2 []fileattr) { | |
// Very trivial check for dircmp - still needs more enhancements | |
if (len(list1) > len(list2)) { | |
fmt.Printf("Directory: %s supersedes in total files %d over Directory: %s\n", dir1, (len(list1) - len(list2)), dir2) | |
} else { | |
fmt.Printf("Directory %s supersedes in total files %d over Directory: %s\n", dir2, (len(list2) - len(list1)), dir1) | |
} | |
// | |
// | |
} | |
func main() { | |
flag.Parse() | |
left_dir := flag.Arg(0) | |
right_dir := flag.Arg(1) | |
if flag.NArg() < 2 { | |
fmt.Printf("Please provide arguments in this fashion <master> <slave>\n") | |
os.Exit(255) | |
} else if flag.NArg() == 2 { | |
filepath.Walk(left_dir, left_visit) | |
filepath.Walk(right_dir, right_visit) | |
} | |
cmp(left_dir, right_dir, left_list, right_list) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment