Skip to content

Commit 77ddb17

Browse files
committed
better CLI flag parse
1 parent b1e2346 commit 77ddb17

File tree

6 files changed

+195
-72
lines changed

6 files changed

+195
-72
lines changed

compressor/compress.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package compressor
33
import (
44
"fmt"
55
"os"
6-
"path"
76
"path/filepath"
87
"strings"
98
"sync"
@@ -17,7 +16,7 @@ import (
1716
func Compress(filenameStrs []string, outputDir string, password string) error {
1817
// Set default output directory if not provided
1918
if outputDir == "" {
20-
outputDir = path.Dir(filenameStrs[0])
19+
outputDir = filepath.Dir(filenameStrs[0])
2120
}
2221

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

2827
// Read all files concurrently
29-
err := ReadAllFilesConcurrently(filenameStrs, &files, &originalSize)
30-
if err != nil {
31-
return err
28+
errs := ReadAllFilesConcurrently(filenameStrs, &files, &originalSize)
29+
30+
// Check for errors from goroutines
31+
if len(errs) > 0 {
32+
for _, err := range errs {
33+
utils.ColorPrint(utils.RED, fmt.Sprintf("Error: %v\n", err))
34+
}
35+
os.Exit(1)
3236
}
3337

3438
// Compress files using Huffman coding
@@ -83,7 +87,7 @@ func Compress(filenameStrs []string, outputDir string, password string) error {
8387
return nil
8488
}
8589

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

108+
errors := make([]error, 0)
109+
104110
// Check for errors from goroutines
105111
for err := range errChan {
106112
if err != nil {
113+
//print all errors
107114
errMutex.Lock()
108-
defer errMutex.Unlock()
109-
return err // Return the first error encountered
115+
errors = append(errors, err)
116+
errMutex.Unlock()
110117
}
111118
}
112119

113-
return nil
120+
return errors
114121
}
115122

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

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

133142
// Store file information (name and content)
134143
*files = append(*files, utils.File{
135-
Name: path.Base(filePath),
144+
Name: filepath.Base(filePath),
136145
Content: content,
137146
})
138147

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

146155
// Set default output directory if not provided
147156
if outputDir == "" {
148-
outputDir = path.Dir(compressedFilename)
157+
outputDir = filepath.Dir(compressedFilename)
149158
}
150159

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

167176
// Decompress file using Huffman coding
168177
files, err := Unzip(utils.File{
169-
Name: path.Base(compressedFilename),
178+
Name: filepath.Base(compressedFilename),
170179
Content: compressedContent,
171180
})
172181

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

246255
func InvalidateFileName(file *utils.File, outputDir *string) {
247-
fileExt := path.Ext(file.Name)
256+
fileExt := filepath.Ext(file.Name)
248257
//extract the file name without the extension
249-
filename := path.Base(file.Name)
258+
filename := filepath.Base(file.Name)
250259
filename = strings.TrimSuffix(filename, fileExt)
251260

252261
count := 1

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package main
33
import (
44
"file-compressor/compressor"
55
"file-compressor/utils"
6-
6+
77
"time"
88
)
99

1010

11+
1112
func main() {
1213
//test files path '/test'
1314

readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ Run
1515

1616
```txt
1717
-a Read all files in the provided directory
18-
-d File paths to decompress
19-
-c File paths to compress
18+
-d File path to decompress (Only one file at once)
19+
-c File paths to compress (Space separated file paths)
2020
-o string
2121
Output directory for compressed files (Optional)
2222
-p string
@@ -27,11 +27,11 @@ Run
2727
### Compress
2828
#### Compress without password:
2929
```txt
30-
./sq -c file.txt,file2.txt
30+
./sq -c file.txt file2.txt
3131
```
3232
#### Compress with password:
3333
```txt
34-
./sq -c file.txt,file2.txt -p mySecurepass1234
34+
./sq -c file.txt file2.txt -p mySecurepass1234
3535
```
3636
#### Or compress the whole directory:
3737
```txt

resources.rc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
IDI_ICON1 ICON "_icon.ico"
44

55
1 VERSIONINFO
6-
FILEVERSION 1,0,7
7-
PRODUCTVERSION 1,0,7
6+
FILEVERSION 1,0,8
7+
PRODUCTVERSION 1,0,8
88
FILEOS 0x4
99
FILETYPE 0x1
1010
{
@@ -19,7 +19,7 @@ FILETYPE 0x1
1919
VALUE "LegalCopyright", "BrainbirdLab"
2020
VALUE "OriginalFilename", "SquirrelZip file archiver"
2121
VALUE "ProductName", "SquirrelZip file archiver"
22-
VALUE "ProductVersion", "1.0.7"
22+
VALUE "ProductVersion", "1.0.8"
2323
VALUE "Author", "Fuad Hasan"
2424
}
2525
}

resources.syso

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)