-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathfileSystemIdManager.ts
More file actions
70 lines (60 loc) · 2.15 KB
/
fileSystemIdManager.ts
File metadata and controls
70 lines (60 loc) · 2.15 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
68
69
70
import { IdProvider } from "../common/api/idProvider";
import * as os from 'os';
import * as fs from 'fs';
import * as path from 'path';
import { Logger } from '../common/utils/logger';
import { generateUUID } from "../common/utils/uuid";
let REDHAT_ANONYMOUS_UUID: string | undefined;
/**
* Service providing the Red Hat anonymous user id, read/stored from the `~/.redhat/anonymousId` file.
*/
export class FileSystemIdProvider implements IdProvider {
constructor(private redhatDir?: string) { }
async getRedHatUUID(): Promise<string> {
return this.loadRedHatUUID();
}
public loadRedHatUUID(uuidGenerator?: (source?: string) => string): string {
if (REDHAT_ANONYMOUS_UUID) {
return REDHAT_ANONYMOUS_UUID;
}
const redhatUUIDFilePath = this.getAnonymousIdFile();
try {
REDHAT_ANONYMOUS_UUID = this.readFile(redhatUUIDFilePath);
if (REDHAT_ANONYMOUS_UUID) {
Logger.log(`loaded Red Hat UUID: ${REDHAT_ANONYMOUS_UUID}`);
} else {
Logger.log('No Red Hat UUID found');
REDHAT_ANONYMOUS_UUID = uuidGenerator ? uuidGenerator() : generateUUID();
this.writeFile(redhatUUIDFilePath, REDHAT_ANONYMOUS_UUID);
Logger.log(`Written Red Hat UUID: ${REDHAT_ANONYMOUS_UUID} to ${redhatUUIDFilePath}`);
}
} catch (e: any) {
Logger.log('Failed to access Red Hat UUID: ' + e?.message);
}
return REDHAT_ANONYMOUS_UUID!;
}
public getAnonymousIdFile(): string {
const homedir = os.homedir();
if (!this.redhatDir) {
this.redhatDir = path.join(homedir, '.redhat');
}
return path.join(this.redhatDir, 'anonymousId');
}
public readFile(filePath: string): string | undefined {
let content: string | undefined;
if (fs.existsSync(filePath)) {
content = fs.readFileSync(filePath, { encoding: 'utf8' });
if (content) {
content = content.trim();
}
}
return content;
}
public writeFile(filePath: string, content: string) {
const parentDir = path.dirname(filePath);
if (!fs.existsSync(parentDir)) {
fs.mkdirSync(parentDir, { recursive: true });
}
fs.writeFileSync(filePath, content, { encoding: 'utf8' });
}
}