-
Notifications
You must be signed in to change notification settings - Fork 2
/
plaintextmaxsize.go
189 lines (160 loc) · 3.98 KB
/
plaintextmaxsize.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package goef
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"text/template"
)
const datafile = `package {{.PkgName}}
// DO NOT EDIT. This file is auto-created by github.com/siongui/goef
var vfs{{.N}} = map[string]string{
{{ range .Files }}"{{ .Name }}": ` + "`" + `{{ .PlainTextContent }}` + "`" + `,
{{ end }}}
`
const readfile = `package %s
// DO NOT EDIT. This file is auto-created by github.com/siongui/goef
import (
"os"
)
func ReadFile(filename string) ([]byte, error) {
%s
return nil, os.ErrNotExist
}
func MapKeys() []string {
length := 0
%s
keys := make([]string, length)
i := 0
%s
return keys
}
`
const readcode = `
content%d, ok%d := vfs%d[filename]
if ok%d {
return []byte(content%d), nil
}
`
const lengthcode = `
length += len(vfs%d)
`
const appendcode = `
for k := range vfs%d {
keys[i] = k
i++
}
`
var minSize = int64(len([]byte(datafile)))
type pkgData3 struct {
PkgName string
N int64
Files []pkgFile2
}
func createReadFile(pkgname, outputdir string, totalNumberOfDataFiles int64) (err error) {
rd := ""
ln := ""
ap := ""
for i := int64(0); i < totalNumberOfDataFiles; i++ {
rd += fmt.Sprintf(readcode, i, i, i, i, i)
ln += fmt.Sprintf(lengthcode, i)
ap += fmt.Sprintf(appendcode, i)
}
fc := fmt.Sprintf(readfile, pkgname, rd, ln, ap)
outputpath := path.Join(outputdir, "read.go")
err = ioutil.WriteFile(outputpath, []byte(fc), 0644)
return
}
func createDataFile(pd pkgData3, outputdir string) (err error) {
filename := fmt.Sprintf("data%d.go", pd.N)
outputpath := path.Join(outputdir, filename)
fo, err := os.Create(outputpath)
if err != nil {
return
}
defer fo.Close()
tmpl, err := template.New("goembed").Parse(datafile)
if err != nil {
return
}
err = tmpl.Execute(fo, pd)
return
}
// GenerateGoPackagePlainTextWithMaxFileSize is the same as
// GenerateGoPackagePlainText method, except the output file size cannot be over
// the given max limit. This is useful for deploy your code on cloud services
// such as Google App Engine because they usually limit the max size of a single
// file.
func GenerateGoPackagePlainTextWithMaxFileSize(pkgname, dirpath, outputdir string, maxSize int64) (err error) {
if maxSize < minSize {
return fmt.Errorf("maxSize cannot be less than %d", minSize)
}
// check if outputdir exists
info, err := os.Stat(outputdir)
if err != nil {
if os.IsNotExist(err) {
// outputdir does not exist, create it.
err = os.MkdirAll(outputdir, 0755)
if err != nil {
return
}
} else {
return
}
} else {
// check if outputdir is really a dir
if !info.IsDir() {
return errors.New("argument *outputdir* is not a directory!")
}
}
pd := pkgData3{PkgName: pkgname, N: 0}
currentDataFileSize := minSize
err = filepath.Walk(dirpath, func(filepath string, info os.FileInfo, e error) error {
if e != nil {
return e
}
if info.Mode().IsRegular() {
name, content, errf := getFilenameAndPlainTextContent(dirpath, filepath, info)
if errf != nil {
return errf
}
// 10 includes rough estimate for "`... in template rendering
increasedSize := int64(len([]byte(name+content))) + 10
if increasedSize > (maxSize - minSize) {
return fmt.Errorf("file %s is too big (Please increase maxSize %d)", name, maxSize)
}
currentDataFileSize += increasedSize
if currentDataFileSize > maxSize {
if len(pd.Files) == 0 {
return errors.New("Please increase maxSize value")
}
if err3 := createDataFile(pd, outputdir); err3 != nil {
return err3
}
pd = pkgData3{PkgName: pkgname, N: pd.N + 1}
currentDataFileSize = minSize + increasedSize
}
pd.Files = append(pd.Files,
pkgFile2{
Name: name,
PlainTextContent: content,
})
}
return nil
})
if err != nil {
return
}
totalNumberOfDataFiles := pd.N
if len(pd.Files) > 0 {
err = createDataFile(pd, outputdir)
if err != nil {
return err
}
totalNumberOfDataFiles += 1
}
err = createReadFile(pkgname, outputdir, totalNumberOfDataFiles)
return
}