Skip to content

Commit

Permalink
Add os-release parser
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jan 15, 2019
1 parent f17ad8c commit bf690dd
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
55 changes: 55 additions & 0 deletions cmd/os_release.go
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()
}
72 changes: 72 additions & 0 deletions cmd/os_release_test.go
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)
}
}
}

0 comments on commit bf690dd

Please sign in to comment.