mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 12:23:22 +00:00
refactor: deduplicate agent path resolution and add agent backup to sync/target
Extract resolveAgentBackupPath() in restore_tui.go to replace 3 identical inline agent path resolution blocks (~30 lines removed). The new helper delegates to the canonical resolveAgentTargetPath() with builtin fallback. Add agent directory backup before sync (backupTargetsBeforeSync) and before target removal (backupTargets) so agent data is preserved alongside skill backups. Extract resolveGlobalAgentBackupContextFromCfg to avoid redundant config.Load() when config is already available.
This commit is contained in:
@@ -120,7 +120,12 @@ func resolveGlobalAgentBackupContext() (string, []agentTarget, error) {
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
return resolveGlobalAgentBackupContextFromCfg(cfg)
|
||||
}
|
||||
|
||||
// resolveGlobalAgentBackupContextFromCfg is like resolveGlobalAgentBackupContext
|
||||
// but accepts an already-loaded config to avoid redundant config.Load() calls.
|
||||
func resolveGlobalAgentBackupContextFromCfg(cfg *config.Config) (string, []agentTarget, error) {
|
||||
builtinAgents := config.DefaultAgentTargets()
|
||||
var targets []agentTarget
|
||||
for name := range cfg.Targets {
|
||||
|
||||
@@ -34,6 +34,14 @@ func agentBaseTarget(name string) string {
|
||||
return strings.TrimSuffix(name, "-agents")
|
||||
}
|
||||
|
||||
// resolveAgentBackupPath resolves the agent target path for a backup entry name,
|
||||
// reusing the canonical resolveAgentTargetPath with builtin fallback.
|
||||
func resolveAgentBackupPath(targets map[string]config.TargetConfig, entryName string) string {
|
||||
baseName := agentBaseTarget(entryName)
|
||||
tc := targets[baseName] // zero-value is safe — AgentsConfig returns empty, falls through to builtin
|
||||
return resolveAgentTargetPath(tc, config.DefaultAgentTargets(), baseName)
|
||||
}
|
||||
|
||||
// restorePhase tracks which screen is active.
|
||||
type restorePhase int
|
||||
|
||||
@@ -477,21 +485,9 @@ func (m restoreTUIModel) startRestore() (tea.Model, tea.Cmd) {
|
||||
cmd := func() tea.Msg {
|
||||
start := time.Now()
|
||||
|
||||
// Resolve destination path — agent backups restore to the agent directory.
|
||||
var destPath string
|
||||
if isAgentBackupEntry(targetName) {
|
||||
baseName := agentBaseTarget(targetName)
|
||||
if tc, ok := targets[baseName]; ok {
|
||||
if ac := tc.AgentsConfig(); ac.Path != "" {
|
||||
destPath = config.ExpandPath(ac.Path)
|
||||
}
|
||||
}
|
||||
if destPath == "" {
|
||||
builtinAgents := config.DefaultAgentTargets()
|
||||
if bt, ok := builtinAgents[baseName]; ok {
|
||||
destPath = config.ExpandPath(bt.Path)
|
||||
}
|
||||
}
|
||||
destPath = resolveAgentBackupPath(targets, targetName)
|
||||
} else {
|
||||
if tc, ok := targets[targetName]; ok {
|
||||
destPath = tc.SkillsConfig().Path
|
||||
@@ -879,20 +875,8 @@ func (m restoreTUIModel) renderTargetDetail(s backup.TargetBackupSummary) string
|
||||
|
||||
row("Target: ", s.TargetName)
|
||||
|
||||
// Target path and current state — agent entries resolve via AgentsConfig or builtin defaults.
|
||||
if isAgentBackupEntry(s.TargetName) {
|
||||
baseName := agentBaseTarget(s.TargetName)
|
||||
var agentPath string
|
||||
if t, ok := m.targets[baseName]; ok {
|
||||
if ac := t.AgentsConfig(); ac.Path != "" {
|
||||
agentPath = config.ExpandPath(ac.Path)
|
||||
}
|
||||
}
|
||||
if agentPath == "" {
|
||||
if bt, ok := config.DefaultAgentTargets()[baseName]; ok {
|
||||
agentPath = config.ExpandPath(bt.Path)
|
||||
}
|
||||
}
|
||||
agentPath := resolveAgentBackupPath(m.targets, s.TargetName)
|
||||
if agentPath != "" {
|
||||
row("Path: ", agentPath)
|
||||
row("Status: ", describeTargetState(agentPath))
|
||||
@@ -971,20 +955,9 @@ func (m restoreTUIModel) renderVersionDetail(v backup.BackupVersion) string {
|
||||
row("Size: ", "calculating...")
|
||||
}
|
||||
|
||||
// Diff with current target — resolve agent path for agent backup entries.
|
||||
var diffPath string
|
||||
if isAgentBackupEntry(m.selectedTarget) {
|
||||
baseName := agentBaseTarget(m.selectedTarget)
|
||||
if t, ok := m.targets[baseName]; ok {
|
||||
if ac := t.AgentsConfig(); ac.Path != "" {
|
||||
diffPath = config.ExpandPath(ac.Path)
|
||||
}
|
||||
}
|
||||
if diffPath == "" {
|
||||
if bt, ok := config.DefaultAgentTargets()[baseName]; ok {
|
||||
diffPath = config.ExpandPath(bt.Path)
|
||||
}
|
||||
}
|
||||
diffPath = resolveAgentBackupPath(m.targets, m.selectedTarget)
|
||||
} else if t, ok := m.targets[m.selectedTarget]; ok {
|
||||
diffPath = t.SkillsConfig().Path
|
||||
}
|
||||
|
||||
@@ -439,6 +439,25 @@ func backupTargetsBeforeSync(cfg *config.Config) {
|
||||
ui.Success("%s -> %s", name, backupPath)
|
||||
}
|
||||
}
|
||||
|
||||
// Also backup agent targets if any exist.
|
||||
backupDir, agentTargets, err := resolveGlobalAgentBackupContextFromCfg(cfg)
|
||||
if err != nil || len(agentTargets) == 0 {
|
||||
return
|
||||
}
|
||||
for _, at := range agentTargets {
|
||||
entryName := at.name + "-agents"
|
||||
bp, bErr := backup.CreateInDir(backupDir, entryName, at.agentPath)
|
||||
if bErr != nil {
|
||||
ui.Warning("Failed to backup %s: %v", entryName, bErr)
|
||||
} else if bp != "" {
|
||||
if !backedUp {
|
||||
ui.Header("Backing up")
|
||||
backedUp = true
|
||||
}
|
||||
ui.Success("%s -> %s", entryName, bp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func syncTarget(name string, target config.TargetConfig, cfg *config.Config, dryRun, force bool) error {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"skillshare/internal/backup"
|
||||
"skillshare/internal/config"
|
||||
"skillshare/internal/resource"
|
||||
"skillshare/internal/sync"
|
||||
@@ -56,6 +57,25 @@ func syncAgentsGlobal(cfg *config.Config, dryRun, force, jsonOutput bool, start
|
||||
}
|
||||
}
|
||||
|
||||
// Backup agent targets before sync (non-dry-run only).
|
||||
if !dryRun && !jsonOutput {
|
||||
backupDir, agentTargets, _ := resolveGlobalAgentBackupContextFromCfg(cfg)
|
||||
backedUp := false
|
||||
for _, at := range agentTargets {
|
||||
entryName := at.name + "-agents"
|
||||
bp, bErr := backup.CreateInDir(backupDir, entryName, at.agentPath)
|
||||
if bErr != nil {
|
||||
ui.Warning("Failed to backup %s: %v", entryName, bErr)
|
||||
} else if bp != "" {
|
||||
if !backedUp {
|
||||
ui.Header("Backing up")
|
||||
backedUp = true
|
||||
}
|
||||
ui.Success("%s -> %s", entryName, bp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve agent-capable targets: user config agents sub-key + built-in defaults
|
||||
builtinAgents := config.DefaultAgentTargets()
|
||||
var totals agentSyncStats
|
||||
|
||||
@@ -257,6 +257,28 @@ func backupTargets(cfg *config.Config, toRemove []string) {
|
||||
ui.Success("%s -> %s", targetName, backupPath)
|
||||
}
|
||||
}
|
||||
|
||||
// Also backup agent directories for targets being removed.
|
||||
backupDir, agentTargets, err := resolveGlobalAgentBackupContextFromCfg(cfg)
|
||||
if err != nil || len(agentTargets) == 0 {
|
||||
return
|
||||
}
|
||||
removeSet := make(map[string]struct{}, len(toRemove))
|
||||
for _, name := range toRemove {
|
||||
removeSet[name] = struct{}{}
|
||||
}
|
||||
for _, at := range agentTargets {
|
||||
if _, ok := removeSet[at.name]; !ok {
|
||||
continue
|
||||
}
|
||||
entryName := at.name + "-agents"
|
||||
bp, bErr := backup.CreateInDir(backupDir, entryName, at.agentPath)
|
||||
if bErr != nil {
|
||||
ui.Warning("Failed to backup %s: %v", entryName, bErr)
|
||||
} else if bp != "" {
|
||||
ui.Success("%s -> %s", entryName, bp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// unlinkTarget unlinks a single target
|
||||
|
||||
Reference in New Issue
Block a user