|
1 | 1 | import path from 'path'; |
2 | 2 | import fetch from 'node-fetch'; |
3 | | -// import FormData from 'form-data'; |
| 3 | +import * as readline from 'node:readline/promises'; |
| 4 | +import { stdin as input, stdout as output } from 'node:process'; |
| 5 | + |
4 | 6 | import { FormData, File } from 'formdata-node'; |
5 | 7 |
|
6 | 8 | import { errors } from './../errors/index.js'; |
7 | 9 |
|
8 | 10 | import CLIErrorHandler from './../utils/CLIErrorHandler.js'; |
9 | 11 | import { loadConfigFromYAML } from './../utils/loadConfigFromYAML.js'; |
| 12 | +import executeShellCommandClass from './../utils/executeShellCommandClass.js'; |
10 | 13 |
|
11 | 14 | export const serverGetLogs = async (options, program) => { |
12 | 15 | await (async () => { |
@@ -126,8 +129,68 @@ export const serverShell = async (options, program) => { |
126 | 129 | const endpoint = configuration?.endpoint ?? 'http://localhost:8081'; |
127 | 130 | const adminSecret = options?.adminSecret ?? configuration?.admin_secret ?? ''; |
128 | 131 |
|
129 | | - // TODO: Implement this |
130 | | - throw new Error(errors.FUNCTIONALITY_NOT_IMPLEMENTED); |
| 132 | + const headers = { |
| 133 | + 'x-hlambda-admin-secret': adminSecret, |
| 134 | + }; |
| 135 | + const response = await fetch(`${endpoint}/console/api/v1/command-cwd`, { |
| 136 | + method: 'GET', |
| 137 | + headers, |
| 138 | + }); |
| 139 | + |
| 140 | + if (response.status === 200) { |
| 141 | + console.log('Connection valid!'.green); |
| 142 | + } else { |
| 143 | + throw new Error(errors.ERROR_INVALID_HLAMBDA_ADMIN_SECRET); |
| 144 | + } |
| 145 | + console.log(response.status); |
| 146 | + const cwdCommandResult = await response.json(); |
| 147 | + |
| 148 | + const executeShellCommand = executeShellCommandClass(adminSecret, endpoint, true); |
| 149 | + |
| 150 | + let workingDirectory = cwdCommandResult?.cwd ?? './'; |
| 151 | + |
| 152 | + const writePwdConsole = () => { |
| 153 | + process.stdout.write(`<${'hlambda-server'}@${endpoint}> ${workingDirectory} # `.yellow); |
| 154 | + }; |
| 155 | + writePwdConsole(); |
| 156 | + |
| 157 | + // Register linebyline listener |
| 158 | + const rl = readline.createInterface({ input, output }); |
| 159 | + |
| 160 | + // const answer = await rl.question('What do you think of Node.js? '); |
| 161 | + // console.log(`Thank you for your valuable feedback: ${answer}`); |
| 162 | + |
| 163 | + rl.on('line', async (terminalInput) => { |
| 164 | + if (terminalInput === 'exit' || terminalInput === 'quit') { |
| 165 | + rl.close(); |
| 166 | + return; |
| 167 | + } |
| 168 | + if (terminalInput.toLowerCase().startsWith('cd')) { |
| 169 | + // Change working dir for the command... |
| 170 | + const t = terminalInput.match(/cd\s(.+)/); |
| 171 | + const responseCd = await fetch(`${endpoint}/console/api/v1/command-change-dir`, { |
| 172 | + method: 'POST', |
| 173 | + headers: { |
| 174 | + 'x-hlambda-admin-secret': adminSecret, |
| 175 | + Accept: 'application/json', |
| 176 | + 'Content-Type': 'application/json', // Important |
| 177 | + }, |
| 178 | + body: JSON.stringify({ |
| 179 | + path: t[1], |
| 180 | + }), |
| 181 | + }); |
| 182 | + const responseJson = await responseCd.json(); |
| 183 | + const outputString = responseJson; |
| 184 | + // console.log(outputString); |
| 185 | + workingDirectory = outputString?.cwd; |
| 186 | + writePwdConsole(); |
| 187 | + return; |
| 188 | + } |
| 189 | + const result = await executeShellCommand(terminalInput, workingDirectory); |
| 190 | + const data = await result.json(); |
| 191 | + process.stdout.write(data?.data); |
| 192 | + writePwdConsole(); |
| 193 | + }); |
131 | 194 | })() |
132 | 195 | .then(() => {}) |
133 | 196 | .catch(CLIErrorHandler(program)); |
|
0 commit comments