|
1 | 1 | package update |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "os" |
| 6 | + "os/exec" |
5 | 7 | "path/filepath" |
6 | 8 | "strings" |
| 9 | + "syscall" |
7 | 10 | ) |
8 | 11 |
|
9 | 12 | type InstallMethod int |
@@ -87,3 +90,180 @@ func npmProjectDir(resolvedPath string) string { |
87 | 90 | } |
88 | 91 | return "" |
89 | 92 | } |
| 93 | + |
| 94 | +// InstallDetection represents a detected lstk installation. |
| 95 | +type InstallDetection struct { |
| 96 | + Path string |
| 97 | + Method InstallMethod |
| 98 | + Version string |
| 99 | + IsRunning bool |
| 100 | +} |
| 101 | + |
| 102 | +// DetectMultipleInstalls scans common installation paths for lstk binaries |
| 103 | +// and returns a list of all detected installations with their metadata. |
| 104 | +func DetectMultipleInstalls() []InstallDetection { |
| 105 | + var detections []InstallDetection |
| 106 | + candidates := collectCandidatePaths() |
| 107 | + |
| 108 | + // Track resolved inodes to avoid reporting symlinks to the same binary |
| 109 | + type inodeKey struct { |
| 110 | + dev, ino uint64 |
| 111 | + } |
| 112 | + seenInodes := make(map[inodeKey]string) |
| 113 | + |
| 114 | + runningExe, _ := os.Executable() |
| 115 | + runningResolved := "" |
| 116 | + if runningExe != "" { |
| 117 | + runningResolved, _ = filepath.EvalSymlinks(runningExe) |
| 118 | + if runningResolved == "" { |
| 119 | + runningResolved = runningExe |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + for _, path := range candidates { |
| 124 | + // Verify it's actually an lstk binary |
| 125 | + version, ok := getBinaryVersion(path) |
| 126 | + if !ok { |
| 127 | + continue |
| 128 | + } |
| 129 | + |
| 130 | + // Resolve symlinks and check inode |
| 131 | + resolved, err := filepath.EvalSymlinks(path) |
| 132 | + if err != nil { |
| 133 | + resolved = path |
| 134 | + } |
| 135 | + |
| 136 | + // Get inode to detect duplicates |
| 137 | + info, err := os.Stat(resolved) |
| 138 | + if err != nil { |
| 139 | + continue |
| 140 | + } |
| 141 | + |
| 142 | + // Skip if we've already seen this inode |
| 143 | + if stat, ok := info.Sys().(*syscall.Stat_t); ok { |
| 144 | + key := inodeKey{dev: uint64(stat.Dev), ino: uint64(stat.Ino)} |
| 145 | + if _, ok := seenInodes[key]; ok { |
| 146 | + continue |
| 147 | + } |
| 148 | + seenInodes[key] = resolved |
| 149 | + } |
| 150 | + |
| 151 | + method := classifyPath(resolved) |
| 152 | + isRunning := resolved == runningResolved |
| 153 | + |
| 154 | + detections = append(detections, InstallDetection{ |
| 155 | + Path: path, |
| 156 | + Method: method, |
| 157 | + Version: version, |
| 158 | + IsRunning: isRunning, |
| 159 | + }) |
| 160 | + } |
| 161 | + |
| 162 | + return detections |
| 163 | +} |
| 164 | + |
| 165 | +// syscallStat is a helper to extract syscall stat data cross-platform |
| 166 | +type syscallStat interface { |
| 167 | + Sys() any |
| 168 | +} |
| 169 | + |
| 170 | +// collectCandidatePaths returns a list of paths to check for lstk installations. |
| 171 | +func collectCandidatePaths() []string { |
| 172 | + var paths []string |
| 173 | + |
| 174 | + // Add paths from PATH environment variable |
| 175 | + pathDirs := filepath.SplitList(os.Getenv("PATH")) |
| 176 | + for _, dir := range pathDirs { |
| 177 | + if dir == "" { |
| 178 | + continue |
| 179 | + } |
| 180 | + paths = append(paths, filepath.Join(dir, "lstk")) |
| 181 | + } |
| 182 | + |
| 183 | + // Homebrew paths (macOS/Linux) |
| 184 | + homebrewPaths := []string{ |
| 185 | + "/opt/homebrew/bin/lstk", // Apple Silicon |
| 186 | + "/usr/local/bin/lstk", // Intel Mac / Linuxbrew |
| 187 | + "/home/linuxbrew/.linuxbrew/bin/lstk", // Linuxbrew |
| 188 | + } |
| 189 | + paths = append(paths, homebrewPaths...) |
| 190 | + |
| 191 | + // npm global paths |
| 192 | + npmPaths := getNPMGlobalPaths() |
| 193 | + paths = append(paths, npmPaths...) |
| 194 | + |
| 195 | + // Common binary directories |
| 196 | + commonBinPaths := []string{ |
| 197 | + filepath.Join(os.Getenv("HOME"), ".local", "bin", "lstk"), |
| 198 | + filepath.Join(os.Getenv("HOME"), "bin", "lstk"), |
| 199 | + } |
| 200 | + paths = append(paths, commonBinPaths...) |
| 201 | + |
| 202 | + return paths |
| 203 | +} |
| 204 | + |
| 205 | +// getNPMGlobalPaths returns npm global install paths based on npm config. |
| 206 | +func getNPMGlobalPaths() []string { |
| 207 | + var paths []string |
| 208 | + |
| 209 | + // Try to get npm prefix via command |
| 210 | + cmd := exec.Command("npm", "root", "-g") |
| 211 | + out, err := cmd.Output() |
| 212 | + if err == nil { |
| 213 | + prefix := strings.TrimSpace(string(out)) |
| 214 | + paths = append(paths, filepath.Join(prefix, ".bin", "lstk")) |
| 215 | + paths = append(paths, filepath.Join(prefix, "node_modules", "@localstack", "lstk", "bin", "lstk")) |
| 216 | + paths = append(paths, filepath.Join(prefix, "node_modules", "@localstack", "lstk_darwin_arm64", "lstk")) |
| 217 | + paths = append(paths, filepath.Join(prefix, "node_modules", "@localstack", "lstk_darwin_amd64", "lstk")) |
| 218 | + paths = append(paths, filepath.Join(prefix, "node_modules", "@localstack", "lstk_linux_amd64", "lstk")) |
| 219 | + paths = append(paths, filepath.Join(prefix, "node_modules", "@localstack", "lstk_windows_amd64", "lstk.exe")) |
| 220 | + } |
| 221 | + |
| 222 | + // Fallback to common npm global prefixes |
| 223 | + fallbackPrefixes := []string{ |
| 224 | + filepath.Join(os.Getenv("HOME"), ".local", "share", "mise", "installs"), |
| 225 | + filepath.Join(os.Getenv("HOME"), ".nvm", "versions"), |
| 226 | + "/usr/local", |
| 227 | + "/opt/homebrew", |
| 228 | + } |
| 229 | + for _, prefix := range fallbackPrefixes { |
| 230 | + if prefix == "" { |
| 231 | + continue |
| 232 | + } |
| 233 | + paths = append(paths, |
| 234 | + filepath.Join(prefix, "lib", "node_modules", "@localstack", "lstk", "bin", "lstk"), |
| 235 | + filepath.Join(prefix, "lib", "node_modules", "@localstack", "lstk", "lstk"), |
| 236 | + ) |
| 237 | + } |
| 238 | + |
| 239 | + return paths |
| 240 | +} |
| 241 | + |
| 242 | +// getBinaryVersion runs the binary with --version and parses the output. |
| 243 | +// Returns the version string and true if successful. |
| 244 | +func getBinaryVersion(path string) (string, bool) { |
| 245 | + if _, err := os.Stat(path); err != nil { |
| 246 | + return "", false |
| 247 | + } |
| 248 | + |
| 249 | + cmd := exec.Command(path, "--version") |
| 250 | + var stdout bytes.Buffer |
| 251 | + cmd.Stdout = &stdout |
| 252 | + cmd.Stderr = nil // discard stderr |
| 253 | + |
| 254 | + if err := cmd.Run(); err != nil { |
| 255 | + return "", false |
| 256 | + } |
| 257 | + |
| 258 | + version := strings.TrimSpace(stdout.String()) |
| 259 | + if version == "" { |
| 260 | + return "", false |
| 261 | + } |
| 262 | + |
| 263 | + // Basic validation - should look like a version |
| 264 | + if !strings.Contains(version, ".") && !strings.HasPrefix(version, "v") { |
| 265 | + return "", false |
| 266 | + } |
| 267 | + |
| 268 | + return version, true |
| 269 | +} |
0 commit comments