From deca1d01d329f12b091000f9c310baf4f82e1392 Mon Sep 17 00:00:00 2001 From: ssongliu Date: Mon, 20 Feb 2023 15:39:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=201pctl=20=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/init/viper/viper.go | 3 +++ cmd/server/cmd/user-info.go | 17 +++++++++++++++-- cmd/server/cmd/version.go | 17 +++++++++++++++-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/backend/init/viper/viper.go b/backend/init/viper/viper.go index 35d231850..77f30aaa5 100644 --- a/backend/init/viper/viper.go +++ b/backend/init/viper/viper.go @@ -45,6 +45,9 @@ func Init() { if len(baseDir) == 0 { panic("error `BASE_DIR` find in /usr/bin/1pctl") } + if strings.HasSuffix(baseDir, "/") { + baseDir = baseDir[:strings.LastIndex(baseDir, "/")] + } reader := bytes.NewReader(conf.AppYaml) if err := v.ReadConfig(reader); err != nil { panic(fmt.Errorf("Fatal error config file: %s \n", err)) diff --git a/cmd/server/cmd/user-info.go b/cmd/server/cmd/user-info.go index d0e7e6aa3..dbd5d30f6 100644 --- a/cmd/server/cmd/user-info.go +++ b/cmd/server/cmd/user-info.go @@ -2,8 +2,10 @@ package cmd import ( "fmt" + "strings" "github.com/1Panel-dev/1Panel/backend/global" + cmdUtils "github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/encrypt" "github.com/spf13/cobra" "gorm.io/driver/sqlite" @@ -18,8 +20,19 @@ var userinfoCmd = &cobra.Command{ Use: "user-info", Short: "获取用户信息", RunE: func(cmd *cobra.Command, args []string) error { - fullPath := "/opt/1panel/db/1Panel.db" - db, err := gorm.Open(sqlite.Open(fullPath), &gorm.Config{}) + stdout, err := cmdUtils.Exec("grep '^BASE_DIR=' /usr/bin/1pctl | cut -d'=' -f2") + if err != nil { + panic(err) + } + baseDir := strings.ReplaceAll(stdout, "\n", "") + if len(baseDir) == 0 { + fmt.Printf("error `BASE_DIR` find in /usr/bin/1pctl \n") + } + if strings.HasSuffix(baseDir, "/") { + baseDir = baseDir[:strings.LastIndex(baseDir, "/")] + } + + db, err := gorm.Open(sqlite.Open(baseDir+"/1panel/db/1Panel.db"), &gorm.Config{}) if err != nil { fmt.Printf("init my db conn failed, err: %v \n", err) } diff --git a/cmd/server/cmd/version.go b/cmd/server/cmd/version.go index 8c640d3bf..8577c6383 100644 --- a/cmd/server/cmd/version.go +++ b/cmd/server/cmd/version.go @@ -2,7 +2,9 @@ package cmd import ( "fmt" + "strings" + cmdUtils "github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/spf13/cobra" "gorm.io/driver/sqlite" "gorm.io/gorm" @@ -16,8 +18,19 @@ var versionCmd = &cobra.Command{ Use: "version", Short: "获取系统版本信息", RunE: func(cmd *cobra.Command, args []string) error { - fullPath := "/opt/1panel/db/1Panel.db" - db, err := gorm.Open(sqlite.Open(fullPath), &gorm.Config{}) + stdout, err := cmdUtils.Exec("grep '^BASE_DIR=' /usr/bin/1pctl | cut -d'=' -f2") + if err != nil { + panic(err) + } + baseDir := strings.ReplaceAll(stdout, "\n", "") + if len(baseDir) == 0 { + fmt.Printf("error `BASE_DIR` find in /usr/bin/1pctl \n") + } + if strings.HasSuffix(baseDir, "/") { + baseDir = baseDir[:strings.LastIndex(baseDir, "/")] + } + + db, err := gorm.Open(sqlite.Open(baseDir+"/1panel/db/1Panel.db"), &gorm.Config{}) if err != nil { fmt.Printf("init my db conn failed, err: %v \n", err) }