mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-09-03 14:39:26 +08:00
- Added `ResolvePluginsDir` to normalize plugin directory paths, including tilde (`~`) expansion. - Integrated directory resolution into config loading, runtime setup, and plugin management flows. - Updated tests across components to validate correct handling of unresolved and expanded plugin paths. - Added error handling for invalid or unresolved plugin directories to prevent runtime issues. Closes: #4313
47 lines
1.3 KiB
Go
47 lines
1.3 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
const defaultPluginsDir = "plugins"
|
|
|
|
// ResolvePluginsDir normalizes the plugin directory for consistent use throughout the app.
|
|
// It expands a leading tilde (~) to the user's home directory and defaults empty values to plugins.
|
|
func ResolvePluginsDir(pluginsDir string) (string, error) {
|
|
pluginsDir = strings.TrimSpace(pluginsDir)
|
|
if pluginsDir == "" {
|
|
pluginsDir = defaultPluginsDir
|
|
}
|
|
if strings.HasPrefix(pluginsDir, "~") {
|
|
homeDir, errUserHomeDir := os.UserHomeDir()
|
|
if errUserHomeDir != nil {
|
|
return "", fmt.Errorf("resolve plugins directory: %w", errUserHomeDir)
|
|
}
|
|
remainder := strings.TrimPrefix(pluginsDir, "~")
|
|
remainder = strings.TrimLeft(remainder, "/\\")
|
|
if remainder == "" {
|
|
return filepath.Clean(homeDir), nil
|
|
}
|
|
normalized := strings.ReplaceAll(remainder, "\\", "/")
|
|
return filepath.Clean(filepath.Join(homeDir, filepath.FromSlash(normalized))), nil
|
|
}
|
|
return filepath.Clean(pluginsDir), nil
|
|
}
|
|
|
|
// ResolvePluginsDir resolves and stores the effective plugin directory.
|
|
func (cfg *Config) ResolvePluginsDir() error {
|
|
if cfg == nil {
|
|
return nil
|
|
}
|
|
pluginsDir, errResolvePluginsDir := ResolvePluginsDir(cfg.Plugins.Dir)
|
|
if errResolvePluginsDir != nil {
|
|
return errResolvePluginsDir
|
|
}
|
|
cfg.Plugins.Dir = pluginsDir
|
|
return nil
|
|
}
|