Skip to content

Commit

Permalink
better CLI flag parse
Browse files Browse the repository at this point in the history
  • Loading branch information
itsfuad committed Aug 2, 2024
1 parent b1e2346 commit 77ddb17
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 72 deletions.
41 changes: 25 additions & 16 deletions compressor/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package compressor
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"sync"
Expand All @@ -17,7 +16,7 @@ import (
func Compress(filenameStrs []string, outputDir string, password string) error {
// Set default output directory if not provided
if outputDir == "" {
outputDir = path.Dir(filenameStrs[0])
outputDir = filepath.Dir(filenameStrs[0])
}

// Prepare to store files' content
Expand All @@ -26,9 +25,14 @@ func Compress(filenameStrs []string, outputDir string, password string) error {
var compressedSize int64

// Read all files concurrently
err := ReadAllFilesConcurrently(filenameStrs, &files, &originalSize)
if err != nil {
return err
errs := ReadAllFilesConcurrently(filenameStrs, &files, &originalSize)

// Check for errors from goroutines
if len(errs) > 0 {
for _, err := range errs {
utils.ColorPrint(utils.RED, fmt.Sprintf("Error: %v\n", err))
}
os.Exit(1)
}

// Compress files using Huffman coding
Expand Down Expand Up @@ -83,7 +87,7 @@ func Compress(filenameStrs []string, outputDir string, password string) error {
return nil
}

func ReadAllFilesConcurrently(filenameStrs []string, files *[]utils.File, originalSize *int64) error {
func ReadAllFilesConcurrently(filenameStrs []string, files *[]utils.File, originalSize *int64) []error {
// Use a wait group to synchronize goroutines
var wg sync.WaitGroup
var errMutex sync.Mutex // Mutex to handle errors safely
Expand All @@ -101,16 +105,19 @@ func ReadAllFilesConcurrently(filenameStrs []string, files *[]utils.File, origin
wg.Wait()
close(errChan)

errors := make([]error, 0)

// Check for errors from goroutines
for err := range errChan {
if err != nil {
//print all errors
errMutex.Lock()
defer errMutex.Unlock()
return err // Return the first error encountered
errors = append(errors, err)
errMutex.Unlock()
}
}

return nil
return errors
}

func readFileFromDisk(filePath string, files *[]utils.File, originalSize *int64, wg *sync.WaitGroup, errChan chan error) {
Expand All @@ -120,19 +127,21 @@ func readFileFromDisk(filePath string, files *[]utils.File, originalSize *int64,
// Check if the file or folder exists
info, err := os.Stat(filePath)
if os.IsNotExist(err) {
errChan <- fmt.Errorf("file or folder does not exist: %s", filePath)
errChan <- fmt.Errorf("file or folder does not exist: '%s'", filePath)
return
}

utils.ColorPrint(utils.YELLOW, fmt.Sprintf("Compressing file (%s)\n", filePath))
// Read file content
content, err := os.ReadFile(filePath)
if err != nil {
errChan <- fmt.Errorf("failed to read file %s: %w", filePath, err)
errChan <- fmt.Errorf("failed to read file '%s': %w", filePath, err)
return
}

// Store file information (name and content)
*files = append(*files, utils.File{
Name: path.Base(filePath),
Name: filepath.Base(filePath),
Content: content,
})

Expand All @@ -145,7 +154,7 @@ func Decompress(compressedFilename string, outputDir string, password string) er

// Set default output directory if not provided
if outputDir == "" {
outputDir = path.Dir(compressedFilename)
outputDir = filepath.Dir(compressedFilename)
}

compressedContent := make([]byte, 0)
Expand All @@ -166,7 +175,7 @@ func Decompress(compressedFilename string, outputDir string, password string) er

// Decompress file using Huffman coding
files, err := Unzip(utils.File{
Name: path.Base(compressedFilename),
Name: filepath.Base(compressedFilename),
Content: compressedContent,
})

Expand Down Expand Up @@ -244,9 +253,9 @@ func writeFileToDisk(file utils.File, outputDir string, wg *sync.WaitGroup, errC
}

func InvalidateFileName(file *utils.File, outputDir *string) {
fileExt := path.Ext(file.Name)
fileExt := filepath.Ext(file.Name)
//extract the file name without the extension
filename := path.Base(file.Name)
filename := filepath.Base(file.Name)
filename = strings.TrimSuffix(filename, fileExt)

count := 1
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package main
import (
"file-compressor/compressor"
"file-compressor/utils"

"time"
)



func main() {
//test files path '/test'

Expand Down
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ Run

```txt
-a Read all files in the provided directory
-d File paths to decompress
-c File paths to compress
-d File path to decompress (Only one file at once)
-c File paths to compress (Space separated file paths)
-o string
Output directory for compressed files (Optional)
-p string
Expand All @@ -27,11 +27,11 @@ Run
### Compress
#### Compress without password:
```txt
./sq -c file.txt,file2.txt
./sq -c file.txt file2.txt
```
#### Compress with password:
```txt
./sq -c file.txt,file2.txt -p mySecurepass1234
./sq -c file.txt file2.txt -p mySecurepass1234
```
#### Or compress the whole directory:
```txt
Expand Down
6 changes: 3 additions & 3 deletions resources.rc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
IDI_ICON1 ICON "_icon.ico"

1 VERSIONINFO
FILEVERSION 1,0,7
PRODUCTVERSION 1,0,7
FILEVERSION 1,0,8
PRODUCTVERSION 1,0,8
FILEOS 0x4
FILETYPE 0x1
{
Expand All @@ -19,7 +19,7 @@ FILETYPE 0x1
VALUE "LegalCopyright", "BrainbirdLab"
VALUE "OriginalFilename", "SquirrelZip file archiver"
VALUE "ProductName", "SquirrelZip file archiver"
VALUE "ProductVersion", "1.0.7"
VALUE "ProductVersion", "1.0.8"
VALUE "Author", "Fuad Hasan"
}
}
Expand Down
Binary file modified resources.syso
Binary file not shown.
Loading

0 comments on commit 77ddb17

Please sign in to comment.