Skip to content

Commit 375d14c

Browse files
committed
Fix debug file loader
1 parent a20b750 commit 375d14c

File tree

1 file changed

+63
-65
lines changed

1 file changed

+63
-65
lines changed

templ.go

Lines changed: 63 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,77 +8,13 @@ package vault
88
const sharedTypesTempl = `
99
import (
1010
"net/http"
11-
"sort"
12-
"strings"
13-
"os"
14-
"fmt"
1511
)
1612
1713
// AssetLoader implements a function to load an asset from the vault
1814
type AssetLoader interface {
1915
// Open loads a file from the vault.
2016
Open(name string) (http.File, error)
2117
}
22-
23-
// assetMap holds all information about the embedded files
24-
type assetMap map[string]memFile
25-
26-
// createDirFile creates a http.File for the given path.
27-
func createDirFile(path string, assets assetMap) http.File {
28-
var fis []os.FileInfo
29-
processed := map[string]struct{}{}
30-
31-
for _, val := range assets {
32-
if val.path == path {
33-
fis = append(fis, val)
34-
continue
35-
}
36-
37-
if strings.HasPrefix(val.path, path) {
38-
dir := strings.TrimPrefix(val.path, path)
39-
dir = strings.TrimPrefix(dir, "/")
40-
if n := strings.Index(dir, "/"); n >= 0 {
41-
dir = dir[:n]
42-
}
43-
44-
if _, ok := processed[dir]; !ok {
45-
var prefix string
46-
if path == "/" {
47-
prefix = "/" + dir
48-
} else {
49-
prefix = fmt.Sprintf("%v/%v", path, dir)
50-
}
51-
52-
fis = append(fis, memDir{dir: dir, size: getSize(prefix, assets)})
53-
processed[dir] = struct{}{}
54-
}
55-
}
56-
}
57-
58-
sort.Slice(fis, func(i, j int) bool {
59-
switch {
60-
case fis[i].IsDir() && !fis[j].IsDir():
61-
return true
62-
case !fis[i].IsDir() && fis[j].IsDir():
63-
return false
64-
default:
65-
return fis[i].Name() < fis[j].Name()
66-
}
67-
})
68-
69-
return &memDir{dir: path, size: getSize(path, assets), files: fis}
70-
}
71-
72-
// getSize summarize all files under the given path.
73-
func getSize(path string, assets assetMap) int64 {
74-
var cnt int64
75-
for _, item := range assets {
76-
if strings.HasPrefix(item.path, path) {
77-
cnt += item.size
78-
}
79-
}
80-
return cnt
81-
}
8218
`
8319

8420
const releaseImportTempl = `
@@ -87,6 +23,8 @@ import (
8723
"errors"
8824
"io"
8925
"os"
26+
"fmt"
27+
"sort"
9028
"strings"
9129
"time"
9230
"net/http"
@@ -323,14 +261,74 @@ func New{{.Suffix}}Loader() AssetLoader {
323261
}
324262
return loader
325263
}
264+
265+
266+
// assetMap holds all information about the embedded files
267+
type assetMap map[string]memFile
268+
269+
// createDirFile creates a http.File for the given path.
270+
func createDirFile(path string, assets assetMap) http.File {
271+
var fis []os.FileInfo
272+
processed := map[string]struct{}{}
273+
274+
for _, val := range assets {
275+
if val.path == path {
276+
fis = append(fis, val)
277+
continue
278+
}
279+
280+
if strings.HasPrefix(val.path, path) {
281+
dir := strings.TrimPrefix(val.path, path)
282+
dir = strings.TrimPrefix(dir, "/")
283+
if n := strings.Index(dir, "/"); n >= 0 {
284+
dir = dir[:n]
285+
}
286+
287+
if _, ok := processed[dir]; !ok {
288+
var prefix string
289+
if path == "/" {
290+
prefix = "/" + dir
291+
} else {
292+
prefix = fmt.Sprintf("%v/%v", path, dir)
293+
}
294+
295+
fis = append(fis, memDir{dir: dir, size: getSize(prefix, assets)})
296+
processed[dir] = struct{}{}
297+
}
298+
}
299+
}
300+
301+
sort.Slice(fis, func(i, j int) bool {
302+
switch {
303+
case fis[i].IsDir() && !fis[j].IsDir():
304+
return true
305+
case !fis[i].IsDir() && fis[j].IsDir():
306+
return false
307+
default:
308+
return fis[i].Name() < fis[j].Name()
309+
}
310+
})
311+
312+
return &memDir{dir: path, size: getSize(path, assets), files: fis}
313+
}
314+
315+
// getSize summarize all files under the given path.
316+
func getSize(path string, assets assetMap) int64 {
317+
var cnt int64
318+
for _, item := range assets {
319+
if strings.HasPrefix(item.path, path) {
320+
cnt += item.size
321+
}
322+
}
323+
return cnt
324+
}
326325
`
327326

328327
const debugFileTemp = `
329328
import (
330329
"fmt"
331330
"os"
332331
"path"
333-
"strings"
334332
"net/http"
335333
)
336334

0 commit comments

Comments
 (0)