Skip to content

Commit 22e1d1a

Browse files
committed
make progress on ec2
1 parent c3d4ebb commit 22e1d1a

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { showMessageWithCancel } from '../../shared/utilities/messages'
2828
import { SshConfig } from '../../shared/sshConfig'
2929
import { SshKeyPair } from './sshKeyPair'
3030
import { Ec2SessionTracker } from './remoteSessionManager'
31-
import { getEc2SsmEnv, getRemoveLabeledEntriesCommand } from './utils'
31+
import { getEc2SsmEnv, getRemoveLinesCommand } from './utils'
3232

3333
export type Ec2ConnectErrorCode = 'EC2SSMStatus' | 'EC2SSMPermission' | 'EC2SSMConnect' | 'EC2SSMAgentStatus'
3434

@@ -268,14 +268,13 @@ export class Ec2Connecter implements vscode.Disposable {
268268
const hintComment = '# Added by AWS Toolkit for VSCode'
269269

270270
const remoteAuthorizedKeysPath = `/home/${remoteUser}/.ssh/authorized_keys`
271-
const deleteExistingKeyCommand = getRemoveLabeledEntriesCommand(hintComment, remoteAuthorizedKeysPath)
271+
const deleteExistingKeyCommand = getRemoveLinesCommand(hintComment, remoteAuthorizedKeysPath)
272272

273273
const appendStr = (s: string) => `echo "${s}" >> ${remoteAuthorizedKeysPath}`
274-
const writeCommentCommand = appendStr(hintComment)
275-
const writeKeyCommand = appendStr(sshPubKey)
274+
const writeKeyCommand = appendStr([sshPubKey, hintComment].join(' '))
276275

277276
await this.ssmClient.sendCommandAndWait(selection.instanceId, 'AWS-RunShellScript', {
278-
commands: [deleteExistingKeyCommand, writeCommentCommand, writeKeyCommand],
277+
commands: [deleteExistingKeyCommand, writeKeyCommand],
279278
})
280279
}
281280

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ export function getEc2SsmEnv(
4747
)
4848
}
4949
/**
50-
* Generate bash command (as string) to remove lines in file prefixed by `hintComment`.
51-
* @param hintComment prefix comment for deleted lines.
50+
* Generate bash command (as string) to remove lines in file suffixed by `hintComment`.
51+
* @param hintComment suffix comment for deleted lines.
5252
* @param filepath filepath (as string) to target with the command.
5353
* @returns bash command to remove lines from file.
5454
*/
55-
export function getRemoveLabeledEntriesCommand(hintComment: string, filepath: string): string {
56-
return `sed -i '' '/${hintComment}/N; /${hintComment}/ {N; d}' ${filepath}`
55+
export function getRemoveLinesCommand(hintComment: string, filepath: string): string {
56+
return `sed /${hintComment}/d ${filepath}`
5757
}

packages/core/src/test/awsService/ec2/utils.test.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
import assert from 'assert'
77
import * as sinon from 'sinon'
88
import { SafeEc2Instance } from '../../../shared/clients/ec2Client'
9-
import { getIconCode } from '../../../awsService/ec2/utils'
10-
import { DefaultAwsContext } from '../../../shared'
9+
import { getIconCode, getRemoveLinesCommand } from '../../../awsService/ec2/utils'
10+
import { DefaultAwsContext, globals } from '../../../shared'
11+
import { fs } from '../../../shared/fs/fs'
12+
import { createTestWorkspaceFolder } from '../../testUtil'
13+
import path from 'path'
14+
import { ChildProcess } from '../../../shared/utilities/processUtils'
1115

1216
describe('utils', async function () {
1317
before(function () {
@@ -52,4 +56,25 @@ describe('utils', async function () {
5256
assert.strictEqual(getIconCode(stoppingInstance), 'loading~spin')
5357
})
5458
})
59+
60+
describe('getRemoveLinesCommand', async function () {
61+
it('removes lines prefixed by pattern', async function () {
62+
const pattern = 'pattern'
63+
const lines = ['line1', 'line2 pattern', 'line3', 'line4 pattern', 'line5', 'line6 pattern', 'line7']
64+
const expected = ['line1', 'line3', 'line5', 'line7']
65+
const tempPath = await createTestWorkspaceFolder()
66+
const textFile = path.join(tempPath.uri.fsPath, 'test.txt')
67+
await fs.writeFile(textFile, lines.join('\n'))
68+
69+
const [command, ...args] = getRemoveLinesCommand(pattern, textFile).split(' ')
70+
const process = new ChildProcess(command, args, { collect: true })
71+
const result = await process.run()
72+
73+
assert.strictEqual(result.exitCode, 0, `ChildProcess failed with error=${result.error}`)
74+
const newContent = await fs.readFileText(textFile)
75+
76+
assert.strictEqual(newContent, expected.join('\n') + '\n')
77+
await fs.delete(tempPath.uri.fsPath, { recursive: true, force: true })
78+
})
79+
})
5580
})

0 commit comments

Comments
 (0)