-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add execute command action with payload (#156)
* - Add action to execute command, optionally passing the event data to the command as a JSON string by replacing the "{event}" string. - Include event data on emitEvent - Add .idea folder to .gitignore * update run command * rename command to shell command * rename run command to shell command * cleanup --------- Co-authored-by: Vince Au <vinceau09@gmail.com>
- Loading branch information
1 parent
e5e477e
commit 9f7a9dc
Showing
5 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,3 +30,6 @@ yarn-error.log* | |
# yalc | ||
.yalc | ||
yalc.lock | ||
|
||
# Intellij | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { exec } from "child_process"; | ||
import * as React from "react"; | ||
import { Form, Icon, Message, TextArea } from "semantic-ui-react"; | ||
|
||
import { Text } from "@/components/Form"; | ||
import type { ActionTypeGenerator, Context } from "@/lib/event_actions"; | ||
|
||
import type { ActionComponent } from "./types"; | ||
|
||
type ActionRunShellCommandParams = { | ||
command: string; | ||
}; | ||
|
||
const ActionRunShellCommandFunc: ActionTypeGenerator = (params: ActionRunShellCommandParams) => { | ||
return async (ctx: Context): Promise<Context> => { | ||
// Make sure we actually have a command to run | ||
if (params.command) { | ||
const ctxJson = JSON.stringify(ctx).replace(/"/gm, '\\"'); | ||
const command = ctx ? params.command.replace(/{event}/gm, ctxJson) : params.command; | ||
|
||
runShellCommand(command); | ||
} | ||
|
||
return ctx; | ||
}; | ||
}; | ||
|
||
const ActionIcon = () => { | ||
return <Icon name="terminal" size="large" />; | ||
}; | ||
|
||
const RunShellCommandInput = ({ | ||
value, | ||
onChange, | ||
}: { | ||
value: ActionRunShellCommandParams; | ||
onChange: (val: ActionRunShellCommandParams) => void; | ||
}) => { | ||
const defaultValue = value && value.command ? value.command : ""; | ||
const [cmd, setMsg] = React.useState(defaultValue); | ||
return ( | ||
<div style={{ marginTop: 10 }}> | ||
<Message warning={true}> | ||
<Icon name="warning sign" /> | ||
Running unknown commands can be very dangerous! Only run commands that you fully understand! | ||
</Message> | ||
<Form> | ||
<TextArea | ||
style={{ fontFamily: "monospace", fontSize: 16 }} | ||
onBlur={() => onChange({ command: cmd })} | ||
value={cmd} | ||
onChange={(_: any, { value }: any) => setMsg(value)} | ||
placeholder="Enter a shell command to run..." | ||
/> | ||
</Form> | ||
<Text>Pro tip: Use {event} to get the event data as a JSON string.</Text> | ||
</div> | ||
); | ||
}; | ||
|
||
export const ActionRunShellCommand: ActionComponent = { | ||
label: "run a shell command", | ||
action: ActionRunShellCommandFunc, | ||
Icon: ActionIcon, | ||
Component: RunShellCommandInput, | ||
}; | ||
|
||
async function runShellCommand(command: string) { | ||
exec(command, (error: Error | null, stdout: string, stderr: string) => { | ||
if (error) { | ||
console.error(`error: ${error.message}`); | ||
return; | ||
} | ||
if (stderr) { | ||
console.warn(`stderr: ${stderr}`); | ||
return; | ||
} | ||
console.log(`stdout: ${stdout}`); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters