|
| 1 | +package installer |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/databricks/cli/experimental/aitools/lib/agents" |
| 12 | + "github.com/databricks/cli/libs/cmdio" |
| 13 | + "github.com/databricks/cli/libs/log" |
| 14 | +) |
| 15 | + |
| 16 | +// UninstallSkills removes all installed skills, their symlinks, and the state file. |
| 17 | +func UninstallSkills(ctx context.Context) error { |
| 18 | + globalDir, err := GlobalSkillsDir(ctx) |
| 19 | + if err != nil { |
| 20 | + return err |
| 21 | + } |
| 22 | + |
| 23 | + state, err := LoadState(globalDir) |
| 24 | + if err != nil { |
| 25 | + return fmt.Errorf("failed to load install state: %w", err) |
| 26 | + } |
| 27 | + |
| 28 | + if state == nil { |
| 29 | + if hasLegacyInstall(ctx, globalDir) { |
| 30 | + return errors.New("found skills from a previous install without state tracking; run 'databricks experimental aitools install' first, then uninstall") |
| 31 | + } |
| 32 | + return errors.New("no skills installed") |
| 33 | + } |
| 34 | + |
| 35 | + skillCount := len(state.Skills) |
| 36 | + |
| 37 | + // Remove skill directories and symlinks for each skill in state. |
| 38 | + for name := range state.Skills { |
| 39 | + canonicalDir := filepath.Join(globalDir, name) |
| 40 | + |
| 41 | + // Remove symlinks from agent directories (only symlinks pointing to canonical dir). |
| 42 | + removeSymlinksFromAgents(ctx, name, canonicalDir) |
| 43 | + |
| 44 | + // Remove canonical skill directory. |
| 45 | + if err := os.RemoveAll(canonicalDir); err != nil { |
| 46 | + log.Warnf(ctx, "Failed to remove %s: %v", canonicalDir, err) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Clean up orphaned symlinks pointing into the canonical dir. |
| 51 | + cleanOrphanedSymlinks(ctx, globalDir) |
| 52 | + |
| 53 | + // Delete state file. |
| 54 | + stateFile := filepath.Join(globalDir, stateFileName) |
| 55 | + if err := os.Remove(stateFile); err != nil && !os.IsNotExist(err) { |
| 56 | + return fmt.Errorf("failed to remove state file: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + noun := "skills" |
| 60 | + if skillCount == 1 { |
| 61 | + noun = "skill" |
| 62 | + } |
| 63 | + cmdio.LogString(ctx, fmt.Sprintf("Uninstalled %d %s.", skillCount, noun)) |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +// removeSymlinksFromAgents removes a skill's symlink from all agent directories |
| 68 | +// in the registry, but only if the entry is a symlink pointing into canonicalDir. |
| 69 | +// Non-symlink directories are left untouched to avoid deleting user-managed content. |
| 70 | +func removeSymlinksFromAgents(ctx context.Context, skillName, canonicalDir string) { |
| 71 | + for i := range agents.Registry { |
| 72 | + agent := &agents.Registry[i] |
| 73 | + skillsDir, err := agent.SkillsDir(ctx) |
| 74 | + if err != nil { |
| 75 | + continue |
| 76 | + } |
| 77 | + |
| 78 | + destDir := filepath.Join(skillsDir, skillName) |
| 79 | + |
| 80 | + // Use Lstat to detect symlinks (Stat follows them). |
| 81 | + fi, err := os.Lstat(destDir) |
| 82 | + if os.IsNotExist(err) { |
| 83 | + continue |
| 84 | + } |
| 85 | + if err != nil { |
| 86 | + log.Warnf(ctx, "Failed to stat %s for %s: %v", destDir, agent.DisplayName, err) |
| 87 | + continue |
| 88 | + } |
| 89 | + |
| 90 | + if fi.Mode()&os.ModeSymlink == 0 { |
| 91 | + log.Debugf(ctx, "Skipping non-symlink %s for %s", destDir, agent.DisplayName) |
| 92 | + continue |
| 93 | + } |
| 94 | + |
| 95 | + target, err := os.Readlink(destDir) |
| 96 | + if err != nil { |
| 97 | + log.Warnf(ctx, "Failed to read symlink %s: %v", destDir, err) |
| 98 | + continue |
| 99 | + } |
| 100 | + |
| 101 | + // Only remove if the symlink points into our canonical dir. |
| 102 | + if !strings.HasPrefix(target, canonicalDir+string(os.PathSeparator)) && target != canonicalDir { |
| 103 | + log.Debugf(ctx, "Skipping symlink %s (points to %s, not %s)", destDir, target, canonicalDir) |
| 104 | + continue |
| 105 | + } |
| 106 | + |
| 107 | + if err := os.Remove(destDir); err != nil { |
| 108 | + log.Warnf(ctx, "Failed to remove symlink %s: %v", destDir, err) |
| 109 | + } else { |
| 110 | + log.Debugf(ctx, "Removed %q from %s", skillName, agent.DisplayName) |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// cleanOrphanedSymlinks scans all agent skill directories for symlinks pointing |
| 116 | +// into globalDir that are not tracked in state, and removes them. |
| 117 | +func cleanOrphanedSymlinks(ctx context.Context, globalDir string) { |
| 118 | + for i := range agents.Registry { |
| 119 | + agent := &agents.Registry[i] |
| 120 | + skillsDir, err := agent.SkillsDir(ctx) |
| 121 | + if err != nil { |
| 122 | + continue |
| 123 | + } |
| 124 | + |
| 125 | + entries, err := os.ReadDir(skillsDir) |
| 126 | + if err != nil { |
| 127 | + continue |
| 128 | + } |
| 129 | + |
| 130 | + for _, entry := range entries { |
| 131 | + entryPath := filepath.Join(skillsDir, entry.Name()) |
| 132 | + |
| 133 | + fi, err := os.Lstat(entryPath) |
| 134 | + if err != nil { |
| 135 | + continue |
| 136 | + } |
| 137 | + |
| 138 | + if fi.Mode()&os.ModeSymlink == 0 { |
| 139 | + continue |
| 140 | + } |
| 141 | + |
| 142 | + target, err := os.Readlink(entryPath) |
| 143 | + if err != nil { |
| 144 | + continue |
| 145 | + } |
| 146 | + |
| 147 | + // Check if the symlink points into our global skills dir. |
| 148 | + if !strings.HasPrefix(target, globalDir+string(os.PathSeparator)) && target != globalDir { |
| 149 | + continue |
| 150 | + } |
| 151 | + |
| 152 | + // This symlink points into our managed dir. Remove it. |
| 153 | + if err := os.Remove(entryPath); err != nil { |
| 154 | + log.Warnf(ctx, "Failed to remove orphaned symlink %s: %v", entryPath, err) |
| 155 | + } else { |
| 156 | + log.Debugf(ctx, "Removed orphaned symlink %s -> %s", entryPath, target) |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments