Skip to content

Commit 40d5e82

Browse files
committed
Adding bufferio for reading large files
Signed-off-by: Raj Babu Das <mail.rajdas@gmail.com>
1 parent 2847045 commit 40d5e82

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.idea
2+
main

main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
package main
22

33
import (
4+
"fmt"
5+
"os"
6+
47
"github.com/imrajdas/diffr/pkg/cmd/root"
58
)
69

10+
var Version string
11+
712
func main() {
13+
err := os.Setenv("Version", Version)
14+
if err != nil {
15+
fmt.Println("Failed to fetched CLIVersion")
16+
}
17+
818
root.Execute()
919
}

pkg/cmd/root/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func Execute() {
2020
}
2121

2222
func init() {
23-
rootCmd.Flags().IntVarP(&diffr.Port, "port", "p", 8080, "Set the port for the web server to listen on, default is 8080")
23+
rootCmd.Flags().IntVarP(&diffr.Port, "port", "p", 8675, "Set the port for the web server to listen on, default is 8080")
2424
rootCmd.Flags().StringVarP(&diffr.Address, "address", "a", "http://localhost", "Set the address for the web server to listen on, default is http://localhost")
2525

2626
rootCmd.CompletionOptions.DisableDefaultCmd = true

pkg/diffr/diff.go

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,54 @@
11
package diffr
22

33
import (
4+
"bufio"
45
"fmt"
5-
"io/ioutil"
66
"os"
77
"path/filepath"
8+
"runtime"
9+
"strings"
810
"sync"
911

1012
"github.com/pmezard/go-difflib/difflib"
1113
)
1214

15+
func readFileContent(filePath string) (string, error) {
16+
file, err := os.Open(filePath)
17+
if err != nil {
18+
return "", err
19+
}
20+
defer file.Close()
21+
22+
bufferedReader := bufio.NewReader(file)
23+
var contentBuilder strings.Builder
24+
bufferSize := 4096
25+
buffer := make([]byte, bufferSize)
26+
27+
for {
28+
n, err := bufferedReader.Read(buffer)
29+
if err != nil {
30+
break
31+
}
32+
contentBuilder.Write(buffer[:n])
33+
}
34+
35+
return contentBuilder.String(), nil
36+
}
37+
1338
func compareFiles(file1, file2 string) (string, error) {
14-
content1, err := ioutil.ReadFile(file1)
39+
content1, err := readFileContent(file1)
1540
if err != nil {
1641
return "", err
1742
}
1843

19-
content2, err := ioutil.ReadFile(file2)
44+
content2, err := readFileContent(file2)
2045
if err != nil {
2146
return "", err
2247
}
2348

2449
diff := difflib.UnifiedDiff{
25-
A: difflib.SplitLines(string(content1)),
26-
B: difflib.SplitLines(string(content2)),
50+
A: difflib.SplitLines(content1),
51+
B: difflib.SplitLines(content2),
2752
FromFile: file1,
2853
ToFile: file2,
2954
Context: 3,
@@ -69,9 +94,16 @@ func CompareDirectories(dir1, dir2 string, diffChan chan<- string, errorChan cha
6994
diffChan <- fmt.Sprintf("Differences in file: %s\n%s", relPath, diff)
7095
}
7196
} else if os.IsNotExist(err) {
72-
diff, err := compareFiles(path1, "/dev/null")
97+
var nullDevice string
98+
if runtime.GOOS == "windows" {
99+
nullDevice = "NUL"
100+
} else {
101+
nullDevice = "/dev/null"
102+
}
103+
104+
diff, err := compareFiles(path1, nullDevice)
73105
if err != nil {
74-
errorChan <- fmt.Errorf("error comparing files %s and /dev/null: %s", path1, err)
106+
errorChan <- fmt.Errorf("error comparing files %s and %s: %s", path1, nullDevice, err)
75107
return nil
76108
}
77109

0 commit comments

Comments
 (0)