Skip to content

Commit bf4b54b

Browse files
committed
ENG-2660: Print selected SSH output to stderr
If debug is not enabled, still print selected SSH output to stderr: - "Authentoicated to" ... message - "port forwarding failed" messages Example: fabianjoya@MacBook-Pro p0cli % ./p0 ssh dev-instance-fabian-joya --provider gcloud -- -NR '*:8080:localhost:8088' -o 'GatewayPorts yes' Authenticated to dev-instance-fabian-joya (via proxy) using "publickey". Warning: remote port forwarding failed for listen port 8080
1 parent b2e854a commit bf4b54b

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

src/plugins/ssh/index.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ const accessPropagationGuard = (
118118

119119
child.stderr.on("data", (chunk) => {
120120
const chunkString: string = chunk.toString("utf-8");
121-
122-
if (debug) print2(chunkString);
121+
parseAndPrintSshOutputToStderr(chunkString, debug);
123122

124123
const match = UNPROVISIONED_ACCESS_MESSAGES.find((message) =>
125124
chunkString.match(message.pattern)
@@ -146,6 +145,36 @@ const accessPropagationGuard = (
146145
};
147146
};
148147

148+
/**
149+
* Parses and prints a chunk of SSH output to stderr.
150+
*
151+
* If debug is enabled, all output is printed. Otherwise, only selected messages are printed.
152+
*
153+
* @param chunkString the chunk to print
154+
* @param debug true if debug output is enabled
155+
*/
156+
const parseAndPrintSshOutputToStderr = (
157+
chunkString: string,
158+
debug?: boolean
159+
) => {
160+
const lines = chunkString.split("\n");
161+
162+
// SSH only prints the "Authenticated to" message if the verbose option (-v) is enabled
163+
for (const line of lines) {
164+
if (debug) {
165+
print2(line);
166+
} else {
167+
if (line.includes("Authenticated to")) {
168+
print2(line);
169+
}
170+
171+
if (line.includes("port forwarding failed")) {
172+
print2(line);
173+
}
174+
}
175+
}
176+
};
177+
149178
const spawnChildProcess = (
150179
credential: AwsCredentials | undefined,
151180
command: string,
@@ -315,7 +344,7 @@ const addCommonArgs = (args: CommandArgs, proxyCommand: string[]) => {
315344
}
316345

317346
const verboseOptionExists = sshOptions.some((opt) => opt === "-v");
318-
if (!verboseOptionExists && args.debug) {
347+
if (!verboseOptionExists) {
319348
sshOptions.push("-v");
320349
}
321350
};

0 commit comments

Comments
 (0)