-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
122 lines (101 loc) · 2.61 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
const OS_WINDOWS = "windows"
const BYTE_PERMS = 0775
// Return extension of a file given his name as parameter
func GetFileExtension(fileName string) string {
tabExt := strings.Split(fileName, ".")
n := len(tabExt)
ext := tabExt[n-1]
return strings.ToLower(ext)
}
// Check if a table contains a given string
func Contains(val string, tab []string) bool {
check := false
for _, value := range tab {
if val == value {
check = true
break
}
}
return check
}
func mv(filePath, destinationPath string) error {
oSys := strings.Split(runtime.GOOS, "/")[0]
cmd := new(exec.Cmd)
if oSys == OS_WINDOWS {
cmd = exec.Command("move", filePath, destinationPath)
} else {
cmd = exec.Command("mv", filePath, destinationPath)
}
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out // standard output
cmd.Stderr = &stderr // errors output
err := cmd.Run()
return err
}
func MoveFiles(basePath string, organiser map[string][]string) error {
var err error
var workDir string
var destPath string
var currentPath string
for dir, fileNames := range organiser {
workDir = filepath.Join(basePath, dir)
if _, err = os.Stat(workDir); os.IsNotExist(err) {
err = os.Mkdir(workDir, BYTE_PERMS)
if err != nil {
return err
}
}
for _, fileName := range fileNames {
currentPath = filepath.Join(basePath, fileName)
destPath = filepath.Join(workDir, fileName)
err = mv(currentPath, destPath)
if err != nil {
return err
}
}
}
return nil
}
// Return file type given his extension
func GetFileType(ext string) FileType {
extensions := map[FileType][]string{
SOURCES_CODE: SOURCE_CODE_EXT,
SPREADSHEETS: SPREADSHEETS_EXT,
VIDEOS: VIDEOS_EXT,
PUBLISHING: PUBLISHING_EXT,
DOCUMENTS: DOCUMENTS_EXT,
PRESENTATIONS: PRESENTATION_EXT,
MEDIA_ARCHIVES: MEDIA_ARCHIVES_EXT,
AUDIO: AUDIO_EXT,
ARCHIVES: ARCHIVES_EXT,
PICTURES: PICTURES_EXT,
VECTOR_GRAPHICS: VECTOR_GRAPHICS_EXT,
_3D_GRAPHICS: _3_D_GRAPHICS_EXT,
SHORTCUTS: SHORTCUTS_EXT,
EXECUTABLES: EXECUTABLE_FILES_EXT,
CAD: COMPUTER_AID_DESIGN_EXT,
EDA: ELECTRONIC_DESIGN_AUTOMATION_EXT,
DATABASES: DATABASE_EXT,
FINANCIAL_DATAS: FINANCIAL_DATA_EXT,
FONTS: FONTS_EXT,
PLAYLIST: PLAYLIST_EXT,
VIRTUAL_MACHINES: VIRTUAL_MACHINES_EXT,
WEB_PAGES: WEB_PAGES_EXT,
}
for fileType, extList := range extensions {
if Contains(ext, extList) {
return fileType
}
}
return OTHERS
}