Skip to content

Commit

Permalink
feat: run generated commands directly
Browse files Browse the repository at this point in the history
  • Loading branch information
danleyb2 committed Oct 18, 2024
1 parent 1d60f19 commit a88d732
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
37 changes: 35 additions & 2 deletions docker/dd-extension/ui/src/components/ShowCommand.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from "react";
import React, { useState } from "react";
import { useDockerDesktopClient } from "../hooks/useDockerDesktopClient";
import Form from "react-bootstrap/Form";
import {
Row,
} from "react-bootstrap";
import Loader from "./Loader";

interface ShowCommandProps {
validated: boolean;
Expand All @@ -16,6 +17,8 @@ export default function ShowCommand({
command,
curlPort,
}: ShowCommandProps) {
const [isRunningCommand, setRunningCommand] = useState(false);

if (!validated) {
return null;
}
Expand All @@ -32,6 +35,28 @@ export default function ShowCommand({
});
}

function runCommand(e: any){
setRunningCommand(true);
// Generate list of run options
console.debug(command);
const cmd:any = command.match(/[^ ]+/g)?.slice(2);
// Run in the background
if (!cmd.includes('-d')){
cmd.unshift('-d')
}

ddClient.docker.cli.exec("run", cmd).then(async (result) => {
console.debug(result);
setRunningCommand(false);
ddClient.desktopUI.toast.success('Command Ran Successfully.');
await ddClient.desktopUI.navigate.viewContainer(result.stdout.trim())
}).catch(e => {
setRunningCommand(false);
console.error(e);
ddClient.desktopUI.toast.error('Failed Run Command.');
})
}

const curlCommand = curlPort ? (
<div>
<p className="card-title mt-3 mb-0" style={{ display: "inline-block" }}>
Expand All @@ -57,7 +82,15 @@ export default function ShowCommand({
<code className="card-text d-block">{command}</code>
<div className="mt-3">
<button
className="btn btn-sm btn-warning"
className="btn btn-sm btn-success mx-2"
style={{ borderRadius: 15 }}
type="button"
onClick={runCommand}
>
<Loader isLoading={isRunningCommand} /> Run
</button>
<button
className="btn btn-sm btn-warning mx-2"
style={{ borderRadius: 15 }}
type="button"
onClick={copyToClipboard}
Expand Down
7 changes: 3 additions & 4 deletions docker/dd-extension/ui/src/components/Snapshot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ export default function Snapshot() {
const [licenseKey, setLicenseKey] = useState('');
const [token, setToken] = useState('');
const [tokenValidated, setTokenValidated] = useState(false);

const [isLoading, setLoading] = useState(false);
const [command, setCommand] = useState<string>("");
const [curlPort, setCurlPort] = useState("");

const [command, setCommand] = useState<string>("");
const [curlPort, setCurlPort] = useState("8080");
const [dockerimage, setDockerimage] = useState('');

const [country, setCountry] = useState('Global');
const [architecture, setArchitecture] = useState('alpr');
const [restartPolicy, setRestartPolicy] = useState('no');
Expand Down Expand Up @@ -218,6 +216,7 @@ export default function Snapshot() {
label='No (Docker Default)'
id='rps1'
value='no'
checked={restartPolicy == 'no'}
onChange={handleInputChange}
/>
<Form.Check
Expand Down
8 changes: 6 additions & 2 deletions docker/dd-extension/ui/src/components/Stream.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default function Stream() {
const [license, setLicense] = useState<string>("");
const [tokenValidated, setTokenValidated] = useState(false);
const [isLoading, setLoading] = useState(false);
const [restartPolicy, setRestartPolicy] = useState('no');

const ddClient = useDockerDesktopClient();

Expand All @@ -41,6 +42,8 @@ export default function Stream() {
const { name, value } = e.target;
if (name == "license") {
setLicense(value);
} else if (name == "restart-policy") {
setRestartPolicy(value);
}
setTokenValidated(false);
};
Expand All @@ -62,8 +65,8 @@ export default function Stream() {
if (valid) {
// Pull image and update
ddClient.docker.cli.exec("pull", [STREAM_IMAGE]).then((result) => {
const autoBoot = data['restart-policy'] != 'no'
? " --restart " + data['restart-policy']
const autoBoot = restartPolicy != 'no'
? " --restart " + restartPolicy
: "--rm";
const command = `docker run ${autoBoot} -t -v ${data.streamPath}:/user-data/ -e LICENSE_KEY=${data.license} -e TOKEN=${data.token} ${STREAM_IMAGE}`;
setCommand(command);
Expand Down Expand Up @@ -150,6 +153,7 @@ export default function Stream() {
label='No (Docker Default)'
id='rp1'
value='no'
checked={restartPolicy == 'no'}
onChange={handleInputChange}
/>
<Form.Check
Expand Down

0 comments on commit a88d732

Please sign in to comment.