latest, lints

This commit is contained in:
Jeffrey Paul 2024-05-22 09:55:46 -07:00
parent 1178e5adf7
commit 39eefcafee
2 changed files with 38 additions and 11 deletions

5
Makefile Normal file
View File

@ -0,0 +1,5 @@
fmt:
go fmt ./...
lint:
golangci-lint run

View File

@ -7,7 +7,6 @@ import (
"github.com/schollz/progressbar/v3" "github.com/schollz/progressbar/v3"
"html/template" "html/template"
"io" "io"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -126,7 +125,7 @@ func findSourcesFilePath() (string, error) {
log.Printf("Found Ubuntu sources file: %s", sourcesListPath) log.Printf("Found Ubuntu sources file: %s", sourcesListPath)
return sourcesListPath, nil return sourcesListPath, nil
} }
files, err := ioutil.ReadDir(sourcesListDPath) files, err := os.ReadDir(sourcesListDPath)
if err != nil { if err != nil {
return "", fmt.Errorf("failed to read directory %s: %v", sourcesListDPath, err) return "", fmt.Errorf("failed to read directory %s: %v", sourcesListDPath, err)
} }
@ -146,7 +145,7 @@ func findSourcesFilePath() (string, error) {
} }
func isUbuntuSourcesFile(filePath string) bool { func isUbuntuSourcesFile(filePath string) bool {
content, err := ioutil.ReadFile(filePath) content, err := os.ReadFile(filePath)
if err != nil { if err != nil {
log.Printf("Failed to read file %s: %v", filePath, err) log.Printf("Failed to read file %s: %v", filePath, err)
return false return false
@ -213,7 +212,9 @@ func findFastestMirror() (string, time.Duration, []time.Duration, error) {
bar := progressbar.Default(int64(len(mirrors))) bar := progressbar.Default(int64(len(mirrors)))
for _, mirror := range 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://") { if strings.HasPrefix(mirror, "https://") {
mirror = strings.TrimSuffix(mirror, "/") mirror = strings.TrimSuffix(mirror, "/")
startTime := time.Now() startTime := time.Now()
@ -256,23 +257,44 @@ func isValidMirror(httpClient http.Client, mirrorURL, codename string) bool {
} }
func extractSuites(filePath string) ([]string, error) { func extractSuites(filePath string) ([]string, error) {
content, err := ioutil.ReadFile(filePath) content, err := os.ReadFile(filePath)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read file %s: %v", filePath, err) return nil, fmt.Errorf("failed to read file %s: %v", filePath, err)
} }
// Extract suites from the sources file
var suites []string var suites []string
scanner := bufio.NewScanner(strings.NewReader(string(content))) scanner := bufio.NewScanner(strings.NewReader(string(content)))
isModernFormat := false
for scanner.Scan() {
line := scanner.Text()
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() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
if strings.HasPrefix(line, "deb ") || strings.HasPrefix(line, "deb-src ") { if strings.HasPrefix(line, "deb ") || strings.HasPrefix(line, "deb-src ") {
parts := strings.Fields(line) parts := strings.Fields(line)
if len(parts) >= 4 { if len(parts) >= 4 {
suites = append(suites, parts[3:]...) suites = append(suites, parts[3])
} }
} }
} }
}
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("failed to scan file %s: %v", filePath, err) 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() content = buf.String()
} }
err = ioutil.WriteFile(filePath, []byte(content), 0644) err = os.WriteFile(filePath, []byte(content), 0644)
if err != nil { if err != nil {
return fmt.Errorf("failed to update sources file %s: %v", filePath, err) return fmt.Errorf("failed to update sources file %s: %v", filePath, err)
} }