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

Implement "embedded" command to extract static resources #9982

Merged
merged 17 commits into from
Feb 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
draft
  • Loading branch information
guillep2k committed Jan 25, 2020
commit 7380696e4cb771d99db1f455f82e00290938f681
168 changes: 168 additions & 0 deletions cmd/embedded.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

// +build bindata

package cmd

import (
"fmt"
// "os"
// "path/filepath"
"strings"

"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"

"github.com/gobwas/glob"
"github.com/urfave/cli"
)

// Cmdembedded represents the available extract sub-command.
var (
Cmdembedded = cli.Command{
Name: "embedded",
Usage: "Extract embedded resources",
Description: "A command for extracting embedded resources, like templates and images.",
Subcommands: []cli.Command{
subcmdList,
subcmdExtract,
},
Flags: []cli.Flag{
/*
cli.StringFlag{
Name: "name, n",
Value: "**",
Usage: "glob pattern used to match files",
},
*/
cli.BoolFlag{
Name: "include-vendored,vendor",
Usage: "Include files under public/vendor as well",
},
},
}

subcmdList = cli.Command{
Name: "list",
Usage: "List files matching the given pattern",
Action: runList,
}

subcmdExtract = cli.Command{
Name: "extract",
Usage: "Extract resources",
Action: runExtract,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "overwrite",
Usage: "Overwrite files if they already exist",
},
cli.BoolFlag{
Name: "custom",
Usage: "Extract to the 'custom' directory",
},
cli.StringFlag{
Name: "destination",
Usage: "Destination for the extracted files",
},
},
}

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

type section struct {
Path string
Names func() []string
IsDir func(string) (bool, error)
}

type asset struct {
Section *section
Name string
}

func initEmbeddedExtractor(c *cli.Context) error {

// Silence the console logger
log.DelNamedLogger("console")

// Read configuration file
setting.NewContext()

pats, err := getPatterns(c.Args())
if err != nil {
return err
}
sections := make(map[string]*section,3)

sections["public"] = &section{ Path: "public", Names: public.AssetNames, IsDir: public.AssetIsDir }
sections["options"] = &section{ Path: "options", Names: options.AssetNames, IsDir: options.AssetIsDir }
sections["templates"] = &section{ Path: "templates", Names: templates.AssetNames, IsDir: templates.AssetIsDir }

for _, sec := range sections {
assets = append(assets, buildAssetList(sec, pats, c)...)
}

return nil
}

func runList(ctx *cli.Context) error {
if err := initEmbeddedExtractor(ctx); err != nil {
return err
}
// fmt.Println("Using app.ini at", setting.CustomConf)

for _, asset := range assets {
fmt.Printf("- [%s] [%s]\n", asset.Section.Path, asset.Name)
}
fmt.Println("End of list.")
return nil
}

func runExtract(ctx *cli.Context) error {
fmt.Println("Not implemented")
return nil
}

func buildAssetList(sec *section, globs []glob.Glob, c *cli.Context) []asset {
var 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})
break
}
}
}
}
return results
}

func getPatterns(args []string) ([]glob.Glob, error) {
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 {
return nil, fmt.Errorf("'%s': Invalid glob pattern: %v", args[i], err)
} else {
pat[i] = g
}
}
return pat, nil
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ arguments - which can alternatively be run by running the subcommand web.`
cmd.CmdKeys,
cmd.CmdConvert,
cmd.CmdDoctor,
cmd.Cmdembedded,
}
// Now adjust these commands to add our global configuration options

Expand Down
31 changes: 31 additions & 0 deletions modules/options/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,34 @@ func fileFromDir(name string) ([]byte, error) {

return ioutil.ReadAll(f)
}

func Asset(name string) ([]byte, error) {
f, err := Assets.Open("/" + name)
if err != nil {
return nil, err
}
defer f.Close()
return ioutil.ReadAll(f)
}

func AssetNames() []string {
realFS := Assets.(vfsgen۰FS)
var results = make([]string, 0, len(realFS))
for k := range realFS {
results = append(results, k[1:])
}
return results
}

func AssetIsDir(name string) (bool, error) {
if f, err := Assets.Open("/" + name); err != nil {
return false, err
} else {
defer f.Close()
if fi, err := f.Stat(); err != nil {
return false, err
} else {
return fi.IsDir(), nil
}
}
}
33 changes: 33 additions & 0 deletions modules/public/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
package public

import (
"io/ioutil"

"gitea.com/macaron/macaron"
)

Expand All @@ -17,3 +19,34 @@ func Static(opts *Options) macaron.Handler {
// used when in the options there is no FileSystem.
return opts.staticHandler("")
}

func Asset(name string) ([]byte, error) {
f, err := Assets.Open("/" + name)
if err != nil {
return nil, err
}
defer f.Close()
return ioutil.ReadAll(f)
}

func AssetNames() []string {
realFS := Assets.(vfsgen۰FS)
var results = make([]string, 0, len(realFS))
for k := range realFS {
results = append(results, k[1:])
}
return results
}

func AssetIsDir(name string) (bool, error) {
if f, err := Assets.Open("/" + name); err != nil {
return false, err
} else {
defer f.Close()
if fi, err := f.Stat(); err != nil {
return false, err
} else {
return fi.IsDir(), nil
}
}
}
13 changes: 13 additions & 0 deletions modules/templates/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,16 @@ func AssetNames() []string {
}
return results
}

func AssetIsDir(name string) (bool, error) {
if f, err := Assets.Open("/" + name); err != nil {
return false, err
} else {
defer f.Close()
if fi, err := f.Stat(); err != nil {
return false, err
} else {
return fi.IsDir(), nil
}
}
}