Skip to content

Commit 6e3ea87

Browse files
committed
refactor to allow onStdOut parameter
1 parent c86daa3 commit 6e3ea87

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

packages/core/src/awsService/ec2/sshKeyPair.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { ToolkitError } from '../../shared/errors'
88
import { tryRun } from '../../shared/utilities/pathFind'
99
import { Timeout } from '../../shared/utilities/timeoutUtils'
1010
import { findAsync } from '../../shared/utilities/collectionUtils'
11+
import { ChildProcess } from '../../shared/utilities/childProcess'
1112

1213
type sshKeyType = 'rsa' | 'ed25519'
1314

@@ -29,12 +30,6 @@ export class SshKeyPair {
2930
}
3031

3132
public static async getSshKeyPair(keyPath: string, lifetime: number, overwrite: boolean = true) {
32-
// Overwrite key if already exists
33-
if (overwrite) {
34-
await fs.delete(keyPath, { force: true })
35-
await fs.delete(`${keyPath}.pub`, { force: true })
36-
}
37-
3833
await SshKeyPair.generateSshKeyPair(keyPath)
3934
return new SshKeyPair(keyPath, lifetime)
4035
}
@@ -52,7 +47,16 @@ export class SshKeyPair {
5247
* @param keyType type of key to generate.
5348
*/
5449
public static async tryKeyGen(keyPath: string, keyType: sshKeyType): Promise<boolean> {
55-
return !(await tryRun('ssh-keygen', ['-t', keyType, '-N', '', '-q', '-f', keyPath], 'yes', 'unknown key type'))
50+
const overrideKeys = async (proc: ChildProcess, text: string) => {
51+
await proc.send('yes')
52+
}
53+
return !(await tryRun(
54+
'ssh-keygen',
55+
['-t', keyType, '-N', '', '-q', '-f', keyPath],
56+
'yes',
57+
'unknown key type',
58+
overrideKeys
59+
))
5660
}
5761

5862
public static async tryKeyTypes(keyPath: string, keyTypes: sshKeyType[]): Promise<boolean> {

packages/core/src/shared/utilities/pathFind.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ export async function tryRun(
3030
p: string,
3131
args: string[],
3232
logging: 'yes' | 'no' | 'noresult' = 'yes',
33-
expected?: string
33+
expected?: string,
34+
onStdout?: (proc: ChildProcess, text: string) => void
3435
): Promise<boolean> {
3536
const proc = new ChildProcess(p, args, { logging: 'no' })
36-
const r = await proc.run()
37+
const options = onStdout ? { onStdout: (text: string) => onStdout(proc, text) } : {}
38+
const r = await proc.run(options)
3739
const ok = r.exitCode === 0 && (expected === undefined || r.stdout.includes(expected))
3840
if (logging === 'noresult') {
3941
getLogger().info('tryRun: %s: %s', ok ? 'ok' : 'failed', proc)

0 commit comments

Comments
 (0)