commit 86dbbbf69f1fc2ef63612ef77bd112c4ba48a195 Author: sneak Date: Wed May 22 08:50:21 2024 -0700 initial diff --git a/cmd/fastmirror/main.go b/cmd/fastmirror/main.go new file mode 100644 index 0000000..ca8afcc --- /dev/null +++ b/cmd/fastmirror/main.go @@ -0,0 +1,5 @@ +package main + +func main() { + fastmirror.cliEntry() +} diff --git a/fastmirror.go b/fastmirror.go new file mode 100644 index 0000000..238fa99 --- /dev/null +++ b/fastmirror.go @@ -0,0 +1,99 @@ +package fastmirror + +import ( + "fmt" + "io/ioutil" + "log" + "os" + "os/exec" + "path/filepath" + "runtime" + "strings" +) + +func cliEntry() { + if runtime.GOOS != "linux" { + log.Fatal("This program is only for Linux") + } + if !isUbuntu() { + log.Fatal("This program is only for Ubuntu") + } + + fp := identifySourcesFile() + + fmt.Printf("Found sources file: %s\n", fp) + + architecture := runtime.GOARCH + switch architecture { + case "amd64", "386": + fmt.Println("Use archive.ubuntu.com for packages") + default: + fmt.Println("Use ports.ubuntu.com for packages") + } +} + +func getUbuntuCodename() string { + cmd := exec.Command("lsb_release", "-cs") + output, err := cmd.Output() + if err != nil { + log.Fatal("Failed to run lsb_release: %v\n", err) + } + codename := strings.TrimSpace(string(output)) + return codename +} + +func isUbuntu() bool { + cmd := exec.Command("lsb_release", "-is") + output, err := cmd.Output() + if err != nil { + log.Fatal("Failed to run lsb_release: %v\n", err) + } + + distro := strings.TrimSpace(string(output)) + if distro == "Ubuntu" { + return true + } else { + return false + } +} + +func identifySourcesFile() string { + const sourcesList = "/etc/apt/sources.list" + const sourcesListD = "/etc/apt/sources.list.d/" + if checkFileForUbuntu(sourcesList) { + fmt.Printf("Found Ubuntu sources file: %s\n", sourcesList) + return sourcesList + } + files, err := ioutil.ReadDir(sourcesListD) + if err != nil { + fmt.Printf("Failed to read directory %s: %v\n", sourcesListD, err) + os.Exit(1) + } + + for _, file := range files { + if file.IsDir() { + continue + } + + filePath := filepath.Join(sourcesListD, file.Name()) + if checkFileForUbuntu(filePath) { + fmt.Printf("Found Ubuntu sources file: %s\n", filePath) + return filePath + } + } + return "" +} + +func checkFileForUbuntu(filePath string) bool { + content, err := ioutil.ReadFile(filePath) + if err != nil { + fmt.Printf("Failed to read file %s: %v\n", filePath, err) + return false + } + + if strings.Contains(strings.ToLower(string(content)), "ubuntu.com") { + return true + } + + return false +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..191a6b2 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.eeqj.de/sneak/fastmirror + +go 1.22.2