Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a general approach to access custom/static/builtin assets #24022

Merged
merged 22 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 33 additions & 34 deletions cmd/embedded.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

//go:build bindata

package cmd

import (
Expand All @@ -13,6 +11,7 @@ import (
"sort"
"strings"

"code.gitea.io/gitea/modules/assetfs"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/options"
"code.gitea.io/gitea/modules/public"
Expand Down Expand Up @@ -89,15 +88,12 @@ var (
},
}

sections map[string]*section
assets []asset
assets []asset
)

type section struct {
lunny marked this conversation as resolved.
Show resolved Hide resolved
Path string
Names func() []string
IsDir func(string) (bool, error)
Asset func(string) ([]byte, error)
Path string
AssetFS *assetfs.LayeredFS
}

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

sections["public"] = &section{Path: "public", Names: public.AssetNames, IsDir: public.AssetIsDir, Asset: public.Asset}
sections["options"] = &section{Path: "options", Names: options.AssetNames, IsDir: options.AssetIsDir, Asset: options.Asset}
sections["templates"] = &section{Path: "templates", Names: templates.BuiltinAssetNames, IsDir: templates.BuiltinAssetIsDir, Asset: templates.BuiltinAsset}
sections["public"] = &section{Path: "public", AssetFS: assetfs.Layered(public.BuiltinAssets())}
sections["options"] = &section{Path: "options", AssetFS: assetfs.Layered(options.BuiltinAssets())}
sections["templates"] = &section{Path: "templates", AssetFS: assetfs.Layered(templates.BuiltinAssets())}

for _, sec := range sections {
assets = append(assets, buildAssetList(sec, pats, c)...)
Expand Down Expand Up @@ -179,12 +175,12 @@ func runViewDo(c *cli.Context) error {
}

if len(assets) == 0 {
return fmt.Errorf("No files matched the given pattern")
return fmt.Errorf("no files matched the given pattern")
} else if len(assets) > 1 {
return fmt.Errorf("Too many files matched the given pattern; try to be more specific")
return fmt.Errorf("too many files matched the given pattern, try to be more specific")
}

data, err := assets[0].Section.Asset(assets[0].Name)
data, err := assets[0].Section.AssetFS.ReadFile(assets[0].Name)
if err != nil {
return fmt.Errorf("%s: %w", assets[0].Path, err)
}
Expand Down Expand Up @@ -227,7 +223,7 @@ func runExtractDo(c *cli.Context) error {
if err != nil {
return fmt.Errorf("%s: %s", destdir, err)
} else if !fi.IsDir() {
return fmt.Errorf("%s is not a directory.", destdir)
return fmt.Errorf("destination %q is not a directory", destdir)
}

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

data, err := a.Section.Asset(a.Name)
data, err := a.Section.AssetFS.ReadFile(a.Name)
if err != nil {
return fmt.Errorf("%s: %w", a.Path, err)
}
Expand Down Expand Up @@ -295,23 +291,26 @@ func extractAsset(d string, a asset, overwrite, rename bool) error {

func buildAssetList(sec *section, globs []glob.Glob, c *cli.Context) []asset {
results := make([]asset, 0, 64)
for _, name := range sec.Names() {
if isdir, err := sec.IsDir(name); !isdir && err == nil {
if sec.Path == "public" &&
strings.HasPrefix(name, "vendor/") &&
!c.Bool("include-vendored") {
continue
}
matchName := sec.Path + "/" + name
for _, g := range globs {
if g.Match(matchName) {
results = append(results, asset{
Section: sec,
Name: name,
Path: sec.Path + "/" + name,
})
break
}
files, err := sec.AssetFS.ListFiles(".", true)
if err != nil {
log.Error("Error listing files in %q: %v", sec.Path, err)
return nil
}
for _, name := range files {
if sec.Path == "public" &&
strings.HasPrefix(name, "vendor/") &&
!c.Bool("include-vendored") {
continue
}
matchName := sec.Path + "/" + name
for _, g := range globs {
if g.Match(matchName) {
results = append(results, asset{
Section: sec,
Name: name,
Path: sec.Path + "/" + name,
})
break
}
}
}
Expand All @@ -326,7 +325,7 @@ func getPatterns(args []string) ([]glob.Glob, error) {
for i := range args {
if g, err := glob.Compile(args[i], '/'); err != nil {
return nil, fmt.Errorf("'%s': Invalid glob pattern: %w", args[i], err)
} else {
} else { //nolint:revive
pat[i] = g
}
}
Expand Down
29 changes: 0 additions & 29 deletions cmd/embedded_stub.go

This file was deleted.

Loading