[pbs-devel] [PATCH proxmox-backup 13/26] ui: utils: remove parseMaintenanceMode function

Hannes Laimer h.laimer at proxmox.com
Tue Jul 5 15:08:21 CEST 2022


... and use parsePropertyString instead + remove quotes from quoted
values in parsePropertyString.

Signed-off-by: Hannes Laimer <h.laimer at proxmox.com>
---
 www/NavigationTree.js            |  8 ++++----
 www/Utils.js                     | 24 +++++++-----------------
 www/datastore/Summary.js         |  6 +++---
 www/window/MaintenanceOptions.js |  6 +++---
 4 files changed, 17 insertions(+), 27 deletions(-)

diff --git a/www/NavigationTree.js b/www/NavigationTree.js
index 5f44aace..aae35012 100644
--- a/www/NavigationTree.js
+++ b/www/NavigationTree.js
@@ -255,10 +255,10 @@ Ext.define('PBS.view.main.NavigationTree', {
 		}
 
 		let [qtip, iconCls] = ['', 'fa fa-database'];
-		const maintenance = records[i].data.maintenance;
-		if (maintenance) {
-		    const [type, message] = PBS.Utils.parseMaintenanceMode(maintenance);
-		    qtip = `${type}${message ? ': ' + message : ''}`;
+		const maintenanceString = records[i].data.maintenance;
+		if (maintenanceString) {
+		    const maintenance = PBS.Utils.parsePropertyString(maintenanceString, 'type');
+		    qtip = `${maintenance.type}${maintenance.message ? ': ' + maintenance.message : ''}`;
 		    iconCls = 'fa fa-database pmx-tree-icon-custom maintenance';
 		}
 
diff --git a/www/Utils.js b/www/Utils.js
index ad451c9f..a1f17bdc 100644
--- a/www/Utils.js
+++ b/www/Utils.js
@@ -73,13 +73,13 @@ Ext.define('PBS.Utils', {
 	Ext.Array.each(value.split(','), function(p) {
 	    var kv = p.split('=', 2);
 	    if (Ext.isDefined(kv[1])) {
-		res[kv[0]] = kv[1];
+		res[kv[0]] = kv[1].replace(/^"(.*)"$/, '$1');
 	    } else if (Ext.isDefined(defaultKey)) {
 		if (Ext.isDefined(res[defaultKey])) {
 		    error = 'defaultKey may be only defined once in propertyString';
 		    return false; // break
 		}
-		res[defaultKey] = kv[0];
+		res[defaultKey] = kv[0].replace(/^"(.*)"$/, '$1');
 	    } else {
 		error = 'invalid propertyString, not a key=value pair and no defaultKey defined';
 		return false; // break
@@ -656,27 +656,17 @@ Ext.define('PBS.Utils', {
 	return `${icon} ${value}`;
     },
 
-    // FIXME: this "parser" is brittle and relies on the order the arguments will appear in
-    parseMaintenanceMode: function(mode) {
-	let [type, message] = mode.split(/,(.+)/);
-	type = type.split("=").pop();
-	message = message ? message.split("=")[1]
-	    .replace(/^"(.*)"$/, '$1')
-	    .replaceAll('\\"', '"') : null;
-	return [type, message];
-    },
-
     renderMaintenance: function(mode, activeTasks) {
 	if (!mode) {
 	    return gettext('None');
 	}
 
-	let [type, message] = PBS.Utils.parseMaintenanceMode(mode);
+	const maintenance = PBS.Utils.parsePropertyString(mode, 'type');
 
 	let extra = '';
 
 	if (activeTasks !== undefined) {
-	    const conflictingTasks = activeTasks.write + (type === 'offline' ? activeTasks.read : 0);
+	    const conflictingTasks = activeTasks.write + (maintenance.type === 'offline' ? activeTasks.read : 0);
 
 	    if (conflictingTasks > 0) {
 		extra += '| <i class="fa fa-spinner fa-pulse fa-fw"></i> ';
@@ -686,12 +676,12 @@ Ext.define('PBS.Utils', {
 	    }
 	}
 
-	if (message) {
-	    extra += ` ("${message}")`;
+	if (maintenance.message) {
+	    extra += ` ("${maintenance.message}")`;
 	}
 
 	let modeText = Proxmox.Utils.unknownText;
-	switch (type) {
+	switch (maintenance.type) {
 	    case 'read-only': modeText = gettext("Read-only");
 		break;
 	    case 'offline': modeText = gettext("Offline");
diff --git a/www/datastore/Summary.js b/www/datastore/Summary.js
index 94be9559..6e73e9ad 100644
--- a/www/datastore/Summary.js
+++ b/www/datastore/Summary.js
@@ -49,10 +49,10 @@ Ext.define('PBS.DataStoreInfo', {
 		    success: function(response) {
 			const config = response.result.data;
 			if (config['maintenance-mode']) {
-			    const [_type, msg] = PBS.Utils.parseMaintenanceMode(config['maintenance-mode']);
+			    const maintenance = PBS.Utils.parsePropertyString(config['maintenance-mode'], 'type');
 			    me.view.el.mask(
-				`${gettext('Datastore is in maintenance mode')}${msg ? ': ' + msg : ''}`,
-				'fa pbs-maintenance-mask',
+			        `${gettext('Datastore is in maintenance mode')}${maintenance.message ? ': ' + maintenance.message : ''}`,
+			        'fa pbs-maintenance-mask',
 			    );
 			} else {
 			    me.view.el.mask(gettext('Datastore is not available'));
diff --git a/www/window/MaintenanceOptions.js b/www/window/MaintenanceOptions.js
index 1ee92542..3cd3e2ce 100644
--- a/www/window/MaintenanceOptions.js
+++ b/www/window/MaintenanceOptions.js
@@ -73,10 +73,10 @@ Ext.define('PBS.window.MaintenanceOptions', {
 	    'maintenance-msg': '',
 	};
 	if (values['maintenance-mode']) {
-	    const [type, message] = PBS.Utils.parseMaintenanceMode(values['maintenance-mode']);
+	    const maintenance = PBS.Utils.parsePropertyString(values['maintenance-mode'], 'type');
 	    options = {
-		'maintenance-type': type,
-		'maintenance-msg': message ?? '',
+		'maintenance-type': maintenance.type,
+		'maintenance-msg': maintenance.message ?? '',
 	    };
 	}
 
-- 
2.30.2






More information about the pbs-devel mailing list