fix: 按系统规范解析桌面服务数据目录

macOS 和 Windows 在未显式指定工作目录时,通过 Go 的用户配置目录接口解析 YdisksXianyuHelper 数据目录;macOS LaunchAgent 不再注入数据库、密钥和浏览器目录参数。

服务自动创建数据目录及 data 子目录,将默认 SQLite 数据库放置到系统用户数据目录,修复 macOS 服务因 data/xianyu_data.db 父目录不存在而启动即退出的问题。

保留显式工作目录、Windows 现有 ProgramData 数据布局以及 Linux/Docker 的默认相对路径行为,并补充服务目录解析和数据库路径测试。

删除名称为桌面端 CI 的 GitHub Actions Workflow,保留桌面安装包构建发布 Workflow。
This commit is contained in:
Christ committed 2026-08-12 00:19:14 +08:00
1 parent bfa72fd9b7
commit d4e9717570
5 files changed
+94 -98

No files matched your search

-86
View File
@@ -1,86 +0,0 @@
name: 桌面端 CI
on:
push:
branches:
- codex/desktop-packaging
workflow_dispatch:
permissions:
contents: read
concurrency:
group: desktop-ci-${{ github.ref }}
cancel-in-progress: true
jobs:
verify:
name: Go 与前端检查
if: github.ref == 'refs/heads/codex/desktop-packaging'
runs-on: ubuntu-latest
steps:
- name: 检出代码
uses: actions/checkout@v6
- name: 配置 Go
uses: actions/setup-go@v7
with:
go-version-file: go.mod
cache: true
- name: 配置 Node.js
uses: actions/setup-node@v6
with:
node-version: 22
cache: npm
cache-dependency-path: frontend/package-lock.json
- name: 构建嵌入式前端
run: npm ci --prefix frontend && npm run build --prefix frontend
- name: 运行 Go 测试
run: go test ./...
- name: 运行 go vet
run: go vet ./...
cross-build:
name: 核心二进制 ${{ matrix.goos }}/${{ matrix.goarch }}
if: github.ref == 'refs/heads/codex/desktop-packaging'
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- goos: linux
goarch: amd64
- goos: linux
goarch: arm64
- goos: windows
goarch: amd64
- goos: windows
goarch: arm64
- goos: darwin
goarch: amd64
- goos: darwin
goarch: arm64
steps:
- name: 检出代码
uses: actions/checkout@v6
- name: 配置 Go
uses: actions/setup-go@v7
with:
go-version-file: go.mod
cache: true
- name: 编译 server 与浏览器安装器
shell: bash
env:
GOOS: ${{ matrix.goos }}
GOARCH: ${{ matrix.goarch }}
CGO_ENABLED: 0
run: |
mkdir -p dist
go build -trimpath -ldflags='-s -w' -o "dist/xianyu-server-${GOOS}-${GOARCH}" ./cmd/server
go build -trimpath -ldflags='-s -w' -o "dist/browser-install-${GOOS}-${GOARCH}" ./cmd/browser-install
+57 -2
View File
@@ -13,6 +13,7 @@ import (
"os"
"os/signal"
"path/filepath"
"runtime"
"strings"
"syscall"
@@ -50,6 +51,12 @@ type serverOptions struct {
service bool
}
const (
defaultDBPath = "data/xianyu_data.db"
userDataDirName = "YdisksXianyuHelper"
defaultDataKeyName = "data-key"
)
func main() {
opts := parseOptions()
if opts.workDir != "" {
@@ -78,7 +85,7 @@ func main() {
func parseOptions() serverOptions {
var opts serverOptions
flag.StringVar(&opts.dbPath, "db", "data/xianyu_data.db", "SQLite 数据库路径(兼容旧用法)")
flag.StringVar(&opts.dbPath, "db", defaultDBPath, "SQLite 数据库路径(兼容旧用法)")
flag.StringVar(&opts.dbURL, "db-url", "", "数据库连接 URL(sqlite:// mysql:// postgres://),优先级高于 -db;也可用 DATABASE_URL 环境变量")
flag.StringVar(&opts.addr, "addr", ":8080", "HTTP 监听地址")
flag.StringVar(&opts.webDir, "web", "", "前端静态资源目录(含 index.html)")
@@ -104,6 +111,25 @@ func runServer(parent context.Context, opts serverOptions) error {
ctx, cancel := context.WithCancel(parent)
defer cancel()
dataDir, err := resolveDataDir(opts.workDir)
if err != nil {
return err
}
if dataDir != "" {
if err := os.MkdirAll(dataDir, 0o700); err != nil {
return fmt.Errorf("创建应用数据目录失败: %w", err)
}
if opts.dataKeyFile == "" {
opts.dataKeyFile = filepath.Join(dataDir, defaultDataKeyName)
}
if opts.playwrightDriverDir == "" {
opts.playwrightDriverDir = filepath.Join(dataDir, "playwright-driver")
}
if opts.playwrightBrowserDir == "" {
opts.playwrightBrowserDir = filepath.Join(dataDir, "playwright-browsers")
}
}
if opts.playwrightDriverDir != "" {
if err := os.Setenv("PLAYWRIGHT_DRIVER_PATH", opts.playwrightDriverDir); err != nil {
return fmt.Errorf("设置 Playwright driver 目录失败: %w", err)
@@ -129,7 +155,12 @@ func runServer(parent context.Context, opts serverOptions) error {
resolvedDBURL = strings.TrimSpace(opts.dbURL)
}
if resolvedDBURL == "" {
resolvedDBURL = opts.dbPath
resolvedDBURL = resolveDBPath(dataDir, opts.dbPath)
}
if dataDir != "" && resolvedDBURL == resolveDBPath(dataDir, defaultDBPath) {
if err := os.MkdirAll(filepath.Dir(resolvedDBURL), 0o700); err != nil {
return fmt.Errorf("创建数据库目录失败: %w", err)
}
}
resolvedLogLevel := strings.TrimSpace(os.Getenv("LOG_LEVEL"))
@@ -245,6 +276,30 @@ func runServer(parent context.Context, opts serverOptions) error {
return runErr
}
// resolveDataDir 返回桌面端的标准用户数据目录。
// Linux/Docker 保留原有相对路径行为;macOS 和 Windows 在没有显式 -workdir
// 时使用当前用户的系统配置目录,避免把具体用户路径写进安装包或代码。
func resolveDataDir(workDir string) (string, error) {
if strings.TrimSpace(workDir) != "" {
return filepath.Clean(workDir), nil
}
if runtime.GOOS != "darwin" && runtime.GOOS != "windows" {
return "", nil
}
configDir, err := os.UserConfigDir()
if err != nil {
return "", fmt.Errorf("读取用户配置目录失败: %w", err)
}
return filepath.Join(configDir, userDataDirName), nil
}
func resolveDBPath(dataDir, configuredPath string) string {
if dataDir != "" && configuredPath == defaultDBPath {
return filepath.Join(dataDir, "data", "xianyu_data.db")
}
return configuredPath
}
func loadOrCreateDataKey(path string) (string, error) {
path = strings.TrimSpace(path)
if path == "" {
+37
View File
@@ -54,3 +54,40 @@ func TestLoadOrCreateDataKeyPersists(t *testing.T) {
t.Fatalf("data key file was not written: err=%v", err)
}
}
func TestResolveDataDirKeepsExplicitDirectory(t *testing.T) {
explicit := filepath.Join(t.TempDir(), "ydisks-data")
got, err := resolveDataDir(explicit)
if err != nil {
t.Fatalf("resolve explicit data directory: %v", err)
}
if got != explicit {
t.Fatalf("explicit data directory changed: got %q want %q", got, explicit)
}
}
func TestUserDataDirName(t *testing.T) {
base := filepath.Join(t.TempDir(), "Application Support")
got := filepath.Join(base, userDataDirName)
want := filepath.Join(base, "YdisksXianyuHelper")
if got != want {
t.Fatalf("unexpected user data directory: got %q want %q", got, want)
}
}
func TestResolveDBPathUsesDataDirectoryForDefault(t *testing.T) {
dataDir := filepath.Join(t.TempDir(), "YdisksXianyuHelper")
got := resolveDBPath(dataDir, defaultDBPath)
want := filepath.Join(dataDir, "data", "xianyu_data.db")
if got != want {
t.Fatalf("unexpected default database path: got %q want %q", got, want)
}
}
func TestResolveDBPathPreservesCustomPath(t *testing.T) {
dataDir := filepath.Join(t.TempDir(), "YdisksXianyuHelper")
custom := filepath.Join(t.TempDir(), "custom.db")
if got := resolveDBPath(dataDir, custom); got != custom {
t.Fatalf("custom database path changed: got %q want %q", got, custom)
}
}
@@ -7,10 +7,6 @@
<array>
<string>__SERVER__</string>
<string>-addr</string><string>127.0.0.1:8080</string>
<string>-workdir</string><string>__DATA_DIR__</string>
<string>-data-key-file</string><string>__DATA_KEY__</string>
<string>-playwright-driver-dir</string><string>__DRIVER_DIR__</string>
<string>-playwright-browser-dir</string><string>__BROWSER_DIR__</string>
</array>
<key>WorkingDirectory</key><string>__DATA_DIR__</string>
<key>RunAtLoad</key><true/>
-6
View File
@@ -54,10 +54,7 @@ escape_sed() {
SERVER_ESCAPED="$(escape_sed "$SERVER")"
TRAY_ESCAPED="$(escape_sed "$TRAY")"
DATA_ESCAPED="$(escape_sed "$DATA_DIR")"
DRIVER_ESCAPED="$(escape_sed "$DRIVER_DIR")"
BROWSER_ESCAPED="$(escape_sed "$BROWSER_DIR")"
LOG_ESCAPED="$(escape_sed "$LOG_DIR")"
DATA_KEY_ESCAPED="$(escape_sed "$DATA_DIR/data-key")"
SERVER_LABEL="com.ydisks.xianyu-helper.server"
TRAY_LABEL="com.ydisks.xianyu-helper.tray"
@@ -65,9 +62,6 @@ server_plist="$AGENT_DIR/$SERVER_LABEL.plist"
tray_plist="$AGENT_DIR/$TRAY_LABEL.plist"
sed -e "s|__SERVER__|$SERVER_ESCAPED|g" \
-e "s|__DATA_DIR__|$DATA_ESCAPED|g" \
-e "s|__DATA_KEY__|$DATA_KEY_ESCAPED|g" \
-e "s|__DRIVER_DIR__|$DRIVER_ESCAPED|g" \
-e "s|__BROWSER_DIR__|$BROWSER_ESCAPED|g" \
-e "s|__LOG_DIR__|$LOG_ESCAPED|g" \
"$APP/Contents/Resources/com.ydisks.xianyu-helper.server.plist.template" > "$server_plist"
sed -e "s|__TRAY__|$TRAY_ESCAPED|g" \