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
Prev Previous commit
Next Next commit
Add !bindata stub, support Windows, fmt
  • Loading branch information
guillep2k committed Jan 25, 2020
commit f472e768f298307d3d203fa56285b4ecd975baae
31 changes: 18 additions & 13 deletions cmd/embedded.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var (
Cmdembedded = cli.Command{
Name: "embedded",
Usage: "Extract embedded resources",
Description: "A command for extracting embedded resources, like templates and images.",
Description: "A command for extracting embedded resources, like templates and images",
Subcommands: []cli.Command{
subcmdList,
subcmdExtract,
Expand Down Expand Up @@ -166,10 +166,20 @@ func runExtractDo(c *cli.Context) error {
fmt.Println("Using app.ini at", setting.CustomConf)
}

if fi, err := os.Stat(destdir); err != nil {
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() {
return fmt.Errorf("%s does not exist or is not a directory.", destdir)
return fmt.Errorf("%s is not a directory.", destdir)
}

fmt.Printf("Extracting to %s:\n", destdir)
Expand All @@ -188,7 +198,7 @@ func runExtractDo(c *cli.Context) error {
}

func extractAsset(d string, a asset, overwrite, rename bool) error {
dest := d + "/" + a.Path
dest := filepath.Join(d, filepath.FromSlash(a.Path))
dir := filepath.Dir(dest)

data, err := a.Section.Asset(a.Name)
Expand All @@ -211,7 +221,7 @@ func extractAsset(d string, a asset, overwrite, rename bool) error {
fmt.Printf("%s already exists; skipped.\n", dest)
return nil
} else if !fi.Mode().IsRegular() {
return fmt.Errorf("%s already exists and is not a regular file", dest)
return fmt.Errorf("%s already exists, but it's not a regular file", dest)
} else if rename {
if err := os.Rename(dest, dest+".bak"); err != nil {
return fmt.Errorf("Error creating backup for %s: %v", dest, err)
Expand All @@ -226,12 +236,8 @@ func extractAsset(d string, a asset, overwrite, rename bool) error {
}
defer file.Close()

for len(data) != 0 {
written, err := file.Write(data)
if err != nil {
return fmt.Errorf("%s: %v", dest, err)
}
data = data[written:]
if _, err = file.Write(data); err != nil {
return fmt.Errorf("%s: %v", dest, err)
}

fmt.Println(dest)
Expand Down Expand Up @@ -268,12 +274,11 @@ func getPatterns(args []string) ([]glob.Glob, error) {
}
pat := make([]glob.Glob, len(args))
for i := range args {
if g, err := glob.Compile(args[i], '.', '/'); err != nil {
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
}

30 changes: 30 additions & 0 deletions cmd/embedded_stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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"

"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",
Action: extractorNotImplemented,
}
)

func extractorNotImplemented(c *cli.Context) error {
err := fmt.Errorf("Sorry: the 'embedded' subcommand is not available in builds without bindata")
fmt.Fprintf(os.Stderr, "%s\n", err)
return err
}
2 changes: 1 addition & 1 deletion modules/public/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package public

import (
"io/ioutil"

"gitea.com/macaron/macaron"
)

Expand Down