forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
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 redhat-developer#3850
Fixes: redhat-developer#3850 Signed-off-by: Victor Rubezhny <[email protected]>
- Loading branch information
Showing
5 changed files
with
136 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/*----------------------------------------------------------------------------------------------- | ||
* 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 { Platform } from '../util/platform'; | ||
import { Registry } from './componentType'; | ||
|
||
|
||
type OdoSettings = { | ||
OdoSettings: { | ||
RegistryList: [{ | ||
Name: string; | ||
URL: string; | ||
secure: boolean; | ||
}]; | ||
} | ||
}; | ||
|
||
export class OdoPreference { | ||
private static instance: OdoPreference; | ||
|
||
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); | ||
} | ||
|
||
const userHomeDir = Platform.getUserHomePath(); | ||
return path.join(userHomeDir, '.odo', configFileName); | ||
} | ||
|
||
public async getRegistries(): Promise<Registry[]> { | ||
const odoPreference = await this.readOdoPreference(); | ||
const odoRegistryList = (odoPreference as OdoSettings)?.OdoSettings?.RegistryList; | ||
return odoRegistryList?.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); | ||
} | ||
|
||
public async readOdoPreference(): Promise<any> { | ||
const odoPreferenceFile = await fs.readFile(this.getOdoPreferenceFile(), 'utf8'); | ||
return YAML.load(odoPreferenceFile); | ||
} | ||
|
||
public async writeOdoPreference(preference: any): Promise<any> { | ||
const preferenceYaml = YAML.dump(preference); | ||
await fs.writeFile(this.getOdoPreferenceFile(), preferenceYaml, 'utf8'); | ||
} | ||
} |
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