Skip to content

Commit

Permalink
fix: workdir path error
Browse files Browse the repository at this point in the history
  • Loading branch information
deepzz0 committed May 8, 2021
1 parent 79ac024 commit c18f3b7
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -6,6 +6,7 @@
*.dylib
*.DS_Store
*.tar.gz
backend

# Test binary, built with `go test -c`
*.test
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -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:
Expand Down
5 changes: 1 addition & 4 deletions pkg/config/config.go
Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions pkg/config/dev.go
@@ -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 changes: 7 additions & 0 deletions pkg/config/pro.go
@@ -0,0 +1,7 @@
// +build prod

// Package config provides ...
package config

// workDir production use current dir
var workDir = func() string { return "" }
2 changes: 1 addition & 1 deletion scripts/dist_tar.sh
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion scripts/run_build.sh
Expand Up @@ -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" \
Expand Down

0 comments on commit c18f3b7

Please sign in to comment.