1
0
Files
Skill/manager/internal/config/install.go

142 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package config
import (
"encoding/json"
"fmt"
"os"
"skillmgr/internal/types"
)
// LoadInstallConfig 加载安装配置
func LoadInstallConfig() (*types.InstallConfig, error) {
path, err := GetInstallConfigPath()
if err != nil {
return nil, err
}
if _, err := os.Stat(path); os.IsNotExist(err) {
return &types.InstallConfig{
Installations: []types.InstallRecord{},
}, nil
}
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var cfg types.InstallConfig
if err := json.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("解析 install.json 失败: %w请检查 JSON 格式)", err)
}
return &cfg, nil
}
// SaveInstallConfig 保存安装配置
func SaveInstallConfig(cfg *types.InstallConfig) error {
path, err := GetInstallConfigPath()
if err != nil {
return err
}
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}
return os.WriteFile(path, data, 0644)
}
// AddInstallRecord 添加安装记录
func AddInstallRecord(record types.InstallRecord) error {
cfg, err := LoadInstallConfig()
if err != nil {
return err
}
cfg.Installations = append(cfg.Installations, record)
return SaveInstallConfig(cfg)
}
// RemoveInstallRecord 移除安装记录
func RemoveInstallRecord(itemType types.ItemType, name string, platform types.Platform, scope types.Scope) error {
cfg, err := LoadInstallConfig()
if err != nil {
return err
}
for i, r := range cfg.Installations {
if r.Type == itemType && r.Name == name && r.Platform == platform && r.Scope == scope {
cfg.Installations = append(cfg.Installations[:i], cfg.Installations[i+1:]...)
return SaveInstallConfig(cfg)
}
}
return nil
}
// FindInstallRecord 查找安装记录
func FindInstallRecord(itemType types.ItemType, name string, platform types.Platform, scope types.Scope) (*types.InstallRecord, error) {
cfg, err := LoadInstallConfig()
if err != nil {
return nil, err
}
for _, r := range cfg.Installations {
if r.Type == itemType && r.Name == name && r.Platform == platform && r.Scope == scope {
return &r, nil
}
}
return nil, nil
}
// UpdateInstallRecord 更新安装记录
func UpdateInstallRecord(record types.InstallRecord) error {
cfg, err := LoadInstallConfig()
if err != nil {
return err
}
for i, r := range cfg.Installations {
if r.Type == record.Type && r.Name == record.Name &&
r.Platform == record.Platform && r.Scope == record.Scope {
cfg.Installations[i] = record
return SaveInstallConfig(cfg)
}
}
return nil
}
// CleanOrphanRecords 清理孤立记录(安装路径不存在)
func CleanOrphanRecords() ([]types.InstallRecord, error) {
cfg, err := LoadInstallConfig()
if err != nil {
return nil, err
}
// 预分配切片容量,减少内存分配次数
cleaned := make([]types.InstallRecord, 0, len(cfg.Installations)/2)
valid := make([]types.InstallRecord, 0, len(cfg.Installations))
for _, r := range cfg.Installations {
if _, err := os.Stat(r.InstallPath); os.IsNotExist(err) {
cleaned = append(cleaned, r)
} else {
valid = append(valid, r)
}
}
if len(cleaned) > 0 {
cfg.Installations = valid
if err := SaveInstallConfig(cfg); err != nil {
return nil, err
}
}
return cleaned, nil
}