| 1 |
/* |
| 2 |
* Copyright (C) 2018 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import React from "react"; |
| 7 |
|
| 8 |
import cockpit from "cockpit"; |
| 9 |
|
| 10 |
export function fmt_to_fragments(format: string, ...args: React.ReactNode[]) { |
| 11 |
const fragments = format.split(/(\$[0-9]+)/g).map(part => { |
| 12 |
if (part[0] == "$") { |
| 13 |
return args[parseInt(part.slice(1))]; // placeholder, from `args` |
| 14 |
} else |
| 15 |
return part; // literal string content |
| 16 |
}); |
| 17 |
|
| 18 |
return React.createElement(React.Fragment, { }, ...fragments); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Checks if a JsonValue is a JsonObject, and acts as a type guard. |
| 23 |
* |
| 24 |
* This function produces correct results for any possible JsonValue, and also |
| 25 |
* for undefined. If you pass other types of values to this function it may |
| 26 |
* return an incorrect result (ie: it doesn't check deeply, so anything that |
| 27 |
* looks like a "simple object" will pass the check). |
| 28 |
*/ |
| 29 |
export function is_json_dict(value: cockpit.JsonValue | undefined): value is cockpit.JsonObject { |
| 30 |
return value?.constructor === Object; |
| 31 |
} |
| 32 |
|
| 33 |
function try_fields( |
| 34 |
dict: cockpit.JsonObject, fields: (string | undefined)[], def: cockpit.JsonValue |
| 35 |
): cockpit.JsonValue { |
| 36 |
for (const field of fields) |
| 37 |
if (field && field in dict) |
| 38 |
return dict[field]; |
| 39 |
return def; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Get an entry from a manifest's ".config[config_name]" field. |
| 44 |
* |
| 45 |
* This can either be a direct value, e.g. |
| 46 |
* |
| 47 |
* "config": { "color": "yellow" } |
| 48 |
* |
| 49 |
* Or an object indexed by any value in "matches". Commonly these are fields |
| 50 |
* from os-release(5) like PLATFORM_ID or ID; e.g. |
| 51 |
* |
| 52 |
* "config": { |
| 53 |
* "fedora": { "color": "blue" }, |
| 54 |
* "platform:el9": { "color": "red" } |
| 55 |
* } |
| 56 |
*/ |
| 57 |
|
| 58 |
/** Parse `sessionStorage.cockpit_anaconda` as JSON (storaged uses the object when present). */ |
| 59 |
export function read_anaconda_session_storage(): cockpit.JsonValue | null { |
| 60 |
try { |
| 61 |
const value = JSON.parse( |
| 62 |
window.sessionStorage.getItem("cockpit_anaconda") as string |
| 63 |
) as cockpit.JsonValue; |
| 64 |
if (value) |
| 65 |
console.log("ANACONDA", value); |
| 66 |
return value; |
| 67 |
} catch { |
| 68 |
console.warn("Can't parse cockpit_anaconda configuration as JSON"); |
| 69 |
return null; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** True when embedded in Anaconda (parent sets JSON in sessionStorage `cockpit_anaconda`). */ |
| 74 |
export function in_anaconda_mode(): boolean { |
| 75 |
try { |
| 76 |
const raw = window.sessionStorage.getItem("cockpit_anaconda"); |
| 77 |
return !!JSON.parse(raw ?? "null"); |
| 78 |
} catch { |
| 79 |
return false; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
export function get_manifest_config_matchlist( |
| 84 |
manifest_name: string, config_name: string, default_value: cockpit.JsonValue, matches: (string | undefined)[] |
| 85 |
): cockpit.JsonValue { |
| 86 |
const config = cockpit.manifests[manifest_name]?.config; |
| 87 |
|
| 88 |
if (is_json_dict(config)) { |
| 89 |
const val = config[config_name]; |
| 90 |
if (is_json_dict(val)) |
| 91 |
return try_fields(val, matches, default_value); |
| 92 |
else |
| 93 |
return val !== undefined ? val : default_value; |
| 94 |
} else { |
| 95 |
return default_value; |
| 96 |
} |
| 97 |
} |