fix: workdir path error

This commit is contained in:
deepzz0
2021-05-08 10:54:21 +08:00
parent 79ac024312
commit c18f3b7da9
7 changed files with 40 additions and 7 deletions

1
.gitignore vendored
View File

@@ -6,6 +6,7 @@
*.dylib
*.DS_Store
*.tar.gz
backend
# Test binary, built with `go test -c`
*.test

View File

@@ -24,7 +24,7 @@ dist:
# clean
clean:
@rm -rf bin && rm *.tar.gz
@rm -rf bin && rm -f *.tar.gz && rm -f backend
# protoc
protoc:

View File

@@ -135,10 +135,7 @@ type Config struct {
func init() {
// compatibility linux and windows
var err error
if gopath := os.Getenv("GOPATH"); gopath != "" {
WorkDir = filepath.Join(gopath, "src", "github.com",
"eiblog", "eiblog")
}
WorkDir = workDir()
path := filepath.Join(WorkDir, "conf", "app.yml")
data, err := ioutil.ReadFile(path)

28
pkg/config/dev.go Normal file
View File

@@ -0,0 +1,28 @@
// +build !prod
// Package config provides ...
package config
import (
"os"
"path"
"path/filepath"
)
// workDir recognize workspace dir
var workDir = func() string {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
for wd != "" {
name := filepath.Join(wd, "conf")
_, err := os.Stat(name)
if err != nil {
wd, _ = path.Split(wd)
continue
}
return wd
}
return ""
}

7
pkg/config/pro.go Normal file
View File

@@ -0,0 +1,7 @@
// +build prod
// Package config provides ...
package config
// workDir production use current dir
var workDir = func() string { return "" }

View File

@@ -11,7 +11,7 @@ for file in pkg/core/*; do
for os in linux darwin windows; do
_target="$app-$_tag.$os-$_arch.tar.gz"
CGO_ENABLED=0 GOOS=$os GOARCH=$_arch \
go build -o backend "./cmd/$app"
go build -tags prod -o backend "./cmd/$app"
if [ "$app" = "eiblog" ]; then
tar czf $_target conf website assets backend
else

View File

@@ -19,7 +19,7 @@ docker buildx create --use --name builder
# build demo app
for file in pkg/core/*; do
app="$(basename $file)";
CGO_ENABLED=0 go build -o bin/backend "./cmd/$app"
CGO_ENABLED=0 go build -tags prod -o bin/backend "./cmd/$app"
# docker image
docker buildx build --platform "$_platform" \
-f "build/package/$app.Dockerfile" \