-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(linux): 🐛 source distro information from /etc/os-release for regi…
…stration
- Loading branch information
Showing
4 changed files
with
108 additions
and
42 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
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
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
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,53 @@ | ||
// Copyright (c) 2024 Joshua Rich <[email protected]> | ||
// | ||
// This software is released under the MIT License. | ||
// https://opensource.org/licenses/MIT | ||
|
||
package whichdistro | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
) | ||
|
||
const ( | ||
OSReleaseFile = "/etc/os-release" | ||
OSReleaseAltFile = "/usr/lib/os-release" | ||
) | ||
|
||
// GetOSRelease will fetch the OS Release info from the canonical file | ||
// locations. The data will be formatted as a map[string]string. If the OS | ||
// Release info cannot be read, an error will be returned containing details of | ||
// why. | ||
func GetOSRelease() (map[string]string, error) { | ||
info := make(map[string]string) | ||
file, err := readOSRelease() | ||
if err != nil { | ||
return nil, err | ||
} | ||
lines := bytes.Split(file, []byte("\n")) | ||
for _, line := range lines { | ||
if bytes.Equal(line, []byte("")) { | ||
continue | ||
} | ||
fields := bytes.FieldsFunc(line, func(r rune) bool { | ||
return r == '=' | ||
}) | ||
info[string(fields[0])] = string(fields[1]) | ||
} | ||
return info, nil | ||
} | ||
|
||
func readOSRelease() ([]byte, error) { | ||
var contents []byte | ||
var err error | ||
contents, err = os.ReadFile(OSReleaseFile) | ||
if err == nil { | ||
return contents, nil | ||
} | ||
contents, err = os.ReadFile(OSReleaseAltFile) | ||
if err == nil { | ||
return contents, nil | ||
} | ||
return nil, err | ||
} |