-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_analysis_orchestrator.go
251 lines (210 loc) · 5.43 KB
/
file_analysis_orchestrator.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package main
import (
"files_analyser/internal"
"fmt"
"github.com/c2h5oh/datasize"
"github.com/sqweek/dialog"
"gopkg.in/urfave/cli.v2"
"log"
"os"
)
func main() {
var sha_algorithm string
var skip_files, batch int
var delete_source_flag string
var recursivity_flag string
app := &cli.App{
Name: "Go File Analysis Suite",
Usage: "gofiles [mode]",
Version: "0.0.2",
Authors: []*cli.Author{
&cli.Author{
"Mesbah Khan",
"[email protected]"}},
Copyright: "copyright 2020",
Commands: []*cli.Command{
{
Name: "hash",
Aliases: []string{"h"},
Usage: "use it to create a hashtable for a directory. options: --hashAlgo {sha256, sha512, md5} sets hashing algorithms , --skipFiles {n} skips n files, --batchSize {n} writes n processed files",
Flags: []cli.Flag{
&cli.StringFlag{
"hashAlgo",
[]string{"sha"},
"hashing algorithm, options : sha256, sha512, md5",
[]string{""},
"",
false,
false,
false,
"",
"sha256",
&sha_algorithm,
false,
},
&cli.IntFlag{
"skipFiles",
[]string{"skip"},
"skip a number of files",
[]string{""},
"",
false,
false,
0,
"",
&skip_files,
false,
},
&cli.IntFlag{
"batchSize",
[]string{"batch"},
"skip a number of files",
[]string{""},
"",
false,
false,
0,
"",
&batch,
false,
},
},
Action: func(c *cli.Context) error {
Start_file_hash_analysis(sha_algorithm, skip_files, batch)
return nil
},
},
{
Name: "copy",
Aliases: []string{"c"},
Usage: "Use it to copy files using a csv loader with source and destination paths",
Flags: []cli.Flag{
&cli.StringFlag{
"deleteSource",
[]string{"ana"},
"enable deletion of source file : use copy -deleteSource",
[]string{""},
"",
false,
false,
false,
"",
"",
&delete_source_flag,
false,
},
},
Action: func(c *cli.Context) error {
Start_file_copy(delete_source_flag)
return nil
},
},
{
Name: "unzip",
Aliases: []string{"u"},
Usage: "use it to unzip all zips within a directory. Select recursivity using -recursive yes or no",
Flags: []cli.Flag{
&cli.StringFlag{
"recursivity",
[]string{"sha"},
"",
[]string{""},
"",
false,
false,
false,
"",
"yes",
&recursivity_flag,
false,
},
},
Action: func(c *cli.Context) error {
Unzip_files_in_folder(recursivity_flag)
return nil
},
},
{
Name: "report",
Aliases: []string{"a"},
Usage: "Use get an anlysis report on folder",
Action: func(c *cli.Context) error {
Analyse_source_folder()
return nil
},
},
},
}
app.Run(os.Args)
}
func Unzip_files_in_folder(recursivity_flag string) {
directory_name, directory_selection_error :=
dialog.Directory().
Title("Select log file storage location").Browse()
if directory_selection_error != nil {
fmt.Println(
directory_selection_error)
}
internal.Unzip_files_in_folder(directory_name, recursivity_flag)
}
func Start_file_hash_analysis(hashing_alogrithm string, skip int, batch int) {
directory_name, directory_selection_error := dialog.Directory().
Title("Select log file storage location").Browse()
if directory_selection_error != nil {
fmt.Println(directory_selection_error)
}
internal.Get_file_hashes_for_folder(directory_name, hashing_alogrithm, skip, batch)
}
func Start_file_copy(delete_source_flag string) {
move_file_list_filename, move_file_selection_err :=
dialog.File().Filter("Select mapping file", "csv").Load()
if move_file_selection_err == nil {
_, copy_err := internal.Copy_files(
move_file_list_filename, delete_source_flag)
if copy_err != nil {
fmt.Println(copy_err)
}
} else {
fmt.Println(
move_file_selection_err)
}
}
func Analyse_source_folder() {
directory_name, directory_selection_error := dialog.Directory().Title("Select location to analyse").Browse()
if directory_selection_error != nil {
fmt.Println(directory_selection_error)
}
files, file_information, _ := internal.Find_all_directory_content_recursive(directory_name)
number_of_files := len(files)
var total_size int64
var largest_file_size int64
var largest_file_name string
var longest_path_length int
var longest_file_name string
longest_path_length = 0
total_size = 0
for _, file := range file_information {
total_size += file.Size()
if file.Size() > largest_file_size {
largest_file_size = file.Size()
largest_file_name = file.Name()
}
}
log_file := internal.Set_log_file()
for _, file := range files {
file_length := len(file)
if file_length > longest_path_length {
longest_path_length = file_length
longest_file_name = file
}
}
log.Printf("Source directory Name: %v\r\n", directory_name)
log.Printf("Log file path: %v\r\n", log_file.Name())
log.Printf("Total Number of files: %v\r\n", number_of_files)
log.Printf("Total size: %s\r", datasize.ByteSize(total_size).HumanReadable())
log.Printf("Largest file name: %v\r\n", largest_file_name)
log.Printf("Largest file size: %s\r\n", datasize.ByteSize(largest_file_size).HumanReadable())
log.Printf("Longest file path length: %v\r\n", longest_path_length)
log.Printf("Longest file path name: %v\r\n", longest_file_name)
log_file.Close()
}