getOsRelease function

Future<OsReleaseInfo?> getOsRelease()

Parses /etc/os-release to extract the distro ID and ID_LIKE fields. ID_LIKE identifies the distro family (e.g. Ubuntu has ID_LIKE=debian). Returns null if the file is unreadable.

Implementation

Future<OsReleaseInfo?> getOsRelease() async {
  if (_osReleaseRead) return _cachedOsRelease;
  _osReleaseRead = true;
  try {
    final content = await File('/etc/os-release').readAsString();
    final idMatch = RegExp(
      r'^ID="?(\S+?)"?\s*$',
      multiLine: true,
    ).firstMatch(content);
    final idLikeMatch = RegExp(
      r'^ID_LIKE="?(.+?)"?\s*$',
      multiLine: true,
    ).firstMatch(content);
    _cachedOsRelease = OsReleaseInfo(
      id: idMatch?.group(1) ?? '',
      idLike: idLikeMatch?.group(1)?.split(' ') ?? [],
    );
    return _cachedOsRelease;
  } catch (_) {
    return null;
  }
}