-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
"Components" sidebar section take a while to load #3850
Fixes: #3850 Signed-off-by: Victor Rubezhny <[email protected]>
- Loading branch information
Showing
11 changed files
with
305 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/*----------------------------------------------------------------------------------------------- | ||
* 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 YAML from 'js-yaml'; | ||
import * as path from 'path'; | ||
import { TelemetryProps } from '../telemetry'; | ||
import { Platform } from '../util/platform'; | ||
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 = YAML.load(odoPreferenceFile) as OdoPreferenceObject; | ||
|
||
// If `odoPreferences.OdoSettings.RegistryList` is `null` or doesn't contain the `DefaultDevfileRegistry` item | ||
// we have to recover at least the 'DefaultDevfileRegistry` item on it because this whole list will be replaced | ||
if (!odoPreferences.OdoSettings.RegistryList) { | ||
odoPreferences.OdoSettings.RegistryList = OdoPreference.DefaultOdoPreference.OdoSettings.RegistryList; | ||
isToBeReWritten = true; | ||
} else { | ||
if (!odoPreferences.OdoSettings.RegistryList.find((r) => r.Name === OdoPreference.DEFAULT_DEVFILE_REGISTRY_NAME)) { | ||
odoPreferences.OdoSettings.RegistryList.push(OdoPreference.DefaultOdoPreference.OdoSettings.RegistryList[0]); | ||
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 = YAML.dump(preference); | ||
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 | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.