-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandRunner.ts
More file actions
53 lines (46 loc) · 1.67 KB
/
Copy pathcommandRunner.ts
File metadata and controls
53 lines (46 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import {Logger} from "jsr:@std/log@0.219.1/logger"
export async function executeAwsCommand(command: string, isVerbose: boolean, logger: Logger): Promise<any> {
if (isVerbose) {
console.log(JSON.stringify({level: "info", message: "Executing AWS command", command, args: command.split(" ").splice}))
}
try {
const commandOutput = new Deno.Command("sh", {
args: ["-c", command],
stdout: "piped",
stderr: "piped",
})
const {code, stdout, stderr} = await commandOutput.output()
const commandError = new TextDecoder().decode(stderr)
const output = new TextDecoder().decode(stdout)
logger.info({code, command})
if (code === 0) {
if (isVerbose) {
logger.info(JSON.stringify({level: "info", message: "AWS command executed successfully", command, output}))
}
return JSON.parse(output)
} else {
throw new Error(`Command failed: ${commandError}`)
}
} catch (error) {
if (isVerbose) {
console.error(
JSON.stringify({
level: "error",
message: "Error executing AWS command",
command,
details: (error instanceof Error ? error.message : String(error)),
})
)
} else {
console.error(
JSON.stringify({
level: "error",
message: "Error executing AWS command",
command,
error: (error as Error).message,
})
)
}
throw error
}
}