1
1
// Copyright 2020 The Gitea Authors. All rights reserved.
2
2
// SPDX-License-Identifier: MIT
3
3
4
- //go:build bindata
5
-
6
4
package cmd
7
5
8
6
import (
@@ -13,6 +11,7 @@ import (
13
11
"sort"
14
12
"strings"
15
13
14
+ "code.gitea.io/gitea/modules/assetfs"
16
15
"code.gitea.io/gitea/modules/log"
17
16
"code.gitea.io/gitea/modules/options"
18
17
"code.gitea.io/gitea/modules/public"
@@ -89,15 +88,12 @@ var (
89
88
},
90
89
}
91
90
92
- sections map [string ]* section
93
- assets []asset
91
+ assets []asset
94
92
)
95
93
96
94
type section struct {
97
- Path string
98
- Names func () []string
99
- IsDir func (string ) (bool , error )
100
- Asset func (string ) ([]byte , error )
95
+ Path string
96
+ AssetFS * assetfs.LayeredFS
101
97
}
102
98
103
99
type asset struct {
@@ -121,9 +117,9 @@ func initEmbeddedExtractor(c *cli.Context) error {
121
117
}
122
118
sections := make (map [string ]* section , 3 )
123
119
124
- sections ["public" ] = & section {Path : "public" , Names : public . AssetNames , IsDir : public .AssetIsDir , Asset : public . Asset }
125
- sections ["options" ] = & section {Path : "options" , Names : options . AssetNames , IsDir : options .AssetIsDir , Asset : options . Asset }
126
- sections ["templates" ] = & section {Path : "templates" , Names : templates . BuiltinAssetNames , IsDir : templates .BuiltinAssetIsDir , Asset : templates . BuiltinAsset }
120
+ sections ["public" ] = & section {Path : "public" , AssetFS : assetfs . Layered ( public .BuiltinAssets ()) }
121
+ sections ["options" ] = & section {Path : "options" , AssetFS : assetfs . Layered ( options .BuiltinAssets ()) }
122
+ sections ["templates" ] = & section {Path : "templates" , AssetFS : assetfs . Layered ( templates .BuiltinAssets ()) }
127
123
128
124
for _ , sec := range sections {
129
125
assets = append (assets , buildAssetList (sec , pats , c )... )
@@ -178,13 +174,7 @@ func runViewDo(c *cli.Context) error {
178
174
return err
179
175
}
180
176
181
- if len (assets ) == 0 {
182
- return fmt .Errorf ("No files matched the given pattern" )
183
- } else if len (assets ) > 1 {
184
- return fmt .Errorf ("Too many files matched the given pattern; try to be more specific" )
185
- }
186
-
187
- data , err := assets [0 ].Section .Asset (assets [0 ].Name )
177
+ data , err := assets [0 ].Section .AssetFS .ReadFile (assets [0 ].Name )
188
178
if err != nil {
189
179
return fmt .Errorf ("%s: %w" , assets [0 ].Path , err )
190
180
}
@@ -227,7 +217,7 @@ func runExtractDo(c *cli.Context) error {
227
217
if err != nil {
228
218
return fmt .Errorf ("%s: %s" , destdir , err )
229
219
} else if ! fi .IsDir () {
230
- return fmt .Errorf ("%s is not a directory. " , destdir )
220
+ return fmt .Errorf ("destination %q is not a directory" , destdir )
231
221
}
232
222
233
223
fmt .Printf ("Extracting to %s:\n " , destdir )
@@ -249,7 +239,7 @@ func extractAsset(d string, a asset, overwrite, rename bool) error {
249
239
dest := filepath .Join (d , filepath .FromSlash (a .Path ))
250
240
dir := filepath .Dir (dest )
251
241
252
- data , err := a .Section .Asset (a .Name )
242
+ data , err := a .Section .AssetFS . ReadFile (a .Name )
253
243
if err != nil {
254
244
return fmt .Errorf ("%s: %w" , a .Path , err )
255
245
}
@@ -295,23 +285,26 @@ func extractAsset(d string, a asset, overwrite, rename bool) error {
295
285
296
286
func buildAssetList (sec * section , globs []glob.Glob , c * cli.Context ) []asset {
297
287
results := make ([]asset , 0 , 64 )
298
- for _ , name := range sec .Names () {
299
- if isdir , err := sec .IsDir (name ); ! isdir && err == nil {
300
- if sec .Path == "public" &&
301
- strings .HasPrefix (name , "vendor/" ) &&
302
- ! c .Bool ("include-vendored" ) {
303
- continue
304
- }
305
- matchName := sec .Path + "/" + name
306
- for _ , g := range globs {
307
- if g .Match (matchName ) {
308
- results = append (results , asset {
309
- Section : sec ,
310
- Name : name ,
311
- Path : sec .Path + "/" + name ,
312
- })
313
- break
314
- }
288
+ files , err := sec .AssetFS .ListFiles ("." , true )
289
+ if err != nil {
290
+ log .Error ("Error listing files in %q: %v" , sec .Path , err )
291
+ return results
292
+ }
293
+ for _ , name := range files {
294
+ if sec .Path == "public" &&
295
+ strings .HasPrefix (name , "vendor/" ) &&
296
+ ! c .Bool ("include-vendored" ) {
297
+ continue
298
+ }
299
+ matchName := sec .Path + "/" + name
300
+ for _ , g := range globs {
301
+ if g .Match (matchName ) {
302
+ results = append (results , asset {
303
+ Section : sec ,
304
+ Name : name ,
305
+ Path : sec .Path + "/" + name ,
306
+ })
307
+ break
315
308
}
316
309
}
317
310
}
@@ -326,7 +319,7 @@ func getPatterns(args []string) ([]glob.Glob, error) {
326
319
for i := range args {
327
320
if g , err := glob .Compile (args [i ], '/' ); err != nil {
328
321
return nil , fmt .Errorf ("'%s': Invalid glob pattern: %w" , args [i ], err )
329
- } else {
322
+ } else { //nolint:revive
330
323
pat [i ] = g
331
324
}
332
325
}
0 commit comments