forked from getlantern/systray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
141 lines (123 loc) · 2.78 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
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
package main
import (
"os"
"fmt"
"io"
"flag"
"io/ioutil"
"path/filepath"
"bufio"
)
var (
flagPackage = flag.String("package", "main", "Package name where resource will be used")
flagDestination = flag.String("dest", "data.go", "Path to destination file")
)
func init() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\nOne non-flag argument: path to source file or directory. Also flags supported:\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
}
func convertToFile(srcPath string, destFile *os.File) error {
const BytesPerLine = 16
if _, err := fmt.Fprintf(destFile, "\"%s\": {\n\t", srcPath); err != nil {
return err
}
srcFile, err := os.Open(srcPath)
if err != nil {
return err
}
defer srcFile.Close()
br := bufio.NewReader(srcFile)
bytesOnLine := 0
for {
b, err := br.ReadByte()
if err != nil {
if err == io.EOF {
break
} else {
return err
}
}
if bytesOnLine == BytesPerLine {
bytesOnLine = 0
if _, err = fmt.Fprint(destFile, "\n\t"); err != nil {
return err
}
}
if _, err = fmt.Fprintf(destFile, "0x%02x,", b); err != nil {
return err
}
bytesOnLine++
}
if _, err = fmt.Fprint(destFile, "\n},\n"); err != nil {
return err
}
return nil
}
func main() {
packageName := *flagPackage
destPath := *flagDestination
fmt.Printf("Package name: %s\n", packageName)
fmt.Printf("Destination: %s\n", destPath)
srcPath := flag.Arg(0)
if srcPath == "" {
fmt.Println("Source path is not defined!")
os.Exit(1)
return
}
fmt.Printf("Source: %s\n", srcPath)
stat, err := os.Stat(srcPath)
if err != nil {
fmt.Printf("Source data is not available: %s\n", err)
os.Exit(1)
return
}
destFile, err := os.Create(destPath)
if err != nil {
fmt.Printf("Unable to create file: %s", err)
os.Exit(1)
return
}
defer destFile.Close()
_, err = fmt.Fprintf(destFile, "package %s\n\nvar filesByteData map[string][]byte = {\n", packageName)
if err != nil {
fmt.Printf("Unable to write to file: %s", err)
os.Exit(1)
return
}
if stat.IsDir() {
fmt.Println("Source is directory. Walk over...")
files, err := ioutil.ReadDir(srcPath)
if err != nil {
fmt.Printf("Can't list directory: %s", err)
os.Exit(1)
return
}
for _, file := range files {
if file.IsDir() {
continue
}
err = convertToFile(filepath.Join(srcPath, file.Name()), destFile)
if err != nil {
fmt.Printf("Can't convert file %s: %s", file.Name(), err)
os.Exit(1)
return
}
}
} else {
err = convertToFile(srcPath, destFile)
if err != nil {
fmt.Printf("Can't convert file %s: %s", srcPath, err)
os.Exit(1)
return
}
}
if _, err = fmt.Fprint(destFile, "\n}\n"); err != nil {
fmt.Printf("Can't convert file %s: %s", srcPath, err)
os.Exit(1)
return
}
fmt.Println("Success!")
}