-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package cmd | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
vfs "github.com/twpayne/go-vfs" | ||
) | ||
|
||
// getOSRelease returns the operating system identification data as defined by | ||
// https://www.freedesktop.org/software/systemd/man/os-release.html. | ||
func getOSRelease(fs vfs.FS) (map[string]string, error) { | ||
for _, filename := range []string{"/usr/lib/os-release", "/etc/os-release"} { | ||
f, err := fs.Open(filename) | ||
if os.IsNotExist(err) { | ||
continue | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
return parseOSRelease(f) | ||
} | ||
return nil, os.ErrNotExist | ||
} | ||
|
||
// maybeUnquote removes quotation marks around s. | ||
func maybeUnquote(s string) string { | ||
// Try to unquote. | ||
if s, err := strconv.Unquote(s); err == nil { | ||
return s | ||
} | ||
// Otherwise return s, unchanged. | ||
return s | ||
} | ||
|
||
// parseOSRelease parses operating system identification data from r as defined | ||
// by https://www.freedesktop.org/software/systemd/man/os-release.html. | ||
func parseOSRelease(r io.Reader) (map[string]string, error) { | ||
result := make(map[string]string) | ||
s := bufio.NewScanner(r) | ||
for s.Scan() { | ||
fields := strings.SplitN(s.Text(), "=", 2) | ||
if len(fields) != 2 { | ||
return nil, fmt.Errorf("cannot parse %q", s.Text()) | ||
} | ||
key := fields[0] | ||
value := maybeUnquote(fields[1]) | ||
result[key] = value | ||
} | ||
return result, s.Err() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/d4l3k/messagediff" | ||
) | ||
|
||
func TestParseOSRelease(t *testing.T) { | ||
for _, tc := range []struct { | ||
s string | ||
want map[string]string | ||
}{ | ||
{ | ||
s: `NAME=Fedora | ||
VERSION="17 (Beefy Miracle)" | ||
ID=fedora | ||
VERSION_ID=17 | ||
PRETTY_NAME="Fedora 17 (Beefy Miracle)" | ||
ANSI_COLOR="0;34" | ||
CPE_NAME="cpe:/o:fedoraproject:fedora:17" | ||
HOME_URL="https://fedoraproject.org/" | ||
BUG_REPORT_URL="https://bugzilla.redhat.com/"`, | ||
want: map[string]string{ | ||
"NAME": "Fedora", | ||
"VERSION": "17 (Beefy Miracle)", | ||
"ID": "fedora", | ||
"VERSION_ID": "17", | ||
"PRETTY_NAME": "Fedora 17 (Beefy Miracle)", | ||
"ANSI_COLOR": "0;34", | ||
"CPE_NAME": "cpe:/o:fedoraproject:fedora:17", | ||
"HOME_URL": "https://fedoraproject.org/", | ||
"BUG_REPORT_URL": "https://bugzilla.redhat.com/", | ||
}, | ||
}, | ||
{ | ||
s: `NAME="Ubuntu" | ||
VERSION="18.04.1 LTS (Bionic Beaver)" | ||
ID=ubuntu | ||
ID_LIKE=debian | ||
PRETTY_NAME="Ubuntu 18.04.1 LTS" | ||
VERSION_ID="18.04" | ||
HOME_URL="https://www.ubuntu.com/" | ||
SUPPORT_URL="https://help.ubuntu.com/" | ||
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" | ||
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" | ||
VERSION_CODENAME=bionic | ||
UBUNTU_CODENAME=bionic`, | ||
want: map[string]string{ | ||
"NAME": "Ubuntu", | ||
"VERSION": "18.04.1 LTS (Bionic Beaver)", | ||
"ID": "ubuntu", | ||
"ID_LIKE": "debian", | ||
"PRETTY_NAME": "Ubuntu 18.04.1 LTS", | ||
"VERSION_ID": "18.04", | ||
"HOME_URL": "https://www.ubuntu.com/", | ||
"SUPPORT_URL": "https://help.ubuntu.com/", | ||
"BUG_REPORT_URL": "https://bugs.launchpad.net/ubuntu/", | ||
"PRIVACY_POLICY_URL": "https://www.ubuntu.com/legal/terms-and-policies/privacy-policy", | ||
"VERSION_CODENAME": "bionic", | ||
"UBUNTU_CODENAME": "bionic", | ||
}, | ||
}, | ||
} { | ||
got, gotErr := parseOSRelease(bytes.NewBufferString(tc.s)) | ||
diff, equal := messagediff.PrettyDiff(tc.want, got) | ||
if gotErr != nil || !equal { | ||
t.Errorf("parseOSRelease(bytes.NewBufferString(%q)) == %+v, %v, want %+v, <nil>\n%s", tc.s, got, gotErr, tc.want, diff) | ||
} | ||
} | ||
} |