-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (47 loc) · 1.58 KB
/
main.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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2025 Qirashi
// Project: packed_webp
package main
import (
"os"
"path/filepath"
"strings"
)
func main() {
if len(os.Args) < 2 {
os.Stderr.WriteString("Usage: packed_webp <files...>\n")
os.Exit(1)
}
for _, path := range os.Args[1:] {
if _, err := os.Stat(path); err != nil {
os.Stderr.WriteString("Error accessing path: " + path + "\n")
continue
}
processFile(path)
}
}
func processFile(filePath string) {
baseName := filepath.Base(filePath)
ext := filepath.Ext(filePath)
switch {
case strings.HasSuffix(baseName, ".packed.webp"):
if err := UnpackWebp(filePath); err != nil {
os.Stderr.WriteString("Unpack error [" + filePath + "]: " + err.Error() + "\n")
} else {
newName := strings.TrimSuffix(filePath, ".packed.webp") + ".webp"
os.Stderr.WriteString("Successfully unpacked: " + filePath + " -> " + newName + "\n")
}
case (ext == ".txt" || ext == ".webp") && !strings.Contains(baseName, ".packed"):
if ext == ".txt" {
filePath = strings.TrimSuffix(filePath, ".txt") + ".webp"
}
if err := PackWebp(filePath); err != nil {
os.Stderr.WriteString("Pack error [" + filePath + "]: " + err.Error() + "\n")
} else {
packedName := strings.TrimSuffix(filePath, ".webp") + ".packed.webp"
os.Stderr.WriteString("Successfully packed: " + filePath + " -> " + packedName + "\n")
}
default:
os.Stderr.WriteString("Skipping unsupported file: " + filePath + "\n")
}
}