fastmirror/main.go

63 lines
1.4 KiB
Go

package fastmirror
import (
"log"
"runtime"
)
func CLIEntry() {
log.Println("Starting Fastmirror CLI")
if runtime.GOOS != "linux" {
log.Fatal("This program is only for Linux")
}
if err := isUbuntu(); err != nil {
log.Fatal(err)
}
sourcesFilePath, err := findSourcesFilePath()
if err != nil {
log.Fatal(err)
}
log.Printf("Found sources file: %s", sourcesFilePath)
// Extract suites from the existing sources file
suites, err := extractSuites(sourcesFilePath)
if err != nil {
log.Fatal(err)
}
log.Printf("Extracted suites: %v", suites)
// Create backup of the sources file
if err := createBackup(sourcesFilePath); err != nil {
log.Fatal(err)
}
oldMirrorURL, err := findMirrorURLInFile(sourcesFilePath)
if err != nil {
// redundant
log.Fatalf("Failed to find mirror URL in file %s: %v", sourcesFilePath, err)
}
// Fetch and find the fastest mirror
fastestMirrorURL, fastestTime, latencies, err := findFastestMirror(suites)
if err != nil {
log.Fatal(err)
}
log.Printf("Fastest mirror: %s", fastestMirrorURL)
// Display latency statistics
displayLatencyStatistics(latencies, fastestMirrorURL, fastestTime)
if err != nil {
log.Fatalf("Failed to find mirror URL in file %s: %v", sourcesFilePath, err)
}
// Update sources file with the fastest mirror
if err := updateSourcesFile(sourcesFilePath, oldMirrorURL, fastestMirrorURL); err != nil {
log.Fatal(err)
}
log.Println("Fastmirror CLI finished successfully")
}