diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cbf18f0 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +fmt: + go fmt ./... + +lint: + golangci-lint run diff --git a/fastmirror.go b/fastmirror.go index f27284c..793ade0 100644 --- a/fastmirror.go +++ b/fastmirror.go @@ -7,7 +7,6 @@ import ( "github.com/schollz/progressbar/v3" "html/template" "io" - "io/ioutil" "log" "net/http" "os" @@ -126,7 +125,7 @@ func findSourcesFilePath() (string, error) { log.Printf("Found Ubuntu sources file: %s", sourcesListPath) return sourcesListPath, nil } - files, err := ioutil.ReadDir(sourcesListDPath) + files, err := os.ReadDir(sourcesListDPath) if err != nil { return "", fmt.Errorf("failed to read directory %s: %v", sourcesListDPath, err) } @@ -146,7 +145,7 @@ func findSourcesFilePath() (string, error) { } func isUbuntuSourcesFile(filePath string) bool { - content, err := ioutil.ReadFile(filePath) + content, err := os.ReadFile(filePath) if err != nil { log.Printf("Failed to read file %s: %v", filePath, err) return false @@ -213,7 +212,9 @@ func findFastestMirror() (string, time.Duration, []time.Duration, error) { bar := progressbar.Default(int64(len(mirrors))) for _, mirror := range mirrors { - bar.Add(1) + if err := bar.Add(1); err != nil { + log.Printf("Error updating progress bar: %v", err) + } if strings.HasPrefix(mirror, "https://") { mirror = strings.TrimSuffix(mirror, "/") startTime := time.Now() @@ -256,23 +257,44 @@ func isValidMirror(httpClient http.Client, mirrorURL, codename string) bool { } func extractSuites(filePath string) ([]string, error) { - content, err := ioutil.ReadFile(filePath) + content, err := os.ReadFile(filePath) if err != nil { return nil, fmt.Errorf("failed to read file %s: %v", filePath, err) } - // Extract suites from the sources file var suites []string scanner := bufio.NewScanner(strings.NewReader(string(content))) + + isModernFormat := false for scanner.Scan() { line := scanner.Text() - if strings.HasPrefix(line, "deb ") || strings.HasPrefix(line, "deb-src ") { - parts := strings.Fields(line) - if len(parts) >= 4 { - suites = append(suites, parts[3:]...) + if strings.HasPrefix(line, "Types:") { + isModernFormat = true + break + } + } + + scanner = bufio.NewScanner(strings.NewReader(string(content))) + if isModernFormat { + for scanner.Scan() { + line := scanner.Text() + if strings.HasPrefix(line, "Suites:") { + parts := strings.Fields(line) + suites = append(suites, parts[1:]...) + } + } + } else { + for scanner.Scan() { + line := scanner.Text() + if strings.HasPrefix(line, "deb ") || strings.HasPrefix(line, "deb-src ") { + parts := strings.Fields(line) + if len(parts) >= 4 { + suites = append(suites, parts[3]) + } } } } + if err := scanner.Err(); err != nil { return nil, fmt.Errorf("failed to scan file %s: %v", filePath, err) } @@ -354,7 +376,7 @@ func updateSourcesFile(filePath, mirrorURL string, suites []string) error { content = buf.String() } - err = ioutil.WriteFile(filePath, []byte(content), 0644) + err = os.WriteFile(filePath, []byte(content), 0644) if err != nil { return fmt.Errorf("failed to update sources file %s: %v", filePath, err) }