5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 07:29:07 +08:00
wails/cmd/linux.go
Mark Stenglein 1efc8cb934
CMD: LINUX: Arch Linux detection without lsb-release
The existing distribution detection does not work on Arch Linux
without the `lsb-release` package installed. This patch adds
detection using `/etc/os-release` in the same way that f9a1881
uses for Fedora and CentOS detection.

I changed the if statement that Bryn Sinclair used to a case
statement to avoid extra if-else-if statements.

I also needed to add a trim statement to remove the `"` characters
that are present in Arch Linux's `/etc/os-release` file.
2019-05-26 20:30:05 -04:00

127 lines
3.2 KiB
Go

package cmd
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
)
// LinuxDistribution is of type int
type LinuxDistribution int
const (
// Unknown is the catch-all distro
Unknown LinuxDistribution = iota
// Ubuntu distribution
Ubuntu
// Arch linux distribution
Arch
// RedHat linux distribution
RedHat
)
// DistroInfo contains all the information relating to a linux distribution
type DistroInfo struct {
Distribution LinuxDistribution
Description string
Release string
Codename string
DistributorID string
}
// GetLinuxDistroInfo returns information about the running linux distribution
func GetLinuxDistroInfo() *DistroInfo {
result := &DistroInfo{Distribution: Unknown}
program := NewProgramHelper()
// Does lsb_release exist?
lsbRelease := program.FindProgram("lsb_release")
if lsbRelease != nil {
stdout, _, _, err := lsbRelease.Run("-a")
if err != nil {
return result
}
for _, line := range strings.Split(stdout, "\n") {
if strings.Contains(line, ":") {
// Iterate lines a
details := strings.Split(line, ":")
key := strings.TrimSpace(details[0])
value := strings.TrimSpace(details[1])
switch key {
case "Distributor ID":
result.DistributorID = value
switch value {
case "Ubuntu":
result.Distribution = Ubuntu
case "Arch", "ManjaroLinux":
result.Distribution = Arch
}
case "Description":
result.Description = value
case "Release":
result.Release = value
case "Codename":
result.Codename = value
}
}
}
// check if /etc/os-release exists
} else if _, err := os.Stat("/etc/os-release"); !os.IsNotExist(err) {
// read /etc/os-release
osRelease, _ := ioutil.ReadFile("/etc/os-release")
// compile a regex to find NAME=distro
re := regexp.MustCompile(`^NAME=(.*)\n`)
// extract the distro name
osName := string(re.FindSubmatch(osRelease)[1])
// strip quotations
osName = strings.Trim(osName, "\"")
// Check distro name against list of distros
switch osName {
case "Fedora":
result.Distribution = RedHat
case "CentOS":
result.Distribution = RedHat
case "Arch Linux":
result.Distribution = Arch
}
}
return result
}
// DpkgInstalled uses dpkg to see if a package is installed
func DpkgInstalled(packageName string) (bool, error) {
program := NewProgramHelper()
dpkg := program.FindProgram("dpkg")
if dpkg == nil {
return false, fmt.Errorf("cannot check dependencies: dpkg not found")
}
_, _, exitCode, _ := dpkg.Run("-L", packageName)
return exitCode == 0, nil
}
// PacmanInstalled uses pacman to see if a package is installed.
func PacmanInstalled(packageName string) (bool, error) {
program := NewProgramHelper()
pacman := program.FindProgram("pacman")
if pacman == nil {
return false, fmt.Errorf("cannot check dependencies: pacman not found")
}
_, _, exitCode, _ := pacman.Run("-Qs", packageName)
return exitCode == 0, nil
}
// RpmInstalled uses rpm to see if a package is installed
func RpmInstalled(packageName string) (bool, error) {
program := NewProgramHelper()
rpm := program.FindProgram("rpm")
if rpm == nil {
return false, fmt.Errorf("cannot check dependencies: rpm not found")
}
_, _, exitCode, _ := rpm.Run("--query", packageName)
return exitCode == 0, nil
}