-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
92 lines (73 loc) · 2.37 KB
/
main.go
File metadata and controls
92 lines (73 loc) · 2.37 KB
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
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
)
func main() {
extFile := flag.String("ignore-file", "", "comma seperated list of file extension to ignore")
extFolder := flag.String("ignore-dir", "", "comma seperated list of directories names to ignore")
configPath := flag.String("config", DefaultCfgPath, "Path to config file")
forceCreateCfg := flag.Bool("force-config", false, "Force-create config file with default values")
outputFile := flag.String("output", "", "Output file (overrides config)")
skipWarn := flag.Bool("no-warn", false, "Skip warning for large dumps. Use with extreme caution!")
Version := flag.Bool("version", false, "print version and exit")
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s <directory> [flags]\n\n", os.Args[0])
fmt.Fprintf(flag.CommandLine.Output(), "Flags:\n")
flag.PrintDefaults()
}
flag.Parse()
if *Version {
fmt.Println(VersionString())
os.Exit(0)
}
if flag.NArg() < 1 {
flag.Usage()
os.Exit(1)
}
root := flag.Arg(0)
if err := LoadOrCreateConfig(*configPath, *forceCreateCfg); err != nil {
log.Fatalf("Failed to load config: %v", err)
}
if Cfg == nil {
log.Fatalf("Config is nil after loading")
}
if *extFile == "" {
extFileSlice := strings.Split(*extFile, ",")
Cfg.ExcludeFileExts = append(Cfg.ExcludeFileExts, extFileSlice...)
}
if *extFolder == "" {
extFolderSlice := strings.Split(*extFolder, ",")
Cfg.ExcludeDirs = append(Cfg.ExcludeDirs, extFolderSlice...)
}
files, err := CollectFiles(root, Cfg)
if err != nil {
log.Fatalf("Failed collecting files: %v", err)
}
// safety: warn for huge directories
const warnThreshold = 5000
if len(files) > warnThreshold && !*skipWarn {
fmt.Printf("[CRITICAL] This will dump %d files. This could result in self DoS!\n", len(files))
if !askConfirm("Do you want to continue?") {
fmt.Println("Aborted.")
os.Exit(1)
}
}
if *outputFile == "" {
*outputFile = Cfg.OutputFile
}
if err := DumpFiles(files, *outputFile); err != nil {
log.Fatalf("Failed dumping files: %v", err)
}
fmt.Printf("Dump created for directory %s: %s (%d files)\n", root, *outputFile, len(files))
}
func askConfirm(prompt string) bool {
fmt.Print(prompt + " [y/N]: ")
var resp string
_, _ = fmt.Scanln(&resp) // ignore error; empty input = NO
resp = strings.ToLower(strings.TrimSpace(resp))
return resp == "y" || resp == "yes"
}