Skip to content

Commit 9e447e0

Browse files
committed
refactor
1 parent 94fde46 commit 9e447e0

29 files changed

+512
-945
lines changed

cmd/embedded.go

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright 2020 The Gitea Authors. All rights reserved.
22
// SPDX-License-Identifier: MIT
33

4-
//go:build bindata
5-
64
package cmd
75

86
import (
@@ -13,6 +11,7 @@ import (
1311
"sort"
1412
"strings"
1513

14+
"code.gitea.io/gitea/modules/assetfs"
1615
"code.gitea.io/gitea/modules/log"
1716
"code.gitea.io/gitea/modules/options"
1817
"code.gitea.io/gitea/modules/public"
@@ -89,15 +88,12 @@ var (
8988
},
9089
}
9190

92-
sections map[string]*section
93-
assets []asset
91+
assets []asset
9492
)
9593

9694
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
10197
}
10298

10399
type asset struct {
@@ -121,9 +117,9 @@ func initEmbeddedExtractor(c *cli.Context) error {
121117
}
122118
sections := make(map[string]*section, 3)
123119

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())}
127123

128124
for _, sec := range sections {
129125
assets = append(assets, buildAssetList(sec, pats, c)...)
@@ -178,13 +174,7 @@ func runViewDo(c *cli.Context) error {
178174
return err
179175
}
180176

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)
188178
if err != nil {
189179
return fmt.Errorf("%s: %w", assets[0].Path, err)
190180
}
@@ -227,7 +217,7 @@ func runExtractDo(c *cli.Context) error {
227217
if err != nil {
228218
return fmt.Errorf("%s: %s", destdir, err)
229219
} 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)
231221
}
232222

233223
fmt.Printf("Extracting to %s:\n", destdir)
@@ -249,7 +239,7 @@ func extractAsset(d string, a asset, overwrite, rename bool) error {
249239
dest := filepath.Join(d, filepath.FromSlash(a.Path))
250240
dir := filepath.Dir(dest)
251241

252-
data, err := a.Section.Asset(a.Name)
242+
data, err := a.Section.AssetFS.ReadFile(a.Name)
253243
if err != nil {
254244
return fmt.Errorf("%s: %w", a.Path, err)
255245
}
@@ -295,23 +285,26 @@ func extractAsset(d string, a asset, overwrite, rename bool) error {
295285

296286
func buildAssetList(sec *section, globs []glob.Glob, c *cli.Context) []asset {
297287
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
315308
}
316309
}
317310
}
@@ -326,7 +319,7 @@ func getPatterns(args []string) ([]glob.Glob, error) {
326319
for i := range args {
327320
if g, err := glob.Compile(args[i], '/'); err != nil {
328321
return nil, fmt.Errorf("'%s': Invalid glob pattern: %w", args[i], err)
329-
} else {
322+
} else { //nolint:revive
330323
pat[i] = g
331324
}
332325
}

cmd/embedded_stub.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)