-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassets.go
163 lines (133 loc) · 3.87 KB
/
assets.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
package assets
import (
"fmt"
"log"
"net/http"
"strings"
"time"
mfs "github.com/ZoltanLajosKis/go-mapfs"
"github.com/shurcooL/vfsgen"
"golang.org/x/tools/godoc/vfs/httpfs"
)
// Source describes an asset source to be retrieved and processed.
type Source struct {
Path string
Location string
Checksum *Checksum
Archive *Archive
}
// Opts provides optional parameters to the Compile function.
type Opts struct {
// BuildTags are the build tags in the generated source code.
// Defaults to no tags.
BuildTags string
// VariableComment is the comment of the variable in the generated source code.
// Defaults to "<VariableName> implements a http.FileSystem.".
VariableComment string
}
type file struct {
path string
data []byte
modTime time.Time
}
// Retrieve retrieves and processes the specified asset sources, and returns
// them using a http.FileSystem interface.
func Retrieve(sources []*Source) (http.FileSystem, error) {
files := make(mfs.Files)
for i, source := range sources {
log.Printf("Processing asset source (%d/%d): %s ...", i+1, len(sources), source.Location)
// Retrieve the file or files
retFiles, err := retrieve(source.Location)
if err != nil {
return nil, &RetrieveError{source.Location, err}
}
// If multiple files are returned store them and finish processing.
// Chekcsum and archive not supported for multiple files.
if len(retFiles) > 1 {
for _, file := range retFiles {
path := strings.TrimSuffix(source.Path, "/") + "/" + file.path
log.Printf("Created asset: %s ...", path)
files[path] = &mfs.File{file.data, file.modTime}
}
continue
}
// Process the single returned file
file := retFiles[0]
// Verify the file checksum if requested
if source.Checksum != nil {
err = verifyChecksum(source.Checksum, file.data)
if err != nil {
return nil, &ChecksumError{source.Location, err}
}
}
// If the file is not an archive store it and finish processing.
if source.Archive == nil {
log.Printf("Created asset: %s ...", source.Path)
files[source.Path] = &mfs.File{file.data, file.modTime}
continue
}
// Extract files from the archive and store them.
archFiles, err := processArchive(source.Archive, file.data)
if err != nil {
return nil, &ArchiveError{source.Location, err}
}
for _, file := range archFiles {
log.Printf("Created asset: %s ...", file.path)
files[file.path] = &mfs.File{file.data, file.modTime}
}
}
fs, err := mfs.New(files)
if err != nil {
return nil, err
}
return httpfs.New(fs), nil
}
// Compile retrieves and processes the specified asset sources, and
// compiles them to the specified variable in the source file.
func Compile(sources []*Source, filePath string, pkgName string, varName string, opts *Opts) error {
fs, err := Retrieve(sources)
if err != nil {
return err
}
if opts == nil {
opts = &Opts{}
}
if opts.VariableComment == "" {
opts.VariableComment = fmt.Sprintf("%s implements a http.FileSystem.", varName)
}
err = vfsgen.Generate(fs, vfsgen.Options{
Filename: filePath,
PackageName: pkgName,
BuildTags: opts.BuildTags,
VariableName: varName,
VariableComment: opts.VariableComment,
})
if err != nil {
return err
}
return nil
}
// RetrieveError is returned when there is a problem retrieving an asset source
type RetrieveError struct {
Location string
Err error
}
func (e *RetrieveError) Error() string {
return e.Location + ": " + e.Err.Error()
}
// ChecksumError is returned when there is a checksum problem with an asset source
type ChecksumError struct {
Location string
Err error
}
func (e *ChecksumError) Error() string {
return e.Location + ": " + e.Err.Error()
}
// ArchiveError is returned when there is a problem processing the archive
type ArchiveError struct {
Path string
Err error
}
func (e *ArchiveError) Error() string {
return e.Path + ": " + e.Err.Error()
}