From 2a80e2197b48056d51500631f72fa71c16a1a038 Mon Sep 17 00:00:00 2001 From: "yusuke.koyoshi" Date: Tue, 20 Aug 2024 15:48:36 +0900 Subject: [PATCH 1/3] Support FreeBSD --- dist_freebsd.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ os_bsd.go | 2 +- os_freebsd.go | 16 ++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 dist_freebsd.go diff --git a/dist_freebsd.go b/dist_freebsd.go new file mode 100644 index 0000000..964f484 --- /dev/null +++ b/dist_freebsd.go @@ -0,0 +1,55 @@ +package osutil + +import ( + "io/ioutil" + "strings" +) + +func getOSRelease() (map[string]string, bool) { + osmap := make(map[string]string) + + raw, err := ioutil.ReadFile("/etc/os-release") + + if err != nil { + return osmap, false + } + + s := string(raw) + + for _, line := range strings.Split(s, "\n") { + if line == "" { + break + } + + pair := strings.Split(line, "=") + k := pair[0] + v := pair[1] + + v = strings.Trim(v, "\"") + + osmap[k] = v + } + + return osmap, true +} + +func GetDist() Distro { + var detect string + var release string + + osmap, osMapExists := getOSRelease() + + if osMapExists { + name := osmap["NAME"] + if name != "" { + detect = name + } + + version := osmap["VERSION_ID"] + if version != "" { + release = version + } + } + + return Distro{detect, release, ""} +} diff --git a/os_bsd.go b/os_bsd.go index d7f0892..2e4eec8 100644 --- a/os_bsd.go +++ b/os_bsd.go @@ -1,4 +1,4 @@ -//+build freebsd,openbsd,dragonfly,netbsd +// +build openbsd,dragonfly,netbsd package osutil diff --git a/os_freebsd.go b/os_freebsd.go index b4c1186..d3bd553 100644 --- a/os_freebsd.go +++ b/os_freebsd.go @@ -1,3 +1,19 @@ package osutil +import "fmt" + const Name = "FreeBSD" + +func GetDisplay() string { + distro := GetDist() + + if distro.Display != "" { + return fmt.Sprintf("%s %s", distro.Display, distro.Release) + } + + return Name +} + +func GetVersion() string { + return "" +} From 4a901f2f98e03091ea8643913440a62e8482a4a2 Mon Sep 17 00:00:00 2001 From: "yusuke.koyoshi" Date: Mon, 30 Sep 2024 17:18:37 +0900 Subject: [PATCH 2/3] Fix FreeBSD version --- dist_freebsd.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/dist_freebsd.go b/dist_freebsd.go index 964f484..a002542 100644 --- a/dist_freebsd.go +++ b/dist_freebsd.go @@ -2,9 +2,21 @@ package osutil import ( "io/ioutil" + "os/exec" "strings" ) +func getFreeBSDVersion() (string, bool) { + proc := exec.Command("freebsd-version", "-u") + raw, err := proc.Output() + + if err != nil { + return "", false + } + + return string(raw), true +} + func getOSRelease() (map[string]string, bool) { osmap := make(map[string]string) @@ -37,6 +49,11 @@ func GetDist() Distro { var detect string var release string + version, freeBSDVersionExists := getFreeBSDVersion() + if freeBSDVersionExists { + return Distro{"FreeBSD", version, ""} + } + osmap, osMapExists := getOSRelease() if osMapExists { From 3e3d12602f62d4858c3cd5509ee963751e8a51b2 Mon Sep 17 00:00:00 2001 From: "yusuke.koyoshi" Date: Mon, 30 Sep 2024 17:31:15 +0900 Subject: [PATCH 3/3] Remove newline --- dist_freebsd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist_freebsd.go b/dist_freebsd.go index a002542..48780ca 100644 --- a/dist_freebsd.go +++ b/dist_freebsd.go @@ -14,7 +14,7 @@ func getFreeBSDVersion() (string, bool) { return "", false } - return string(raw), true + return strings.TrimRight(string(raw), "\n"), true } func getOSRelease() (map[string]string, bool) {