refactor: extract path logic to path.go and run go fmt on codebase

This commit is contained in:
duorameng
2026-05-22 14:52:51 +08:00
parent b6fd889661
commit 4c283a60d5
55 changed files with 409 additions and 420 deletions
+1 -76
View File
@@ -1,81 +1,6 @@
package constant
import (
"os"
"path/filepath"
"time"
)
var (
// ConfigPath 配置文件路径
ConfigPath string
// DataDir 数据目录
DataDir string
// DefaultDBPath 默认数据库路径
DefaultDBPath string
// WebDistDir 前端构建目录
WebDistDir string
// ScriptsWorkDir 脚本工作目录
ScriptsWorkDir string
)
func init() {
rootDir := ResolveAppRootDir()
ConfigPath = filepath.Clean(filepath.Join(rootDir, "configs", "config.ini"))
DataDir = filepath.Clean(filepath.Join(rootDir, "data"))
DefaultDBPath = filepath.Clean(filepath.Join(rootDir, "data", "baihu.db"))
WebDistDir = filepath.Clean(filepath.Join(rootDir, "web", "dist"))
ScriptsWorkDir = filepath.Clean(filepath.Join(rootDir, "data", "scripts"))
}
// ResolveAppRootDir 获取应用程序的绝对根目录路径。
func ResolveAppRootDir() string {
// 1. 检查当前工作目录(CWD)及其上级目录
if cwd, err := os.Getwd(); err == nil {
dir := cwd
for {
if _, err := os.Stat(filepath.Join(dir, "configs", "config.ini")); err == nil {
return dir
}
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return dir
}
parent := filepath.Dir(dir)
if parent == dir {
break
}
dir = parent
}
}
// 2. 检查当前可执行文件路径及其上级目录
if exe, err := os.Executable(); err == nil {
dir := filepath.Dir(exe)
for {
if _, err := os.Stat(filepath.Join(dir, "configs", "config.ini")); err == nil {
return dir
}
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return dir
}
parent := filepath.Dir(dir)
if parent == dir {
break
}
dir = parent
}
}
// 3. 兜底回退到当前工作目录
if cwd, err := os.Getwd(); err == nil {
return cwd
}
return "."
}
import "time"
const (
+77
View File
@@ -0,0 +1,77 @@
package constant
import (
"os"
"path/filepath"
)
var (
// ConfigPath 配置文件路径
ConfigPath string
// DataDir 数据目录
DataDir string
// DefaultDBPath 默认数据库路径
DefaultDBPath string
// WebDistDir 前端构建目录
WebDistDir string
// ScriptsWorkDir 脚本工作目录
ScriptsWorkDir string
)
func init() {
rootDir := ResolveAppRootDir()
ConfigPath = filepath.Clean(filepath.Join(rootDir, "configs", "config.ini"))
DataDir = filepath.Clean(filepath.Join(rootDir, "data"))
DefaultDBPath = filepath.Clean(filepath.Join(rootDir, "data", "baihu.db"))
WebDistDir = filepath.Clean(filepath.Join(rootDir, "web", "dist"))
ScriptsWorkDir = filepath.Clean(filepath.Join(rootDir, "data", "scripts"))
}
// ResolveAppRootDir 获取应用程序的绝对根目录路径。
func ResolveAppRootDir() string {
// 1. 检查当前工作目录(CWD)及其上级目录
if cwd, err := os.Getwd(); err == nil {
dir := cwd
for {
if _, err := os.Stat(filepath.Join(dir, "configs", "config.ini")); err == nil {
return dir
}
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return dir
}
parent := filepath.Dir(dir)
if parent == dir {
break
}
dir = parent
}
}
// 2. 检查当前可执行文件路径及其上级目录
if exe, err := os.Executable(); err == nil {
dir := filepath.Dir(exe)
for {
if _, err := os.Stat(filepath.Join(dir, "configs", "config.ini")); err == nil {
return dir
}
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return dir
}
parent := filepath.Dir(dir)
if parent == dir {
break
}
dir = parent
}
}
// 3. 兜底回退到当前工作目录
if cwd, err := os.Getwd(); err == nil {
return cwd
}
return "."
}
-1
View File
@@ -1,7 +1,6 @@
package controllers
import (
"strconv"
"sync"
"time"
-1
View File
@@ -1,7 +1,6 @@
package controllers
import (
"github.com/engigu/baihu-panel/internal/database"
"github.com/engigu/baihu-panel/internal/models"
"github.com/engigu/baihu-panel/internal/models/vo"
+1
View File
@@ -76,6 +76,7 @@ func (c *MiseController) VerifyCommand(ctx *gin.Context) {
}
utils.Success(ctx, gin.H{"command": cmd})
}
// UseGlobal 设置全局默认版本
func (c *MiseController) UseGlobal(ctx *gin.Context) {
var req struct {
@@ -1,7 +1,6 @@
package controllers
import (
"github.com/engigu/baihu-panel/internal/models/vo"
"github.com/engigu/baihu-panel/internal/services"
"github.com/engigu/baihu-panel/internal/utils"
+1 -2
View File
@@ -6,12 +6,12 @@ import (
"strings"
"github.com/engigu/baihu-panel/internal/constant"
"github.com/engigu/baihu-panel/internal/logger"
"github.com/engigu/baihu-panel/internal/models"
"github.com/engigu/baihu-panel/internal/models/vo"
"github.com/engigu/baihu-panel/internal/services"
"github.com/engigu/baihu-panel/internal/services/tasks"
"github.com/engigu/baihu-panel/internal/utils"
"github.com/engigu/baihu-panel/internal/logger"
"github.com/gin-gonic/gin"
"os"
@@ -695,4 +695,3 @@ func (tc *TaskController) ToggleTask(c *gin.Context) {
utils.Success(c, vo.ToTaskVO(updatedTask))
}
+1 -2
View File
@@ -7,9 +7,9 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"strings"
"unicode/utf8"
"github.com/engigu/baihu-panel/internal/constant"
@@ -23,7 +23,6 @@ import (
"golang.org/x/text/transform"
)
type TerminalController struct {
envService *services.EnvService
}
-1
View File
@@ -95,7 +95,6 @@ func getModelSignature(models []interface{}) string {
return hex.EncodeToString(hash[:])
}
// customMigrations 自定义迁移(处理 AutoMigrate 无法自动完成的变更)
func customMigrations() error {
// 检查 ql_tokens 表是否存在
+1 -2
View File
@@ -1,9 +1,9 @@
package executor
import (
"fmt"
"math/rand"
"strings"
"fmt"
"sync"
"time"
@@ -71,7 +71,6 @@ func (m *CronManager) Stop() {
m.logger.Infof("[CronManager] 调度管理服务已停止")
}
// AddTask 添加或更新计划任务
func (m *CronManager) AddTask(task CronTask) error {
m.mu.Lock()
-1
View File
@@ -16,7 +16,6 @@ type EnvironmentVariable struct {
UserID string `json:"user_id" gorm:"size:20;index"`
CreatedAt LocalTime `json:"created_at"`
UpdatedAt LocalTime `json:"updated_at"`
}
func (EnvironmentVariable) TableName() string {
-1
View File
@@ -20,7 +20,6 @@ func setupEventHandlers(subscribers ...eventbus.Subscriber) {
}
}
func startAppLogCleanup(appLogSvc *services.AppLogService) {
// 初始化时执行一次清理
appLogSvc.CleanUp()
+1 -1
View File
@@ -1,8 +1,8 @@
package router
import (
"strings"
"os"
"strings"
"github.com/engigu/baihu-panel/internal/controllers"
"github.com/engigu/baihu-panel/internal/middleware"
-1
View File
@@ -132,7 +132,6 @@ func initPWARoutes(root *gin.RouterGroup) {
})
}
func handleManifest(ctx *gin.Context) {
staticFS := static.GetFS()
if staticFS == nil {
+1 -2
View File
@@ -12,8 +12,8 @@ import (
"github.com/engigu/baihu-panel/internal/constant"
"github.com/engigu/baihu-panel/internal/database"
"github.com/engigu/baihu-panel/internal/logger"
"github.com/engigu/baihu-panel/internal/executor"
"github.com/engigu/baihu-panel/internal/logger"
"github.com/engigu/baihu-panel/internal/models"
"github.com/engigu/baihu-panel/internal/services/tasks"
"github.com/engigu/baihu-panel/internal/utils"
@@ -378,7 +378,6 @@ func (s *AgentService) GetTasks(agentID string) []models.AgentTask {
return result
}
// ReportResult Agent 上报执行结果
func (s *AgentService) ReportResult(result *models.AgentTaskResult) error {
// 获取依赖的服务
-1
View File
@@ -372,7 +372,6 @@ func (s *BackupService) restoreScriptsDir(r *zip.ReadCloser) {
}
}
func (s *BackupService) addDirToZip(zipWriter *zip.Writer, srcDir, prefix string) error {
return filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
-2
View File
@@ -160,8 +160,6 @@ func LoadConfig(path string) (*AppConfig, error) {
return Config, nil
}
// applyEnvOverrides 从环境变量加载配置
func applyEnvOverrides() {
// Server
+2 -1
View File
@@ -109,7 +109,6 @@ func (s *MiseService) fetchLiveLanguages() ([]MiseLanguage, error) {
return s.listFallback()
}
func (s *MiseService) listFallback() ([]MiseLanguage, error) {
cmd := exec.Command("mise", "ls")
cmd.Env = os.Environ()
@@ -329,6 +328,7 @@ func (s *MiseService) GetVerifyCommand(plugin, version string) (string, error) {
}
return m.GetVerifyCommand(version)
}
// UseGlobal 设置全局默认版本
func (s *MiseService) UseGlobal(plugin, version string) error {
cmd := exec.Command("mise", "use", "-g", fmt.Sprintf("%s@%s", plugin, version))
@@ -355,6 +355,7 @@ func (s *MiseService) UnsetGlobal(plugin, version string) error {
}
return nil
}
// Envs 获取全局环境变量
func (s *MiseService) Envs() (map[string]string, error) {
cmd := exec.Command("mise", "set")
+1 -1
View File
@@ -1,9 +1,9 @@
package repo
import (
"github.com/engigu/baihu-panel/internal/models"
"regexp"
"strings"
"github.com/engigu/baihu-panel/internal/models"
)
// QinglongStrategy 实现与青龙兼容的解析逻辑
+2 -2
View File
@@ -3,12 +3,12 @@ package repo
import (
"bufio"
"fmt"
"github.com/engigu/baihu-panel/internal/utils"
"github.com/robfig/cron/v3"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/engigu/baihu-panel/internal/utils"
"github.com/robfig/cron/v3"
)
var (
+1 -1
View File
@@ -4,11 +4,11 @@ import (
"encoding/json"
"time"
"github.com/engigu/baihu-panel/internal/constant"
"github.com/engigu/baihu-panel/internal/database"
"github.com/engigu/baihu-panel/internal/logger"
"github.com/engigu/baihu-panel/internal/models"
"github.com/engigu/baihu-panel/internal/systime"
"github.com/engigu/baihu-panel/internal/constant"
"github.com/engigu/baihu-panel/internal/utils"
)
-1
View File
@@ -92,7 +92,6 @@ func (ts *TaskService) CreateTask(p *TaskParam) *models.Task {
}
database.DB.Select("*").Create(task)
return task
}
+1
View File
@@ -8,6 +8,7 @@ import (
func GenerateID() string {
return xid.New().String()
}
// IsNumeric 检查字符串是否全为数字
func IsNumeric(s string) bool {
for _, c := range s {
-1
View File
@@ -14,7 +14,6 @@ var (
shellOnce sync.Once
)
// GetShell 返回当前操作系统的 shell 和参数
func GetShell() (shell string, args []string) {
shellOnce.Do(func() {