forked from microsoft/vscode-cpptools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinuxDistribution.ts
More file actions
67 lines (58 loc) · 2.64 KB
/
Copy pathlinuxDistribution.ts
File metadata and controls
67 lines (58 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* See 'LICENSE' in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import * as fs from 'fs';
import * as os from 'os';
export class LinuxDistribution {
constructor(public name, public version) { }
/**
* There is no standard way on Linux to find the distribution name and version.
* Recently, systemd has pushed to standardize the os-release file. This has
* seen adoption in "recent" versions of all major distributions.
* https://www.freedesktop.org/software/systemd/man/os-release.html
*/
public static GetDistroInformation(): Promise<LinuxDistribution> {
let linuxDistro: Promise<LinuxDistribution>;
// First check /etc/os-release and only fallback to /usr/lib/os-release
// as per the os-release documentation.
linuxDistro = LinuxDistribution.getDistroInformationFromFile('/etc/os-release')
.catch(() => {
return LinuxDistribution.getDistroInformationFromFile('/usr/lib/os-release');
}).catch(() => {
// couldn't get distro information
return Promise.resolve(new LinuxDistribution('unknown', 'unknown'));
});
return linuxDistro;
}
private static getDistroInformationFromFile(path: string): Promise<LinuxDistribution> {
return new Promise<LinuxDistribution>((resolve, reject) => {
fs.readFile(path, 'utf8', (error, data) => {
if (error) {
reject(error);
return;
}
resolve(LinuxDistribution.getDistroInformation(data));
});
});
}
// Only public for tests.
public static getDistroInformation(data: string): LinuxDistribution {
const idKey: string = 'ID';
const versionKey: string = 'VERSION_ID';
let distroName: string = 'unknown';
let distroVersion: string = 'unknown';
let keyValues: string[] = data.split(os.EOL);
for (let i: number = 0; i < keyValues.length; i++) {
let keyValue: string[] = keyValues[i].split('=');
if (keyValue.length === 2) {
if (keyValue[0] === idKey) {
distroName = keyValue[1];
} else if (keyValue[0] === versionKey) {
distroVersion = keyValue[1];
}
}
}
return new LinuxDistribution(distroName, distroVersion);
}
}