| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
import cockpit from "cockpit"; |
| 3 |
|
| 4 |
import { Channel } from '../lib/cockpit/channel'; |
| 5 |
|
| 6 |
import '../lib/patternfly/patternfly-6-cockpit.scss'; |
| 7 |
import "../../node_modules/@patternfly/patternfly/components/Button/button.css"; |
| 8 |
import "../../node_modules/@patternfly/patternfly/components/Page/page.css"; |
| 9 |
|
| 10 |
document.addEventListener("DOMContentLoaded", () => { |
| 11 |
document.getElementById("hammer").addEventListener("click", e => e.target.setAttribute("hidden", "hidden")); |
| 12 |
|
| 13 |
document.querySelector(".cockpit-internal-reauthorize .pf-v6-c-button").addEventListener("click", () => { |
| 14 |
document.querySelector(".cockpit-internal-reauthorize span").textContent = "checking..."; |
| 15 |
cockpit.script("pkcheck --action-id org.freedesktop.policykit.exec --process $$ -u 2>&1", { superuser: "try" }) |
| 16 |
.stream(data => console.debug(data)) |
| 17 |
.then(() => { |
| 18 |
document.querySelector(".cockpit-internal-reauthorize span").textContent = "result: authorized"; |
| 19 |
}) |
| 20 |
.catch(() => { |
| 21 |
document.querySelector(".cockpit-internal-reauthorize span").textContent = "result: access-denied"; |
| 22 |
}); |
| 23 |
}); |
| 24 |
|
| 25 |
document.querySelector(".super-channel .pf-v6-c-button").addEventListener("click", () => { |
| 26 |
document.querySelector(".super-channel span").textContent = "checking..."; |
| 27 |
cockpit.spawn(["id"], { superuser: "require" }) |
| 28 |
.then(data => { |
| 29 |
console.log("done"); |
| 30 |
document.querySelector(".super-channel span").textContent = "result: " + data; |
| 31 |
}) |
| 32 |
.catch(ex => { |
| 33 |
console.log("fail"); |
| 34 |
document.querySelector(".super-channel span").textContent = "result: " + ex.problem; |
| 35 |
}); |
| 36 |
}); |
| 37 |
|
| 38 |
document.querySelector(".lock-channel .pf-v6-c-button").addEventListener("click", () => { |
| 39 |
document.querySelector(".lock-channel span").textContent = "locking..."; |
| 40 |
cockpit.spawn(["flock", "-o", "/tmp/playground-test-lock", "-c", "echo locked; sleep infinity"], |
| 41 |
{ superuser: "try", err: "message" }) |
| 42 |
.stream(data => { |
| 43 |
document.querySelector(".lock-channel span").textContent = data; |
| 44 |
}) |
| 45 |
.catch(ex => { |
| 46 |
document.querySelector(".lock-channel span").textContent = "failed: " + ex.toString(); |
| 47 |
}); |
| 48 |
}); |
| 49 |
|
| 50 |
function update_nav() { |
| 51 |
document.getElementById("nav").textContent = ''; |
| 52 |
const path = ["top"].concat(cockpit.location.path); |
| 53 |
const e_nav = document.getElementById("nav"); |
| 54 |
path.forEach((p, i) => { |
| 55 |
if (i < path.length - 1) { |
| 56 |
const e_link = document.createElement("a"); |
| 57 |
e_link.setAttribute("tabindex", "0"); |
| 58 |
e_link.textContent = p; |
| 59 |
e_link.addEventListener("click", () => cockpit.location.go(path.slice(1, i + 1))); |
| 60 |
e_nav.append(e_link, " >> "); |
| 61 |
} else { |
| 62 |
const e_span = document.createElement("span"); |
| 63 |
e_span.textContent = p; |
| 64 |
e_nav.appendChild(e_span); |
| 65 |
} |
| 66 |
}); |
| 67 |
} |
| 68 |
|
| 69 |
cockpit.addEventListener('locationchanged', update_nav); |
| 70 |
update_nav(); |
| 71 |
|
| 72 |
document.getElementById('go-down').addEventListener("click", () => { |
| 73 |
const len = cockpit.location.path.length; |
| 74 |
cockpit.location.go(cockpit.location.path.concat(len.toString()), { length: len.toString() }); |
| 75 |
}); |
| 76 |
|
| 77 |
const counter = cockpit.file("/tmp/counter", { syntax: JSON }); |
| 78 |
|
| 79 |
function normalize_counter(obj) { |
| 80 |
obj = obj || { }; |
| 81 |
obj.counter = obj.counter || 0; |
| 82 |
return obj; |
| 83 |
} |
| 84 |
|
| 85 |
function complain(error) { |
| 86 |
document.getElementById('file-error').textContent = error.toString(); |
| 87 |
} |
| 88 |
|
| 89 |
function changed(content, tag, error) { |
| 90 |
if (error) |
| 91 |
return complain(error); |
| 92 |
document.getElementById('file-content').textContent = normalize_counter(content).counter; |
| 93 |
document.getElementById('file-error').textContent = ""; |
| 94 |
} |
| 95 |
|
| 96 |
counter.watch(changed); |
| 97 |
|
| 98 |
document.getElementById('modify-file').addEventListener("click", () => { |
| 99 |
counter |
| 100 |
.modify(obj => { |
| 101 |
obj = normalize_counter(obj); |
| 102 |
obj.counter += 1; |
| 103 |
return obj; |
| 104 |
}) |
| 105 |
.catch(complain); |
| 106 |
}); |
| 107 |
|
| 108 |
function load_file() { |
| 109 |
cockpit.file("/tmp/counter").read() |
| 110 |
.then(content => { |
| 111 |
document.getElementById('edit-file').value = content; |
| 112 |
}); |
| 113 |
} |
| 114 |
|
| 115 |
function save_file() { |
| 116 |
cockpit.file("/tmp/counter").replace(document.getElementById('edit-file').value); |
| 117 |
} |
| 118 |
|
| 119 |
document.getElementById('load-file').addEventListener("click", load_file); |
| 120 |
document.getElementById('save-file').addEventListener("click", save_file); |
| 121 |
load_file(); |
| 122 |
|
| 123 |
document.getElementById('delete-file').addEventListener("click", () => cockpit.spawn(["rm", "-f", "/tmp/counter"])); |
| 124 |
|
| 125 |
document.body.removeAttribute("hidden"); |
| 126 |
|
| 127 |
function show_hidden() { |
| 128 |
document.getElementById("hidden").textContent = cockpit.hidden ? "hidden" : "visible"; |
| 129 |
} |
| 130 |
|
| 131 |
cockpit.user().then(info => { |
| 132 |
console.log(info); |
| 133 |
document.getElementById("user-info").textContent = JSON.stringify(info); |
| 134 |
}); |
| 135 |
|
| 136 |
cockpit.addEventListener("visibilitychange", show_hidden); |
| 137 |
show_hidden(); |
| 138 |
|
| 139 |
// HACK: The user/group/mode options are not part yet of the Cockpit File API so |
| 140 |
// we resort to creating our own channel here. We can't use the new `Channel` |
| 141 |
// API as importing it with cockpit leads to the wrong cockpit.Channel being |
| 142 |
// used instead of the new class. |
| 143 |
const replace = (filename, content, tag, attrs) => { |
| 144 |
const channel = new Channel({ payload: "fsreplace1", superuser: 'try', path: filename, tag, attrs }); |
| 145 |
channel.wait(); |
| 146 |
|
| 147 |
return new Promise((resolve, reject) => { |
| 148 |
channel.on('close', message => { |
| 149 |
if (message.problem) { |
| 150 |
reject(message); |
| 151 |
} else { |
| 152 |
resolve(); |
| 153 |
} |
| 154 |
}); |
| 155 |
|
| 156 |
channel.send_data(content); |
| 157 |
channel.send_control({ command: 'done' }); |
| 158 |
}); |
| 159 |
}; |
| 160 |
|
| 161 |
const fsreplace_btn = document.getElementById("fsreplace1-create"); |
| 162 |
const fsreplace_error = document.getElementById("fsreplace1-error"); |
| 163 |
fsreplace_btn.addEventListener("click", e => { |
| 164 |
fsreplace_btn.disabled = true; |
| 165 |
fsreplace_error.textContent = ''; |
| 166 |
const filename = document.getElementById("fsreplace1-filename").value; |
| 167 |
const content = document.getElementById("fsreplace1-content").value; |
| 168 |
const use_tag = document.getElementById("fsreplace1-use-tag").checked; |
| 169 |
const file = cockpit.file(filename, { superuser: "try" }); |
| 170 |
const attrs = { }; |
| 171 |
for (const field of ["user", "group", "mode"]) { |
| 172 |
const val = document.getElementById(`fsreplace1-${field}`).value; |
| 173 |
if (!val) |
| 174 |
continue; |
| 175 |
|
| 176 |
attrs[field] = val; |
| 177 |
} |
| 178 |
|
| 179 |
if ('mode' in attrs) |
| 180 |
attrs.mode = Number.parseInt(attrs.mode); |
| 181 |
|
| 182 |
file.read().then((_content, tag) => { |
| 183 |
replace(filename, content, use_tag ? tag : undefined, attrs).catch(exc => { |
| 184 |
fsreplace_error.textContent = cockpit.message(exc); |
| 185 |
}) |
| 186 |
.finally(() => { |
| 187 |
fsreplace_btn.disabled = false; |
| 188 |
}); |
| 189 |
}); |
| 190 |
}); |
| 191 |
}); |