-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.go
More file actions
81 lines (69 loc) · 1.47 KB
/
convert.go
File metadata and controls
81 lines (69 loc) · 1.47 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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
// IncFile is a file to include-
type IncFile struct {
Name string
Path string
Data []byte
}
// Convert binary data to an embeddable variable.
func Convert(content []byte, name string) ([]byte, error) {
var b bytes.Buffer
c := fmt.Sprintf("var %s = []byte(\"", name)
b.WriteString(c)
for _, in := range content {
c = fmt.Sprintf("\\x%02x", in)
b.WriteString(c)
}
b.WriteString("\")\n\n")
return b.Bytes(), nil
}
// ConvertFiles takes a list of files and runs Convert() on each file.
func ConvertFiles(filelist []string) ([]*IncFile, error) {
var list []*IncFile
for _, path := range filelist {
fi, err := os.Stat(path)
if err != nil {
return nil, err
}
if fi.IsDir() {
filist, err := ioutil.ReadDir(path)
if err == nil {
l := []string{}
for _, x := range filist {
fn := filepath.Join(path, x.Name())
l = append(l, fn)
}
convlist, err := ConvertFiles(l)
if err != nil {
pr("Couldn't convert files in directory '%s': %s", path, err.Error())
continue
}
list = append(list, convlist...)
}
continue
}
name := mangleName(path)
in, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
gz, err := Compress(in)
if err != nil {
return nil, err
}
out, err := Convert(gz, name)
if err != nil {
return nil, err
}
file := IncFile{path, name, out}
list = append(list, &file)
}
return list, nil
}