Skip to content

Commit

Permalink
Adding bufferio for reading large files
Browse files Browse the repository at this point in the history
Signed-off-by: Raj Babu Das <[email protected]>
  • Loading branch information
imrajdas committed Aug 26, 2023
1 parent 2847045 commit 40d5e82
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
main
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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()
}
2 changes: 1 addition & 1 deletion pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 39 additions & 7 deletions pkg/diffr/diff.go
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 40d5e82

Please sign in to comment.