Skip to content

Commit

Permalink
feat: try to fix current path
Browse files Browse the repository at this point in the history
  • Loading branch information
RicheyJang committed Mar 18, 2022
1 parent d77c4d4 commit ef45e6e
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pre_works.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
Expand Down Expand Up @@ -57,6 +58,7 @@ func init() {

// DoPreWorks 进行全局初始化工作
func DoPreWorks() {
fixCurrentDir()
// 读取主配置
err := flushMainConfig(consts.DefaultConfigDir, consts.MainConfigFileName)
if err != nil {
Expand All @@ -73,6 +75,29 @@ func DoPreWorks() {
CheckDaemon()
}

// 尝试修正当前路径
func fixCurrentDir() {
runDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Errorf("无法获取当前绝对路径: %v", err)
return
}
wd, _ := os.Getwd()
if wd != runDir {
err = os.Chdir(runDir)
if err != nil {
log.Errorf("无法修改当前工作路径: %v", err)
return
}
wd, err = os.Getwd()
if err != nil {
log.Errorf("can not get wd, err=%v", err)
return
}
log.Infof("修正当前路径为%v", wd)
}
}

// 设置日志
func setupLogger() error {
// 日志等级
Expand Down

0 comments on commit ef45e6e

Please sign in to comment.