Skip to content

Commit c155be7

Browse files
committed
Make bindata.go optional, remove from repository
By default will read assets from filesystems, but can embed them by passing the "bindata" build tag (go build -tags bindata) Fixes #77 Print a WARNING when bindata is not embedded, and honours GITEA_WORK_DIR, when given, for finding filesystem assets. Include automatic test for the dynamic "bin"data library
1 parent c8f300b commit c155be7

File tree

5 files changed

+173
-5331
lines changed

5 files changed

+173
-5331
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
*.a
44
*.so
55

6+
# Generated files
7+
modules/bindata/bindata.go
8+
69
# Folders
710
_obj
811
_test

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ all: build
3434
clean:
3535
go clean -i ./...
3636
rm -rf $(EXECUTABLE) $(DIST)
37+
rm -rf ./modules/bindata/bindata.go
3738

3839
.PHONY: fmt
3940
fmt:
@@ -116,7 +117,7 @@ modules/bindata/bindata.go: $(BINDATA)
116117
@which go-bindata > /dev/null; if [ $$? -ne 0 ]; then \
117118
go get -u github.com/jteeuwen/go-bindata/...; \
118119
fi
119-
go-bindata -o=$@ -ignore="\\.go|README.md|TRANSLATORS" -pkg=bindata conf/...
120+
go-bindata -tags bindata -o=$@ -ignore="\\.go|README.md|TRANSLATORS" -pkg=bindata conf/...
120121
go fmt $@
121122
sed -i.bak 's/confLocaleLocale_/confLocaleLocale/' $@
122123
rm $@.bak

modules/bindata/bindata.go

-5,330
This file was deleted.

modules/bindata/dynamic.go

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// +build !bindata
2+
3+
package bindata
4+
5+
import (
6+
"fmt"
7+
"os"
8+
"path/filepath"
9+
"io/ioutil"
10+
"strings"
11+
)
12+
13+
var rootDir string
14+
var binData map[string] []byte
15+
var dirData map[string] []string
16+
17+
func setBinData(key string, data []byte) {
18+
if binData == nil {
19+
binData = make(map[string] []byte)
20+
}
21+
binData[key] = data
22+
}
23+
24+
func setDirData(key string, data []string) {
25+
if dirData == nil {
26+
dirData = make(map[string] []string)
27+
}
28+
dirData[key] = data
29+
}
30+
31+
func getRootDir() (string, error) {
32+
if rootDir != "" {
33+
return rootDir, nil
34+
}
35+
36+
dir := os.Getenv("GITEA_WORK_DIR")
37+
if dir == "" {
38+
dir = "."
39+
}
40+
41+
dir, err := filepath.Abs(dir)
42+
if err != nil {
43+
return "", fmt.Errorf("%v", err)
44+
}
45+
for {
46+
_, err = ioutil.ReadDir(filepath.Join(dir, "conf"))
47+
if err == nil {
48+
// TODO: check if the file is a directory
49+
break
50+
}
51+
// TODO: check that the error is a "file not found" error ?
52+
newdir := filepath.Join(dir, "..")
53+
if dir == newdir {
54+
return "", fmt.Errorf("Could not find directory containing 'conf', try setting GITEA_WORK_DIR")
55+
}
56+
dir = newdir
57+
}
58+
59+
fmt.Println("WARNING: this deveopment build of Gitea depends on a directory tree, we'll be using the one in ", dir)
60+
61+
rootDir = dir
62+
return dir, nil
63+
}
64+
65+
func resolveName(name string) (string, error) {
66+
67+
name = strings.Replace(name, "\\", "/", -1) // needed ?
68+
69+
dir, err := getRootDir()
70+
if err != nil {
71+
return "", fmt.Errorf("%v", err)
72+
}
73+
74+
return filepath.Join(dir,name), nil
75+
}
76+
77+
// MustAsset is like Asset but panics when Asset would return an error.
78+
// It simplifies safe initialization of global variables.
79+
func MustAsset(name string) []byte {
80+
a, err := Asset(name)
81+
if err != nil {
82+
panic("asset: Asset(" + name + "): " + err.Error())
83+
}
84+
85+
return a
86+
}
87+
88+
// Asset loads and returns the asset for the given name.
89+
// It returns an error if the asset could not be found or
90+
// could not be loaded.
91+
func Asset(name string) ([]byte, error) {
92+
93+
canonicalName, err := resolveName(name)
94+
if err != nil {
95+
return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err)
96+
}
97+
98+
dat := binData[canonicalName]
99+
if dat != nil { return dat, nil }
100+
101+
dat, err = ioutil.ReadFile(canonicalName)
102+
if err != nil {
103+
return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err)
104+
}
105+
106+
setBinData(canonicalName, dat)
107+
return dat, nil
108+
}
109+
110+
// AssetDir returns the file names below a certain
111+
// directory embedded in the file by go-bindata.
112+
// For example if you run go-bindata on data/... and data contains the
113+
// following hierarchy:
114+
// data/
115+
// foo.txt
116+
// img/
117+
// a.png
118+
// b.png
119+
// then AssetDir("data") would return []string{"foo.txt", "img"}
120+
// AssetDir("data/img") would return []string{"a.png", "b.png"}
121+
// AssetDir("foo.txt") and AssetDir("notexist") would return an error
122+
// AssetDir("") will return []string{"data"}.
123+
func AssetDir(name string) ([]string, error) {
124+
canonicalName, err := resolveName(name)
125+
if err != nil {
126+
return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err)
127+
}
128+
129+
rv := dirData[canonicalName]
130+
if rv != nil { return rv, nil }
131+
132+
files, err := ioutil.ReadDir(canonicalName)
133+
if err != nil {
134+
return nil, fmt.Errorf("Error reading directory %s: ", err)
135+
}
136+
137+
rv = make([]string, 0, len(files))
138+
for _, f := range files {
139+
rv = append(rv, f.Name())
140+
}
141+
142+
setDirData(canonicalName, rv)
143+
return rv, nil
144+
}

modules/bindata/dynamic_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package bindata
2+
3+
import (
4+
"testing"
5+
"io/ioutil"
6+
)
7+
8+
func Test_resolveName(t *testing.T) {
9+
10+
files := []string {
11+
"modules/bindata/dynamic_test.go",
12+
"conf/app.ini",
13+
"templates/home.tmpl",
14+
}
15+
16+
for _, c := range files {
17+
name, _ := resolveName(c)
18+
_, err := ioutil.ReadFile(name)
19+
if err != nil {
20+
t.Errorf("%s: %v", name, err)
21+
}
22+
}
23+
24+
}

0 commit comments

Comments
 (0)