Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/core/src/awsService/ec2/sshKeyPair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,16 @@ export class SshKeyPair {
const overrideKeys = async (_t: string, proc: RunParameterContext) => {
await proc.send('yes')
}
return !(await tryRun('ssh-keygen', ['-t', keyType, '-N', '', '-q', '-f', keyPath], 'yes', 'unknown key type', {
onStdout: overrideKeys,
timeout: new Timeout(5000),
}))
return await tryRun(
'ssh-keygen',
['-t', keyType, '-N', '', '-q', '-f', keyPath],
'yes',
/^(?!.*Unknown key type).*/i,
{
onStdout: overrideKeys,
timeout: new Timeout(5000),
}
)
}

public static async tryKeyTypes(keyPath: string, keyTypes: sshKeyType[]): Promise<boolean> {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/shared/utilities/pathFind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { GitExtension } from '../extensions/git'
import { Settings } from '../settings'
import { getLogger } from '../logger/logger'
import { mergeResolvedShellPath } from '../env/resolveEnv'
import { matchesPattern } from './textUtilities'

/** Full path to VSCode CLI. */
let vscPath: string
Expand All @@ -32,15 +33,15 @@ export async function tryRun(
p: string,
args: string[],
logging: 'yes' | 'no' | 'noresult' = 'yes',
expected?: string,
expected?: string | RegExp,
opt?: ChildProcessOptions
): Promise<boolean> {
const proc = new ChildProcess(p, args, { logging: 'no' })
const r = await proc.run({
...opt,
spawnOptions: { env: await mergeResolvedShellPath(opt?.spawnOptions?.env ?? process.env) },
})
const ok = r.exitCode === 0 && (expected === undefined || r.stdout.includes(expected))
const ok = r.exitCode === 0 && (expected === undefined || matchesPattern(r.stdout, expected))
if (logging === 'noresult') {
getLogger().info('tryRun: %s: %s', ok ? 'ok' : 'failed', proc)
} else if (logging !== 'no') {
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/shared/utilities/textUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,10 @@ export function extractFileAndCodeSelectionFromMessage(message: any) {
const selection = message?.context?.focusAreaContext?.selectionInsideExtendedCodeBlock as vscode.Selection
return { filePath, selection }
}

export function matchesPattern(source: string, target: string | RegExp) {
if (typeof target === 'string') {
return source.includes(target)
}
return target.test(source)
}
18 changes: 18 additions & 0 deletions packages/core/src/test/shared/utilities/pathFind.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ describe('pathFind', function () {
assert.ok(regex.test(vscPath), `expected regex ${regex} to match: "${vscPath}"`)
})

describe('tryRun', function () {
it('returns true if output matches expected', async function () {
const posResult = await tryRun('echo', ['hello'], 'no', 'hello')
assert.ok(posResult)

const negResult = await tryRun('echo', ['hi'], 'no', 'hello')
assert.ok(!negResult)
})

it('supports regex on output match', async function () {
const posResult = await tryRun('echo', ['hello'], 'no', /hel*o/)
assert.ok(posResult)

const negResult = await tryRun('echo', ['hi'], 'no', /^(?!(hi)).*$/)
assert.ok(!negResult)
})
})

describe('findSshPath', function () {
let previousPath: string | undefined

Expand Down
Loading