fastmirror/ubuntu.go

53 lines
1.3 KiB
Go

package fastmirror
import (
"fmt"
"os/exec"
"strconv"
"strings"
)
func getUbuntuCodename() (string, error) {
cmd := exec.Command("lsb_release", "-cs")
output, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("failed to run lsb_release: %v", err)
}
return strings.TrimSpace(string(output)), nil
}
func getUbuntuVersion() (int, int, error) {
cmd := exec.Command("lsb_release", "-rs")
output, err := cmd.Output()
if err != nil {
return 0, 0, fmt.Errorf("failed to run lsb_release: %v", err)
}
version := strings.TrimSpace(string(output))
parts := strings.Split(version, ".")
if len(parts) != 2 {
return 0, 0, fmt.Errorf("unexpected version format: %s", version)
}
majorVersion, err := strconv.Atoi(parts[0])
if err != nil {
return 0, 0, fmt.Errorf("failed to parse major version: %v", err)
}
minorVersion, err := strconv.Atoi(parts[1])
if err != nil {
return 0, 0, fmt.Errorf("failed to parse minor version: %v", err)
}
return majorVersion, minorVersion, nil
}
func isUbuntu() error {
cmd := exec.Command("lsb_release", "-is")
output, err := cmd.Output()
if err != nil {
return fmt.Errorf("failed to run lsb_release: %v", err)
}
if strings.TrimSpace(string(output)) != "Ubuntu" {
return fmt.Errorf("this program is only for Ubuntu")
}
return nil
}