Skip to content

Commit

Permalink
Extract more information from /etc/os-release (#42614)
Browse files Browse the repository at this point in the history
We will create a new package that installs teleport in the local system.
In order to do so, we'll need to read `/etc/os-release`.
This PR adds the required fields for detecting the distro if it is based
on a popuplar distro.
  • Loading branch information
marcoandredinis authored Jun 12, 2024
1 parent 62c3d51 commit d87c484
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
41 changes: 28 additions & 13 deletions lib/linux/os_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ import (

// OSRelease represents the information contained in the /etc/os-release file.
type OSRelease struct {
PrettyName string
Name string
VersionID string
Version string
ID string
PrettyName string
Name string
VersionID string
Version string
VersionCodename string
ID string
IDLike string
}

// ParseOSRelease reads the /etc/os-release contents.
Expand All @@ -54,24 +56,37 @@ func ParseOSReleaseFromReader(in io.Reader) (*OSRelease, error) {
scan := bufio.NewScanner(in)
for scan.Scan() {
line := scan.Text()

line = strings.TrimSpace(line) // Remove spaces from start and end.

if len(line) == 0 {
continue // Skip empty lines.
}

if line[0] == '#' {
continue // Skip comments.
}

vals := strings.Split(line, "=")
if len(vals) != 2 {
continue // Skip unexpected line
continue // Skip unexpected line.
}

key := vals[0]
val := strings.Trim(vals[1], `"'`)
key := strings.TrimSpace(vals[0])
val := strings.Trim(vals[1], `"' `)
m[key] = val
}
if err := scan.Err(); err != nil {
return nil, trace.Wrap(err)
}

return &OSRelease{
PrettyName: m["PRETTY_NAME"],
Name: m["NAME"],
VersionID: m["VERSION_ID"],
Version: m["VERSION"],
ID: m["ID"],
PrettyName: m["PRETTY_NAME"],
Name: m["NAME"],
VersionID: m["VERSION_ID"],
Version: m["VERSION"],
VersionCodename: m["VERSION_CODENAME"],
ID: m["ID"],
IDLike: m["ID_LIKE"],
}, nil
}
14 changes: 9 additions & 5 deletions lib/linux/os_release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-poli
UBUNTU_CODENAME=jammy
`),
want: &linux.OSRelease{
PrettyName: "Ubuntu 22.04.3 LTS",
Name: "Ubuntu",
VersionID: "22.04",
Version: "22.04.3 LTS (Jammy Jellyfish)",
ID: "ubuntu",
PrettyName: "Ubuntu 22.04.3 LTS",
Name: "Ubuntu",
VersionID: "22.04",
Version: "22.04.3 LTS (Jammy Jellyfish)",
ID: "ubuntu",
VersionCodename: "jammy",
IDLike: "debian",
},
},
{
Expand All @@ -69,6 +71,8 @@ not supposed to be here either
a=b=c
# a comment
VERSION=1.0
`),
want: &linux.OSRelease{
Expand Down

0 comments on commit d87c484

Please sign in to comment.