Crate etc_os_release

Source
Expand description

A parser and data structures for the /etc/os-release file.

os-release file is used by systemd and other tools to store information about the operating system distribution.

The file is formatted as a list of environment-like shell-compatible variable assignments.

For more information, see os-release(5)

§Usage

Add this to your Cargo.toml:

[dependencies]
etc-os-release = "0.1.1"

§Examples

Open the os-release file and print the OS name and version:

use etc_os_release::OsRelease;

let os_release = OsRelease::open()?;
println!("{}-{}", os_release.id(), os_release.version_id().unwrap_or_default());

Parse a string containing the contents of the os-release file:

use std::str::FromStr;

use etc_os_release::OsRelease;

let os_release = OsRelease::from_str(r#"
NAME=Fedora
VERSION="32 (Workstation Edition)"
ID=fedora
VERSION_ID=32
PRETTY_NAME="Fedora 32 (Workstation Edition)"
ANSI_COLOR="0;38;2;60;110;180"
LOGO=fedora-logo-icon
CPE_NAME="cpe:/o:fedoraproject:fedora:32"
HOME_URL="https://fedoraproject.org/"
DOCUMENTATION_URL="https://docs.fedoraproject.org/en-US/fedora/f32/system-administrators-guide/"
SUPPORT_URL="https://fedoraproject.org/wiki/Communicating_and_getting_help"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_BUGZILLA_PRODUCT="Fedora"
REDHAT_BUGZILLA_PRODUCT_VERSION=32
REDHAT_SUPPORT_PRODUCT="Fedora"
REDHAT_SUPPORT_PRODUCT_VERSION=32
PRIVACY_POLICY_URL="https://fedoraproject.org/wiki/Legal:PrivacyPolicy"
VARIANT="Workstation Edition"
VARIANT_ID=workstation
"#).unwrap();

assert_eq!(os_release.id(), "fedora");
assert_eq!(os_release.version_id(), Some("32"));

Structs§

OsRelease
The parsed contents of the os-release file.
OsReleaseEntry
An entry in the os-release file.

Enums§

Error
Errors that can occur while parsing the os-release file.
OsReleaseLine
A line in the os-release file.