Skip to content

Commit c9dae30

Browse files
committed
complete main function
1 parent a1a6309 commit c9dae30

File tree

6 files changed

+85
-41
lines changed

6 files changed

+85
-41
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
organizer
2-
tests
2+
tests
3+
test_1

archiver.go

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ const FLATE = "flate"
88
const GZIP = "gzip"
99
const LZW = "lzw"
1010
const ZLIB = "zlib"
11+
12+
// for future releases

main.go

+45-24
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,54 @@ package main
22

33
import (
44
"fmt"
5+
"os"
6+
"strings"
57
)
68

9+
const VALIDATION_ERROR = -1
10+
711
func main() {
8-
InitArgs()
12+
var check bool
13+
var msg string
914

10-
fmt.Println(Dirs)
15+
InitArgs()
1116

17+
dirs := strings.Split(Dirs, ", ")
18+
19+
// directories validation
20+
check, msg = AreDirectories(dirs)
21+
if !check {
22+
fmt.Println(msg)
23+
os.Exit(VALIDATION_ERROR)
24+
}
25+
26+
// lang validation
27+
check, msg = isLangCorrect(Lang)
28+
if !check {
29+
fmt.Println(msg)
30+
os.Exit(VALIDATION_ERROR)
31+
}
32+
33+
// method validation
34+
check, msg = isMethodCorrect(Method)
35+
if !check {
36+
fmt.Println(msg)
37+
os.Exit(VALIDATION_ERROR)
38+
}
39+
40+
// TODO for future release
41+
// Archive validation
42+
// Compress validation
43+
44+
for _, dir := range dirs {
45+
err := MoveFiles(dir, OrganizeDir(
46+
dir,
47+
strings.Split(IgnoresFiles, ", "),
48+
strings.Split(IgnoresExt, ", "),
49+
Lang, Method))
50+
51+
if err != nil {
52+
fmt.Println("An error occured while processing ", err)
53+
}
54+
}
1255
}
13-
14-
// organize folder by types of file (extensions)
15-
// images, musics, videos, docs, packages, execs, books,
16-
// archives, fonts,
17-
18-
// organize folder by alphabetical order
19-
20-
// Organize by extension
21-
22-
// By name looking same
23-
24-
// By last modification date
25-
26-
// archive folders, select archive type
27-
28-
// work on multiple folders simultanouely
29-
30-
// ignore some extensions
31-
32-
// ignore some files
33-
34-
// lang choice

organizers.go

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package main
22

33
import (
4-
"os"
4+
"fmt"
5+
"io/ioutil"
56
"strings"
67
)
78

@@ -12,22 +13,37 @@ const DATE_M = "date"
1213
const EXTENSION_M = "ext"
1314
const LOOKING_SAME = "same" // For future release
1415

15-
func OrganizeDir(filesInfos []os.FileInfo, ignore []string, lang string, method string) map[string][]string {
16+
func OrganizeDir(
17+
dirPath string,
18+
ignoreFiles []string,
19+
ignoresExt []string,
20+
lang string,
21+
method string) map[string][]string {
22+
23+
filesInfos, err := ioutil.ReadDir(dirPath)
24+
if err != nil {
25+
fmt.Println("Error while reading ", dirPath)
26+
return nil
27+
}
28+
1629
var ext string
1730
var fileType FileType
1831
var ftName string
1932
var uName string
2033
var firstL string
2134
var dateStr string
22-
var isIgnore bool
35+
var isIgnoreF bool
36+
var isIgnoreE bool
2337

2438
organizer := make(map[string][]string)
2539

2640
for _, info := range filesInfos {
27-
isIgnore = Contains(info.Name(), ignore)
41+
ext = GetFileExtension(info.Name())
42+
isIgnoreF = Contains(info.Name(), ignoreFiles)
43+
isIgnoreE = Contains(ext, ignoresExt)
44+
45+
if !info.IsDir() && !isIgnoreF && !isIgnoreE {
2846

29-
if !info.IsDir() && !isIgnore {
30-
ext = GetFileExtension(info.Name())
3147
fileType = GetFileType(ext)
3248
ftName = fileType.val(lang)
3349

parser.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,27 @@ func InitArgs() {
3636
Example : -method type
3737
`)
3838

39-
flag.StringVar(&IgnoresFiles, "-ignore-file", "",
40-
`
41-
Ignores some files while organising directories
39+
flag.StringVar(&IgnoresFiles, "ef", "",
40+
`Exclude some files while organising directories
4241
Example for one file : --ignore-file file.txt
4342
Example for mulitple files : --ignore-file "file1.txt, file2.txt, file3.txt"
4443
`)
4544

46-
flag.StringVar(&IgnoresExt, "-ignore-ext", "",
47-
`
48-
Ignores files with given extensions
45+
flag.StringVar(&IgnoresExt, "ex", "",
46+
`Excludes files with given extensions
4947
Example for one extension : --ignore-ext txt
5048
Example for mulitple extensions : --ignore-file "mp3, txt, json"
5149
`)
5250

5351
flag.StringVar(&Archive, "archive", "zip",
54-
`
52+
`(Not working yet)
5553
Archive created folders with specified method
5654
Suported methods are zip and tar
5755
Example : -archive zip
5856
`)
5957

6058
flag.StringVar(&Compress, "compress", "gzip",
61-
`
59+
`(Not working yet)
6260
Compress created folders with specified algorithm
6361
Suported algorithms are : bzip2, flate, gzip, lzw, zlib
6462
Example : -archive gzip

utils.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bytes"
45
"os"
56
"os/exec"
67
"path/filepath"
@@ -9,7 +10,7 @@ import (
910
)
1011

1112
const OS_WINDOWS = "windows"
12-
const BYTE_PERMS = 664
13+
const BYTE_PERMS = 0775
1314

1415
// Return extension of a file given his name as parameter
1516
func GetFileExtension(fileName string) string {
@@ -34,7 +35,7 @@ func Contains(val string, tab []string) bool {
3435
}
3536

3637
func mv(filePath, destinationPath string) error {
37-
oSys := strings.Split(runtime.GOARCH, "/")[0]
38+
oSys := strings.Split(runtime.GOOS, "/")[0]
3839
cmd := new(exec.Cmd)
3940

4041
if oSys == OS_WINDOWS {
@@ -43,6 +44,11 @@ func mv(filePath, destinationPath string) error {
4344
cmd = exec.Command("mv", filePath, destinationPath)
4445
}
4546

47+
var out bytes.Buffer
48+
var stderr bytes.Buffer
49+
cmd.Stdout = &out // standard output
50+
cmd.Stderr = &stderr // errors output
51+
4652
err := cmd.Run()
4753

4854
return err

0 commit comments

Comments
 (0)