2020-02-02 02:17:44 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-02-02 02:17:44 +00:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2023-04-12 10:16:45 +00:00
|
|
|
"code.gitea.io/gitea/modules/assetfs"
|
2020-02-02 02:17:44 +00:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/options"
|
|
|
|
"code.gitea.io/gitea/modules/public"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/templates"
|
2021-07-15 15:46:07 +00:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2020-02-02 02:17:44 +00:00
|
|
|
|
|
|
|
"github.com/gobwas/glob"
|
2023-07-21 09:28:19 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-02-02 02:17:44 +00:00
|
|
|
)
|
|
|
|
|
2023-06-28 21:30:06 +00:00
|
|
|
// CmdEmbedded represents the available extract sub-command.
|
2020-02-02 02:17:44 +00:00
|
|
|
var (
|
2023-07-21 09:28:19 +00:00
|
|
|
CmdEmbedded = &cli.Command{
|
2020-02-02 02:17:44 +00:00
|
|
|
Name: "embedded",
|
|
|
|
Usage: "Extract embedded resources",
|
|
|
|
Description: "A command for extracting embedded resources, like templates and images",
|
2023-07-21 09:28:19 +00:00
|
|
|
Subcommands: []*cli.Command{
|
2020-02-02 02:17:44 +00:00
|
|
|
subcmdList,
|
|
|
|
subcmdView,
|
|
|
|
subcmdExtract,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-21 09:28:19 +00:00
|
|
|
subcmdList = &cli.Command{
|
2020-02-02 02:17:44 +00:00
|
|
|
Name: "list",
|
|
|
|
Usage: "List files matching the given pattern",
|
|
|
|
Action: runList,
|
|
|
|
Flags: []cli.Flag{
|
2023-07-21 09:28:19 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "include-vendored",
|
|
|
|
Aliases: []string{"vendor"},
|
|
|
|
Usage: "Include files under public/vendor as well",
|
2020-02-02 02:17:44 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-21 09:28:19 +00:00
|
|
|
subcmdView = &cli.Command{
|
2020-02-02 02:17:44 +00:00
|
|
|
Name: "view",
|
|
|
|
Usage: "View a file matching the given pattern",
|
|
|
|
Action: runView,
|
|
|
|
Flags: []cli.Flag{
|
2023-07-21 09:28:19 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "include-vendored",
|
|
|
|
Aliases: []string{"vendor"},
|
|
|
|
Usage: "Include files under public/vendor as well",
|
2020-02-02 02:17:44 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-21 09:28:19 +00:00
|
|
|
subcmdExtract = &cli.Command{
|
2020-02-02 02:17:44 +00:00
|
|
|
Name: "extract",
|
|
|
|
Usage: "Extract resources",
|
|
|
|
Action: runExtract,
|
|
|
|
Flags: []cli.Flag{
|
2023-07-21 09:28:19 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "include-vendored",
|
|
|
|
Aliases: []string{"vendor"},
|
|
|
|
Usage: "Include files under public/vendor as well",
|
2020-02-02 02:17:44 +00:00
|
|
|
},
|
2023-07-21 09:28:19 +00:00
|
|
|
&cli.BoolFlag{
|
2020-02-02 02:17:44 +00:00
|
|
|
Name: "overwrite",
|
|
|
|
Usage: "Overwrite files if they already exist",
|
|
|
|
},
|
2023-07-21 09:28:19 +00:00
|
|
|
&cli.BoolFlag{
|
2020-02-02 02:17:44 +00:00
|
|
|
Name: "rename",
|
|
|
|
Usage: "Rename files as {name}.bak if they already exist (overwrites previous .bak)",
|
|
|
|
},
|
2023-07-21 09:28:19 +00:00
|
|
|
&cli.BoolFlag{
|
2020-02-02 02:17:44 +00:00
|
|
|
Name: "custom",
|
|
|
|
Usage: "Extract to the 'custom' directory as per app.ini",
|
|
|
|
},
|
2023-07-21 09:28:19 +00:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "destination",
|
|
|
|
Aliases: []string{"dest-dir"},
|
|
|
|
Usage: "Extract to the specified directory",
|
2020-02-02 02:17:44 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-04-12 10:16:45 +00:00
|
|
|
matchedAssetFiles []assetFile
|
2020-02-02 02:17:44 +00:00
|
|
|
)
|
|
|
|
|
2023-04-12 10:16:45 +00:00
|
|
|
type assetFile struct {
|
|
|
|
fs *assetfs.LayeredFS
|
|
|
|
name string
|
|
|
|
path string
|
2020-02-02 02:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func initEmbeddedExtractor(c *cli.Context) error {
|
Rewrite logger system (#24726)
## ⚠️ Breaking
The `log.<mode>.<logger>` style config has been dropped. If you used it,
please check the new config manual & app.example.ini to make your
instance output logs as expected.
Although many legacy options still work, it's encouraged to upgrade to
the new options.
The SMTP logger is deleted because SMTP is not suitable to collect logs.
If you have manually configured Gitea log options, please confirm the
logger system works as expected after upgrading.
## Description
Close #12082 and maybe more log-related issues, resolve some related
FIXMEs in old code (which seems unfixable before)
Just like rewriting queue #24505 : make code maintainable, clear legacy
bugs, and add the ability to support more writers (eg: JSON, structured
log)
There is a new document (with examples): `logging-config.en-us.md`
This PR is safer than the queue rewriting, because it's just for
logging, it won't break other logic.
## The old problems
The logging system is quite old and difficult to maintain:
* Unclear concepts: Logger, NamedLogger, MultiChannelledLogger,
SubLogger, EventLogger, WriterLogger etc
* Some code is diffuclt to konw whether it is right:
`log.DelNamedLogger("console")` vs `log.DelNamedLogger(log.DEFAULT)` vs
`log.DelLogger("console")`
* The old system heavily depends on ini config system, it's difficult to
create new logger for different purpose, and it's very fragile.
* The "color" trick is difficult to use and read, many colors are
unnecessary, and in the future structured log could help
* It's difficult to add other log formats, eg: JSON format
* The log outputer doesn't have full control of its goroutine, it's
difficult to make outputer have advanced behaviors
* The logs could be lost in some cases: eg: no Fatal error when using
CLI.
* Config options are passed by JSON, which is quite fragile.
* INI package makes the KEY in `[log]` section visible in `[log.sub1]`
and `[log.sub1.subA]`, this behavior is quite fragile and would cause
more unclear problems, and there is no strong requirement to support
`log.<mode>.<logger>` syntax.
## The new design
See `logger.go` for documents.
## Screenshot
<details>
![image](https://github.com/go-gitea/gitea/assets/2114189/4462d713-ba39-41f5-bb08-de912e67e1ff)
![image](https://github.com/go-gitea/gitea/assets/2114189/b188035e-f691-428b-8b2d-ff7b2199b2f9)
![image](https://github.com/go-gitea/gitea/assets/2114189/132e9745-1c3b-4e00-9e0d-15eaea495dee)
</details>
## TODO
* [x] add some new tests
* [x] fix some tests
* [x] test some sub-commands (manually ....)
---------
Co-authored-by: Jason Song <i@wolfogre.com>
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Giteabot <teabot@gitea.io>
2023-05-21 22:35:11 +00:00
|
|
|
setupConsoleLogger(log.ERROR, log.CanColorStderr, os.Stderr)
|
2020-02-02 02:17:44 +00:00
|
|
|
|
2023-07-21 09:28:19 +00:00
|
|
|
patterns, err := compileCollectPatterns(c.Args().Slice())
|
2020-02-02 02:17:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-12 10:16:45 +00:00
|
|
|
collectAssetFilesByPattern(c, patterns, "options", options.BuiltinAssets())
|
|
|
|
collectAssetFilesByPattern(c, patterns, "public", public.BuiltinAssets())
|
|
|
|
collectAssetFilesByPattern(c, patterns, "templates", templates.BuiltinAssets())
|
2020-02-02 02:17:44 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runList(c *cli.Context) error {
|
|
|
|
if err := runListDo(c); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%v\n", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runView(c *cli.Context) error {
|
|
|
|
if err := runViewDo(c); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%v\n", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runExtract(c *cli.Context) error {
|
|
|
|
if err := runExtractDo(c); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%v\n", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runListDo(c *cli.Context) error {
|
|
|
|
if err := initEmbeddedExtractor(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-12 10:16:45 +00:00
|
|
|
for _, a := range matchedAssetFiles {
|
|
|
|
fmt.Println(a.path)
|
2020-02-02 02:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runViewDo(c *cli.Context) error {
|
|
|
|
if err := initEmbeddedExtractor(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-12 10:16:45 +00:00
|
|
|
if len(matchedAssetFiles) == 0 {
|
2024-04-21 19:44:03 +00:00
|
|
|
return errors.New("no files matched the given pattern")
|
2023-04-12 10:16:45 +00:00
|
|
|
} else if len(matchedAssetFiles) > 1 {
|
2024-04-21 19:44:03 +00:00
|
|
|
return errors.New("too many files matched the given pattern, try to be more specific")
|
2020-02-02 02:17:44 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 10:16:45 +00:00
|
|
|
data, err := matchedAssetFiles[0].fs.ReadFile(matchedAssetFiles[0].name)
|
2020-02-02 02:17:44 +00:00
|
|
|
if err != nil {
|
2023-04-12 10:16:45 +00:00
|
|
|
return fmt.Errorf("%s: %w", matchedAssetFiles[0].path, err)
|
2020-02-02 02:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = os.Stdout.Write(data); err != nil {
|
2023-04-12 10:16:45 +00:00
|
|
|
return fmt.Errorf("%s: %w", matchedAssetFiles[0].path, err)
|
2020-02-02 02:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runExtractDo(c *cli.Context) error {
|
|
|
|
if err := initEmbeddedExtractor(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-21 09:28:19 +00:00
|
|
|
if c.NArg() == 0 {
|
2024-04-21 19:44:03 +00:00
|
|
|
return errors.New("a list of pattern of files to extract is mandatory (e.g. '**' for all)")
|
2020-02-02 02:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
destdir := "."
|
|
|
|
|
|
|
|
if c.IsSet("destination") {
|
|
|
|
destdir = c.String("destination")
|
|
|
|
} else if c.Bool("custom") {
|
|
|
|
destdir = setting.CustomPath
|
|
|
|
fmt.Println("Using app.ini at", setting.CustomConf)
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := os.Stat(destdir)
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
// In case Windows users attempt to provide a forward-slash path
|
|
|
|
wdestdir := filepath.FromSlash(destdir)
|
|
|
|
if wfi, werr := os.Stat(wdestdir); werr == nil {
|
|
|
|
destdir = wdestdir
|
|
|
|
fi = wfi
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s: %s", destdir, err)
|
|
|
|
} else if !fi.IsDir() {
|
2023-04-12 10:16:45 +00:00
|
|
|
return fmt.Errorf("destination %q is not a directory", destdir)
|
2020-02-02 02:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Extracting to %s:\n", destdir)
|
|
|
|
|
|
|
|
overwrite := c.Bool("overwrite")
|
|
|
|
rename := c.Bool("rename")
|
|
|
|
|
2023-04-12 10:16:45 +00:00
|
|
|
for _, a := range matchedAssetFiles {
|
2020-02-02 02:17:44 +00:00
|
|
|
if err := extractAsset(destdir, a, overwrite, rename); err != nil {
|
|
|
|
// Non-fatal error
|
2023-04-12 10:16:45 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "%s: %v", a.path, err)
|
2020-02-02 02:17:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-12 10:16:45 +00:00
|
|
|
func extractAsset(d string, a assetFile, overwrite, rename bool) error {
|
|
|
|
dest := filepath.Join(d, filepath.FromSlash(a.path))
|
2020-02-02 02:17:44 +00:00
|
|
|
dir := filepath.Dir(dest)
|
|
|
|
|
2023-04-12 10:16:45 +00:00
|
|
|
data, err := a.fs.ReadFile(a.name)
|
2020-02-02 02:17:44 +00:00
|
|
|
if err != nil {
|
2023-04-12 10:16:45 +00:00
|
|
|
return fmt.Errorf("%s: %w", a.path, err)
|
2020-02-02 02:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
|
2022-10-24 19:29:17 +00:00
|
|
|
return fmt.Errorf("%s: %w", dir, err)
|
2020-02-02 02:17:44 +00:00
|
|
|
}
|
|
|
|
|
2022-01-20 17:46:10 +00:00
|
|
|
perms := os.ModePerm & 0o666
|
2020-02-02 02:17:44 +00:00
|
|
|
|
|
|
|
fi, err := os.Lstat(dest)
|
|
|
|
if err != nil {
|
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
2022-10-24 19:29:17 +00:00
|
|
|
return fmt.Errorf("%s: %w", dest, err)
|
2020-02-02 02:17:44 +00:00
|
|
|
}
|
|
|
|
} else if !overwrite && !rename {
|
|
|
|
fmt.Printf("%s already exists; skipped.\n", dest)
|
|
|
|
return nil
|
|
|
|
} else if !fi.Mode().IsRegular() {
|
|
|
|
return fmt.Errorf("%s already exists, but it's not a regular file", dest)
|
|
|
|
} else if rename {
|
2021-07-15 15:46:07 +00:00
|
|
|
if err := util.Rename(dest, dest+".bak"); err != nil {
|
2023-04-12 10:16:45 +00:00
|
|
|
return fmt.Errorf("error creating backup for %s: %w", dest, err)
|
2020-02-02 02:17:44 +00:00
|
|
|
}
|
|
|
|
// Attempt to respect file permissions mask (even if user:group will be set anew)
|
|
|
|
perms = fi.Mode()
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.OpenFile(dest, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, perms)
|
|
|
|
if err != nil {
|
2022-10-24 19:29:17 +00:00
|
|
|
return fmt.Errorf("%s: %w", dest, err)
|
2020-02-02 02:17:44 +00:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if _, err = file.Write(data); err != nil {
|
2022-10-24 19:29:17 +00:00
|
|
|
return fmt.Errorf("%s: %w", dest, err)
|
2020-02-02 02:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(dest)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-12 10:16:45 +00:00
|
|
|
func collectAssetFilesByPattern(c *cli.Context, globs []glob.Glob, path string, layer *assetfs.Layer) {
|
|
|
|
fs := assetfs.Layered(layer)
|
|
|
|
files, err := fs.ListAllFiles(".", true)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error listing files in %q: %v", path, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, name := range files {
|
|
|
|
if path == "public" &&
|
|
|
|
strings.HasPrefix(name, "vendor/") &&
|
|
|
|
!c.Bool("include-vendored") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
matchName := path + "/" + name
|
|
|
|
for _, g := range globs {
|
|
|
|
if g.Match(matchName) {
|
|
|
|
matchedAssetFiles = append(matchedAssetFiles, assetFile{fs: fs, name: name, path: path + "/" + name})
|
|
|
|
break
|
2020-02-02 02:17:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 10:16:45 +00:00
|
|
|
func compileCollectPatterns(args []string) ([]glob.Glob, error) {
|
2020-02-02 02:17:44 +00:00
|
|
|
if len(args) == 0 {
|
|
|
|
args = []string{"**"}
|
|
|
|
}
|
|
|
|
pat := make([]glob.Glob, len(args))
|
|
|
|
for i := range args {
|
|
|
|
if g, err := glob.Compile(args[i], '/'); err != nil {
|
2022-10-24 19:29:17 +00:00
|
|
|
return nil, fmt.Errorf("'%s': Invalid glob pattern: %w", args[i], err)
|
2023-04-12 10:16:45 +00:00
|
|
|
} else { //nolint:revive
|
2020-02-02 02:17:44 +00:00
|
|
|
pat[i] = g
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pat, nil
|
|
|
|
}
|