-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
66 lines (58 loc) · 1.46 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
package main
import (
"archive/zip"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
)
func parseFlags() string {
filePath := flag.String("f", "", "Path to the file")
flag.Parse()
return *filePath
}
func validateFilePath(filePath string) {
if filePath == "" {
fmt.Println("You must specify a file path using the -f flag.")
os.Exit(1)
}
}
func openZipFile(filePath string) *zip.ReadCloser {
r, err := zip.OpenReader(filePath)
if err != nil {
panic(err)
}
return r
}
func getWorkingDirectory() string {
workDir, err := os.Getwd()
if err != nil {
panic(err)
}
return workDir
}
func extractFileName(filePath string) string {
fileNameWithExtension := filepath.Base(filePath)
return strings.TrimSuffix(fileNameWithExtension, filepath.Ext(fileNameWithExtension))
}
func processZipFiles(r *zip.ReadCloser, baseDir, workDir, fileName string) {
for _, f := range r.File {
switch {
case strings.HasSuffix(strings.ToLower(f.Name), ".xml"):
handleXML(f, baseDir)
case strings.EqualFold(f.Name, "config.lua"):
handleConfig(f, filepath.Join(workDir, fileName))
default:
handleResources(f, filepath.Join(baseDir, "resources"))
}
}
}
func containsIllegalCharacters(filename string) bool {
illegalCharacters := []rune{'<', '>', ':', '"', '/', '\\', '|', '?', '*'}
result := strings.ContainsAny(filename, string(illegalCharacters))
if result {
fmt.Printf("warn: file %s contains illegal characters, consider changing its name.\n", filename)
}
return result
}