| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
import React, { useState } from "react"; |
| 3 |
import { createRoot } from 'react-dom/client'; |
| 4 |
|
| 5 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 6 |
import { Form, FormGroup } from "@patternfly/react-core/dist/esm/components/Form/index.js"; |
| 7 |
import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput/index.js"; |
| 8 |
import { Page } from "@patternfly/react-core/dist/esm/components/Page/index.js"; |
| 9 |
|
| 10 |
import cockpit from "cockpit"; |
| 11 |
import { WithDialogs, useDialogs } from "dialogs.jsx"; |
| 12 |
import { connect_host } from "cockpit-connect-ssh"; |
| 13 |
|
| 14 |
import 'page.scss'; |
| 15 |
|
| 16 |
const RemotePage = () => { |
| 17 |
const dialogs = useDialogs(); |
| 18 |
const [host, setHost] = useState("127.0.0.1"); |
| 19 |
const [user, setUser] = useState(""); |
| 20 |
const [command, setCommand] = useState("hostname"); |
| 21 |
const [output, setOutput] = useState(""); |
| 22 |
const [error, setError] = useState(""); |
| 23 |
|
| 24 |
const on_go = async () => { |
| 25 |
setOutput(""); |
| 26 |
setError(""); |
| 27 |
try { |
| 28 |
await connect_host(dialogs, host, user); |
| 29 |
const out = await cockpit.script(command, [], { host, user }); |
| 30 |
setOutput(out.trim()); |
| 31 |
} catch (ex) { |
| 32 |
setError(JSON.stringify(ex)); |
| 33 |
} |
| 34 |
}; |
| 35 |
|
| 36 |
return ( |
| 37 |
<Page className="pf-m-no-sidebar"> |
| 38 |
<Form isHorizontal> |
| 39 |
<FormGroup fieldId="host" label="Host" isRequired> |
| 40 |
<TextInput id="host" value={host} onChange={(_ev, value) => setHost(value)} isRequired /> |
| 41 |
</FormGroup> |
| 42 |
<FormGroup fieldId="user" label="User"> |
| 43 |
<TextInput id="user" value={user} onChange={(_ev, value) => setUser(value)} isRequired /> |
| 44 |
</FormGroup> |
| 45 |
<FormGroup fieldId="command" label="Command" isRequired> |
| 46 |
<TextInput id="command" value={command} onChange={(_ev, value) => setCommand(value)} isRequired /> |
| 47 |
</FormGroup> |
| 48 |
<Button onClick={on_go}>Go</Button> |
| 49 |
</Form> |
| 50 |
|
| 51 |
<label htmlFor="output" className="control-label">Output:</label> |
| 52 |
<pre id="output">{output}</pre> |
| 53 |
<label htmlFor="error" className="control-label">Error:</label> |
| 54 |
<span id="error">{error}</span> |
| 55 |
</Page> |
| 56 |
); |
| 57 |
}; |
| 58 |
|
| 59 |
document.addEventListener("DOMContentLoaded", () => { |
| 60 |
cockpit.translate(); |
| 61 |
const app = document.getElementById("app"); |
| 62 |
cockpit.assert(app); |
| 63 |
createRoot(app).render(<WithDialogs><RemotePage /></WithDialogs>); |
| 64 |
// signal tests that we are ready |
| 65 |
cockpit.transport.wait(() => true); |
| 66 |
}); |