[pbs-devel] [PATCH proxmox-backup 16/23] ui: utils: make parseMaintenanceMode more robust

Lukas Wagner l.wagner at proxmox.com
Tue Sep 19 15:38:32 CEST 2023


On 9/15/23 08:54, Hannes Laimer wrote:

> -    // 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];
> +	if (!mode) {
> +	    return [null, null];
> +	}
> +	return mode.split(',').reduce(([m, msg], pair) => {
> +	    const [key, value] = pair.split('=');
> +	    if (key === 'message') {
> +		return [m, value.replace(/^"(.*)"$/, '$1').replace(/\\"/g, '"')];
> +	    } else {
> +		return [value ?? key, msg];
> +	    }
> +	}, [null, null]);
>       },
>   
>       renderMaintenance: function(mode, activeTasks) {

I know, we don't really do this so far our code base, but for functions 
like these it would certainly be nice to document any function contracts 
(input format, function outputs, side effects).

It's quite hard to review the code for correctness if this information 
is not provided.

What do you think?

-- 
- Lukas





More information about the pbs-devel mailing list