Skip to content

Commit 7bc274b

Browse files
committed
Refactor platform detection and version retrieval logic in downloader
1 parent 84047fb commit 7bc274b

File tree

1 file changed

+86
-54
lines changed

1 file changed

+86
-54
lines changed

cmd/download-libs/main.go

Lines changed: 86 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
"net/http"
1212
"os"
13+
"os/exec"
1314
"path/filepath"
1415
"runtime"
1516
"strings"
@@ -106,18 +107,9 @@ func detectPlatform() string {
106107
return ""
107108
}
108109

109-
// getPlatformWithFallback returns the preferred platform and a fallback if needed
110-
func getPlatformWithFallback() (string, string) {
111-
preferred := detectPlatform()
112-
113-
// Define fallbacks for platforms that might not be available in older releases
114-
fallbacks := map[string]string{
115-
"osx-arm64": "osx-x64", // Apple Silicon can run Intel binaries
116-
"linux-arm64": "linux-x64", // Some ARM64 systems can run x64 with emulation
117-
}
118-
119-
fallback := fallbacks[preferred]
120-
return preferred, fallback
110+
// getPlatform returns the platform identifier for the current system
111+
func getPlatform() string {
112+
return detectPlatform()
121113
}
122114

123115
func listVersions() {
@@ -194,36 +186,24 @@ func getDownloadURL(version string) (string, string, error) {
194186
}
195187

196188
func showCurrent(dest string) {
197-
preferred, fallback := getPlatformWithFallback()
198-
libPath := filepath.Join(dest, "rticonnextdds-connector", "lib", preferred)
189+
platform := getPlatform()
190+
libPath := filepath.Join(dest, "rticonnextdds-connector", "lib", platform)
199191

200192
fmt.Println("📋 Current Installation:")
201-
fmt.Printf(" Platform: %s", preferred)
202-
if fallback != "" {
203-
fmt.Printf(" (fallback: %s)", fallback)
204-
}
205-
fmt.Printf("\n")
193+
fmt.Printf(" Platform: %s\n", platform)
206194
fmt.Printf(" Library path: %s\n", libPath)
207195

208-
// Check preferred platform first
196+
// Check for version information
197+
version := getInstalledVersion(dest)
198+
if version != "" {
199+
fmt.Printf(" Version: %s\n", version)
200+
}
201+
202+
// Check if libraries exist
209203
if _, err := os.Stat(libPath); os.IsNotExist(err) {
210-
// Try fallback if preferred doesn't exist
211-
if fallback != "" {
212-
fallbackPath := filepath.Join(dest, "rticonnextdds-connector", "lib", fallback)
213-
if _, err := os.Stat(fallbackPath); err == nil {
214-
fmt.Printf(" Status: ✅ Libraries installed (using fallback %s)\n", fallback)
215-
fmt.Printf(" Fallback path: %s\n", fallbackPath)
216-
libPath = fallbackPath // Use fallback path for listing files
217-
} else {
218-
fmt.Printf(" Status: ❌ No libraries found\n")
219-
fmt.Printf(" Run: go run github.com/rticommunity/rticonnextdds-connector-go/cmd/download-libs@latest\n")
220-
return
221-
}
222-
} else {
223-
fmt.Printf(" Status: ❌ No libraries found\n")
224-
fmt.Printf(" Run: go run github.com/rticommunity/rticonnextdds-connector-go/cmd/download-libs@latest\n")
225-
return
226-
}
204+
fmt.Printf(" Status: ❌ No libraries found\n")
205+
fmt.Printf(" Run: go run github.com/rticommunity/rticonnextdds-connector-go/cmd/download-libs@latest\n")
206+
return
227207
} else {
228208
fmt.Printf(" Status: ✅ Libraries installed\n")
229209
}
@@ -251,8 +231,73 @@ func showCurrent(dest string) {
251231
}
252232
}
253233

234+
// getInstalledVersion attempts to detect the installed version of RTI Connector libraries
235+
func getInstalledVersion(dest string) string {
236+
return detectVersionFromLibraries(dest)
237+
} // detectVersionFromLibraries tries to determine version based on library characteristics
238+
func detectVersionFromLibraries(dest string) string {
239+
platform := getPlatform()
240+
libPath := filepath.Join(dest, "rticonnextdds-connector", "lib", platform)
241+
242+
// Check if libraries exist
243+
if _, err := os.Stat(libPath); os.IsNotExist(err) {
244+
return ""
245+
}
246+
247+
// Try to extract version from the connector library binary
248+
connectorLib := filepath.Join(libPath, "librtiddsconnector")
249+
250+
// Add appropriate file extension based on platform
251+
switch runtime.GOOS {
252+
case "linux":
253+
connectorLib += ".so"
254+
case "darwin":
255+
connectorLib += ".dylib"
256+
case "windows":
257+
connectorLib += ".dll"
258+
}
259+
260+
if version := extractVersionFromBinary(connectorLib); version != "" {
261+
return version
262+
}
263+
264+
return "unknown (installed before version tracking)"
265+
}
266+
267+
// extractVersionFromBinary attempts to extract version information from the connector library
268+
func extractVersionFromBinary(libPath string) string {
269+
// Check if file exists
270+
if _, err := os.Stat(libPath); os.IsNotExist(err) {
271+
return ""
272+
}
273+
274+
// Use strings command to extract version information
275+
cmd := exec.Command("strings", libPath)
276+
output, err := cmd.Output()
277+
if err != nil {
278+
return ""
279+
}
280+
281+
// Look for RTICONNECTOR_BUILD pattern in the output
282+
lines := strings.Split(string(output), "\n")
283+
for _, line := range lines {
284+
if strings.HasPrefix(line, "RTICONNECTOR_BUILD_") {
285+
// Extract version from line like "RTICONNECTOR_BUILD_7.6.0.0_20250912T000000Z_RTI_REL"
286+
// Remove the prefix and split by underscore
287+
withoutPrefix := strings.TrimPrefix(line, "RTICONNECTOR_BUILD_")
288+
parts := strings.Split(withoutPrefix, "_")
289+
if len(parts) >= 1 {
290+
version := parts[0] // This should be the version number like "7.6.0.0"
291+
return fmt.Sprintf("RTI Connext DDS %s", version)
292+
}
293+
}
294+
}
295+
296+
return ""
297+
}
298+
254299
func downloadLibraries(version, dest string, force bool) error {
255-
preferred, fallback := getPlatformWithFallback()
300+
platform := getPlatform()
256301
libDir := filepath.Join(dest, "rticonnextdds-connector")
257302

258303
// Check if libraries already exist
@@ -265,11 +310,7 @@ func downloadLibraries(version, dest string, force bool) error {
265310
}
266311

267312
fmt.Printf("🌐 Downloading RTI Connector %s...\n", version)
268-
fmt.Printf("📱 Target platform: %s", preferred)
269-
if fallback != "" {
270-
fmt.Printf(" (fallback available: %s)", fallback)
271-
}
272-
fmt.Printf("\n")
313+
fmt.Printf("📱 Target platform: %s\n", platform)
273314

274315
// Get the actual download URL from GitHub API
275316
downloadURL, archiveName, err := getDownloadURL(version)
@@ -373,17 +414,8 @@ func extractZip(src, dest, connectorDir string) error {
373414
}
374415

375416
func showSetupInstructions(dest string) {
376-
preferred, fallback := getPlatformWithFallback()
377-
libPath := filepath.Join(dest, "rticonnextdds-connector", "lib", preferred)
378-
379-
// Check if preferred platform exists, otherwise use fallback
380-
if _, err := os.Stat(libPath); os.IsNotExist(err) && fallback != "" {
381-
fallbackPath := filepath.Join(dest, "rticonnextdds-connector", "lib", fallback)
382-
if _, err := os.Stat(fallbackPath); err == nil {
383-
libPath = fallbackPath
384-
fmt.Printf("ℹ️ Using %s libraries (fallback for %s)\n\n", fallback, preferred)
385-
}
386-
}
417+
platform := getPlatform()
418+
libPath := filepath.Join(dest, "rticonnextdds-connector", "lib", platform)
387419

388420
fmt.Println("🔧 Setup Instructions:")
389421
fmt.Println("Add the following to your environment:")

0 commit comments

Comments
 (0)