initial
This commit is contained in:
		
						commit
						86dbbbf69f
					
				
							
								
								
									
										5
									
								
								cmd/fastmirror/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								cmd/fastmirror/main.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,5 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	fastmirror.cliEntry()
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										99
									
								
								fastmirror.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										99
									
								
								fastmirror.go
									
									
									
									
									
										Normal file
									
								
							@ -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
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user