forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathodoPreference.ts
More file actions
170 lines (146 loc) · 6.12 KB
/
odoPreference.ts
File metadata and controls
170 lines (146 loc) · 6.12 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import * as fs from 'fs/promises';
import * as path from 'path';
import { parse, stringify } from 'yaml';
import { TelemetryProps } from '../telemetry';
import { Platform } from '../util/platform';
import { YAML_STRINGIFY_OPTIONS } from '../util/utils';
import { Registry } from './componentType';
type OdoRegistryObject = {
Name: string;
URL: string;
secure: boolean;
};
type OdoSettingsObject = {
RegistryList: OdoRegistryObject[];
ConsentTelemetry;
};
type OdoPreferenceObject = {
kind: string,
apiversion: string,
OdoSettings: OdoSettingsObject;
};
export class OdoPreferenceError extends Error {
constructor(message: string, public readonly telemetryMessage = message, public readonly parent?, public readonly telemetryProps: TelemetryProps = {}) {
super(message);
Object.setPrototypeOf(this, new.target.prototype);
}
}
export class OdoPreference {
private static instance: OdoPreference;
public static DEFAULT_DEVFILE_REGISTRY_NAME: string = 'DefaultDevfileRegistry';
public static DEFAULT_DEVFILE_REGISTRY_URL: string = 'https://registry.devfile.io';
public static DEFAULT_DEVFILE_REGISTRY_SECURE: boolean = false;
private static DefaultOdoPreference: OdoPreferenceObject = {
kind: 'Preference',
apiversion: 'odo.dev/v1alpha1',
OdoSettings: {
RegistryList: [
{
Name: OdoPreference.DEFAULT_DEVFILE_REGISTRY_NAME,
URL: OdoPreference.DEFAULT_DEVFILE_REGISTRY_URL,
secure: OdoPreference.DEFAULT_DEVFILE_REGISTRY_SECURE
} as OdoRegistryObject
],
ConsentTelemetry: false
} as OdoSettingsObject
};
public static get Instance(): OdoPreference {
if (!OdoPreference.instance) {
OdoPreference.instance = new OdoPreference();
}
return OdoPreference.instance;
}
private getOdoPreferenceFile(): string {
// This value can be provided to set a seperate directory for users 'homedir' resolution
// note for mocking purpose ONLY
const customHomeDir = Platform.ENV.CUSTOM_HOMEDIR;
const configFileName = 'preference.yaml';
if (customHomeDir && customHomeDir.length > 0) {
return path.join(customHomeDir, '.odo', configFileName);
}
return path.join(Platform.getUserHomePath(), '.odo', configFileName);
}
public async getRegistries(): Promise<Registry[]> {
const odoPreference = await this.readOdoPreference();
return odoPreference.OdoSettings.RegistryList.map((r) => {
return {
name: r.Name,
url: r.URL,
secure: r.secure } as Registry;
});
}
public async addRegistry(name: string, url: string, token: string): Promise<Registry> {
const odoPreference = await this.readOdoPreference();
odoPreference.OdoSettings.RegistryList.push({
Name: name,
URL: url,
secure: !!token
} as never);
await this.writeOdoPreference(odoPreference);
return {
name,
secure: !!token,
url,
};
}
public async removeRegistry(name: string): Promise<void> {
const odoPreference = await this.readOdoPreference();
odoPreference.OdoSettings.RegistryList.splice(
odoPreference.OdoSettings.RegistryList.findIndex((registry) => registry.Name === name), 1
);
await this.writeOdoPreference(odoPreference);
}
private async readOdoPreference(): Promise<OdoPreferenceObject> {
let mergedPreference = OdoPreference.DefaultOdoPreference;
const odoPreferenceFilePath = this.getOdoPreferenceFile();
let isToBeReWritten: boolean = false;
try {
const odoPreferenceFile = await fs.readFile(odoPreferenceFilePath, 'utf8');
const odoPreferences = parse(odoPreferenceFile) as OdoPreferenceObject;
// If `odoPreferences.OdoSettings.RegistryList` is `null` or doesn't contain any registry item
// we should recover the 'DefaultDevfileRegistry` item on it
if (!odoPreferences.OdoSettings.RegistryList || odoPreferences.OdoSettings.RegistryList.length === 0) {
odoPreferences.OdoSettings.RegistryList = OdoPreference.DefaultOdoPreference.OdoSettings.RegistryList;
isToBeReWritten = true;
}
mergedPreference = { ...mergedPreference, ...odoPreferences };
} catch {
isToBeReWritten = true;
}
if(isToBeReWritten) {
await this.writeOdoPreference(mergedPreference);
}
return mergedPreference;
}
private async dirExists(path: string): Promise<boolean> {
try {
if ((await fs.stat(path)).isDirectory()) {
return true;
}
} catch {
// Ignore
}
return false;
}
private async writeOdoPreference(preference: any): Promise<any> {
const odoPreferenceFilePath = this.getOdoPreferenceFile();
try {
const preferenceYaml = stringify(preference, YAML_STRINGIFY_OPTIONS);
const odoPreferenceDir = path.parse(odoPreferenceFilePath).dir;
if (!await this.dirExists(odoPreferenceDir)) {
await fs.mkdir(odoPreferenceDir, { recursive: true, mode: 0o750} );
}
await fs.writeFile(this.getOdoPreferenceFile(), preferenceYaml, 'utf8');
} catch (err) {
throw new OdoPreferenceError(
`An error occured while creating the ODO Preference file at ${odoPreferenceFilePath}!`,
`Error when creating the ODO Preference file: ${odoPreferenceFilePath}`,
err
);
}
}
}