Skip to content
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 {
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