SDA-4479 - Backup and restore global config (#2099)

This commit is contained in:
Kiran Niranjan 2024-02-22 13:59:19 +05:30 committed by GitHub
parent 2ad44b0ef4
commit 27a8953326
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 77 additions and 23 deletions

View File

@ -11,7 +11,7 @@ describe('Plist Handler', () => {
betaAutoUpdateChannelEnabled: undefined,
bringToFront: undefined,
browserLoginAutoConnect: undefined,
chromeFlags: {
customFlags: {
authNegotiateDelegateWhitelist: undefined,
authServerWhitelist: undefined,
disableThrottling: undefined,
@ -49,6 +49,7 @@ describe('Plist Handler', () => {
url: undefined,
userDataPath: undefined,
whitelistUrl: undefined,
chromeFlags: undefined,
});
});
});

View File

@ -16,7 +16,11 @@ import {
SDAUserSessionActionTypes,
} from './bi/interface';
import { terminateC9Shell } from './c9-shell-handler';
import { getAllUserDefaults, initializePlistFile } from './plist-handler';
import {
getAllUserDefaults,
initializePlistFile,
setPlistFromPreviousSettings,
} from './plist-handler';
import { appStats } from './stats';
const writeFile = util.promisify(fs.writeFile);
@ -706,16 +710,24 @@ class Config {
* Creates a backup of the global config file
*/
public backupGlobalConfig() {
const installVariant = systemPreferences.getUserDefault(
'installVariant',
'string',
);
// If we already have a valid install variant in the plist
// we have already migrated the data form global config to plist
if (installVariant === '') {
fs.copyFileSync(this.globalConfigPath, this.tempGlobalConfigFilePath);
}
}
/**
* Overwrites the global config file with the backed up config file
*/
public copyGlobalConfig() {
public copyGlobalConfig(settings: IConfig) {
try {
if (fs.existsSync(this.tempGlobalConfigFilePath)) {
fs.copyFileSync(this.tempGlobalConfigFilePath, this.globalConfigPath);
if (settings) {
setPlistFromPreviousSettings(settings);
fs.unlinkSync(this.tempGlobalConfigFilePath);
}
} catch (e) {
@ -792,6 +804,29 @@ class Config {
*/
private readGlobalConfig() {
if (isMac) {
if (fs.existsSync(this.tempGlobalConfigFilePath)) {
this.globalConfig = this.parseConfigData(
fs.readFileSync(this.tempGlobalConfigFilePath, 'utf8'),
);
logger.info(
`config-handler: temp global config exists using this file: `,
this.tempGlobalConfigFilePath,
this.globalConfig,
);
this.copyGlobalConfig(this.globalConfig as IConfig);
return;
}
if (!this.installVariant || this.installVariant === '') {
logger.info(
`config-handler: Initializing new plist file: `,
this.installVariant,
);
initializePlistFile(this.postInstallScriptPath);
}
this.installVariant = systemPreferences.getUserDefault(
'installVariant',
'string',
);
this.globalConfig = getAllUserDefaults();
logger.info(
`config-handler: Global configuration from plist: `,
@ -822,17 +857,6 @@ class Config {
'installVariant',
'string',
);
if (!this.installVariant || this.installVariant === '') {
logger.info(
`config-handler: Initializing new plist file: `,
this.installVariant,
);
initializePlistFile(this.postInstallScriptPath);
this.installVariant = systemPreferences.getUserDefault(
'installVariant',
'string',
);
}
logger.info(
`config-handler: Install variant from plist: `,
this.installVariant,

View File

@ -1,6 +1,7 @@
import { execSync } from 'child_process';
import { systemPreferences } from 'electron';
import { logger } from '../common/logger';
import { getGuid } from '../common/utils';
import { IConfig } from './config-handler';
const GENERAL_SETTINGS = {
@ -40,7 +41,7 @@ const NOTIFICATION_SETTINGS = {
display: 'string',
};
const CHROME_FLAGS = {
const CUSTOM_FLAGS = {
authServerWhitelist: 'string',
authNegotiateDelegateWhitelist: 'string',
disableThrottling: 'string',
@ -74,13 +75,13 @@ export const getAllUserDefaults = (): IConfig => {
NOTIFICATION_SETTINGS[key],
);
});
Object.keys(CHROME_FLAGS).map((key) => {
if (!settings.chromeFlags) {
settings.chromeFlags = {};
Object.keys(CUSTOM_FLAGS).map((key) => {
if (!settings.customFlags) {
settings.customFlags = {};
}
settings.chromeFlags[key] = systemPreferences.getUserDefault(
settings.customFlags[key] = systemPreferences.getUserDefault(
key,
CHROME_FLAGS[key],
CUSTOM_FLAGS[key],
);
});
Object.keys(PERMISSIONS).map((key) => {
@ -96,6 +97,34 @@ export const getAllUserDefaults = (): IConfig => {
return settings;
};
export const setPlistFromPreviousSettings = (settings: IConfig) => {
Object.keys(GENERAL_SETTINGS).map((key) => {
systemPreferences.setUserDefault(key, GENERAL_SETTINGS[key], settings[key]);
});
Object.keys(NOTIFICATION_SETTINGS).map((key) => {
systemPreferences.setUserDefault(
key,
NOTIFICATION_SETTINGS[key],
settings.notificationSettings[key],
);
});
Object.keys(CUSTOM_FLAGS).map((key) => {
systemPreferences.setUserDefault(
key,
CUSTOM_FLAGS[key],
settings.customFlags[key],
);
});
Object.keys(PERMISSIONS).map((key) => {
systemPreferences.setUserDefault(
key,
PERMISSIONS[key],
settings.permissions[key],
);
});
systemPreferences.setUserDefault('installVariant', 'string', getGuid());
};
/**
* Initialize plist file
*/