diff --git a/.gitignore b/.gitignore index 485dee6..92c16db 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .idea +main \ No newline at end of file diff --git a/main.go b/main.go index 04bb25b..160d47d 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,19 @@ package main import ( + "fmt" + "os" + "github.com/imrajdas/diffr/pkg/cmd/root" ) +var Version string + func main() { + err := os.Setenv("Version", Version) + if err != nil { + fmt.Println("Failed to fetched CLIVersion") + } + root.Execute() } diff --git a/pkg/cmd/root/root.go b/pkg/cmd/root/root.go index eba1901..1c01c5a 100644 --- a/pkg/cmd/root/root.go +++ b/pkg/cmd/root/root.go @@ -20,7 +20,7 @@ func Execute() { } func init() { - rootCmd.Flags().IntVarP(&diffr.Port, "port", "p", 8080, "Set the port for the web server to listen on, default is 8080") + rootCmd.Flags().IntVarP(&diffr.Port, "port", "p", 8675, "Set the port for the web server to listen on, default is 8080") rootCmd.Flags().StringVarP(&diffr.Address, "address", "a", "http://localhost", "Set the address for the web server to listen on, default is http://localhost") rootCmd.CompletionOptions.DisableDefaultCmd = true diff --git a/pkg/diffr/diff.go b/pkg/diffr/diff.go index 3332fff..634583c 100644 --- a/pkg/diffr/diff.go +++ b/pkg/diffr/diff.go @@ -1,29 +1,54 @@ package diffr import ( + "bufio" "fmt" - "io/ioutil" "os" "path/filepath" + "runtime" + "strings" "sync" "github.com/pmezard/go-difflib/difflib" ) +func readFileContent(filePath string) (string, error) { + file, err := os.Open(filePath) + if err != nil { + return "", err + } + defer file.Close() + + bufferedReader := bufio.NewReader(file) + var contentBuilder strings.Builder + bufferSize := 4096 + buffer := make([]byte, bufferSize) + + for { + n, err := bufferedReader.Read(buffer) + if err != nil { + break + } + contentBuilder.Write(buffer[:n]) + } + + return contentBuilder.String(), nil +} + func compareFiles(file1, file2 string) (string, error) { - content1, err := ioutil.ReadFile(file1) + content1, err := readFileContent(file1) if err != nil { return "", err } - content2, err := ioutil.ReadFile(file2) + content2, err := readFileContent(file2) if err != nil { return "", err } diff := difflib.UnifiedDiff{ - A: difflib.SplitLines(string(content1)), - B: difflib.SplitLines(string(content2)), + A: difflib.SplitLines(content1), + B: difflib.SplitLines(content2), FromFile: file1, ToFile: file2, Context: 3, @@ -69,9 +94,16 @@ func CompareDirectories(dir1, dir2 string, diffChan chan<- string, errorChan cha diffChan <- fmt.Sprintf("Differences in file: %s\n%s", relPath, diff) } } else if os.IsNotExist(err) { - diff, err := compareFiles(path1, "/dev/null") + var nullDevice string + if runtime.GOOS == "windows" { + nullDevice = "NUL" + } else { + nullDevice = "/dev/null" + } + + diff, err := compareFiles(path1, nullDevice) if err != nil { - errorChan <- fmt.Errorf("error comparing files %s and /dev/null: %s", path1, err) + errorChan <- fmt.Errorf("error comparing files %s and %s: %s", path1, nullDevice, err) return nil }