Skip to content

Commit 7a8eb52

Browse files
keeganirbykaranA-aws
authored andcommitted
refactor(cwl): lift TailLogGroupWizard into its own class aws#5785
## Problem Wizard code intermixed with command logic ## Solution Refactor out the Wizard so the TailLogGroup command class can focus only on command logic. * Additionally adds the AWSToolkit for VSCode UserAgent to the CWL LiveTail client.
1 parent 8fd13e6 commit 7a8eb52

File tree

6 files changed

+117
-106
lines changed

6 files changed

+117
-106
lines changed

packages/core/src/awsService/cloudWatchLogs/commands/tailLogGroup.ts

Lines changed: 2 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,9 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import * as nls from 'vscode-nls'
7-
import { DefaultCloudWatchLogsClient } from '../../../shared/clients/cloudWatchLogsClient'
8-
import { createBackButton, createExitButton, createHelpButton } from '../../../shared/ui/buttons'
9-
import { createInputBox } from '../../../shared/ui/inputPrompter'
10-
import { DataQuickPickItem } from '../../../shared/ui/pickerPrompter'
11-
import { Wizard } from '../../../shared/wizards/wizard'
12-
import { CloudWatchLogsGroupInfo } from '../registry/logDataRegistry'
13-
import { RegionSubmenu, RegionSubmenuResponse } from '../../../shared/ui/common/regionSubmenu'
6+
import { TailLogGroupWizard } from '../wizard/tailLogGroupWizard'
7+
import { getLogger } from '../../../shared'
148
import { CancellationError } from '../../../shared/utilities/timeoutUtils'
15-
import { LogStreamFilterResponse, LogStreamFilterSubmenu } from '../liveTailLogStreamSubmenu'
16-
import { getLogger, ToolkitError } from '../../../shared'
17-
import { cwlFilterPatternHelpUrl } from '../../../shared/constants'
18-
19-
const localize = nls.loadMessageBundle()
20-
21-
export interface TailLogGroupWizardResponse {
22-
regionLogGroupSubmenuResponse: RegionSubmenuResponse<string>
23-
logStreamFilter: LogStreamFilterResponse
24-
filterPattern: string
25-
}
269

2710
export async function tailLogGroup(logData?: { regionName: string; groupName: string }): Promise<void> {
2811
const wizard = new TailLogGroupWizard(logData)
@@ -34,75 +17,3 @@ export async function tailLogGroup(logData?: { regionName: string; groupName: st
3417
//TODO: Remove Log. For testing while we aren't yet consuming the wizardResponse.
3518
getLogger().info(JSON.stringify(wizardResponse))
3619
}
37-
38-
export class TailLogGroupWizard extends Wizard<TailLogGroupWizardResponse> {
39-
public constructor(logGroupInfo?: CloudWatchLogsGroupInfo) {
40-
super({
41-
initState: {
42-
regionLogGroupSubmenuResponse: logGroupInfo
43-
? {
44-
data: logGroupInfo.groupName,
45-
region: logGroupInfo.regionName,
46-
}
47-
: undefined,
48-
},
49-
})
50-
this.form.regionLogGroupSubmenuResponse.bindPrompter(createRegionLogGroupSubmenu)
51-
this.form.logStreamFilter.bindPrompter((state) => {
52-
if (!state.regionLogGroupSubmenuResponse?.data) {
53-
throw new ToolkitError('LogGroupName is null')
54-
}
55-
return new LogStreamFilterSubmenu(
56-
state.regionLogGroupSubmenuResponse.data,
57-
state.regionLogGroupSubmenuResponse.region
58-
)
59-
})
60-
this.form.filterPattern.bindPrompter((state) => createFilterPatternPrompter())
61-
}
62-
}
63-
64-
export function createRegionLogGroupSubmenu(): RegionSubmenu<string> {
65-
return new RegionSubmenu(
66-
getLogGroupQuickPickOptions,
67-
{
68-
title: localize('AWS.cwl.tailLogGroup.logGroupPromptTitle', 'Select Log Group to tail'),
69-
buttons: [createExitButton()],
70-
},
71-
{ title: localize('AWS.cwl.tailLogGroup.regionPromptTitle', 'Select Region for Log Group') },
72-
'LogGroups'
73-
)
74-
}
75-
76-
async function getLogGroupQuickPickOptions(regionCode: string): Promise<DataQuickPickItem<string>[]> {
77-
const client = new DefaultCloudWatchLogsClient(regionCode)
78-
const logGroups = client.describeLogGroups()
79-
80-
const logGroupsOptions: DataQuickPickItem<string>[] = []
81-
82-
for await (const logGroupObject of logGroups) {
83-
if (!logGroupObject.arn || !logGroupObject.logGroupName) {
84-
throw new ToolkitError('LogGroupObject name or arn undefined')
85-
}
86-
87-
logGroupsOptions.push({
88-
label: logGroupObject.logGroupName,
89-
data: formatLogGroupArn(logGroupObject.arn),
90-
})
91-
}
92-
93-
return logGroupsOptions
94-
}
95-
96-
function formatLogGroupArn(logGroupArn: string): string {
97-
return logGroupArn.endsWith(':*') ? logGroupArn.substring(0, logGroupArn.length - 2) : logGroupArn
98-
}
99-
100-
export function createFilterPatternPrompter() {
101-
const helpUri = cwlFilterPatternHelpUrl
102-
return createInputBox({
103-
title: 'Provide log event filter pattern',
104-
placeholder: 'filter pattern (case sensitive; empty matches all)',
105-
prompt: 'Optional pattern to use to filter the results to include only log events that match the pattern.',
106-
buttons: [createHelpButton(helpUri), createBackButton(), createExitButton()],
107-
})
108-
}

packages/core/src/awsService/cloudWatchLogs/registry/liveTailSession.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
*/
55
import * as vscode from 'vscode'
66
import { CloudWatchLogsClient, StartLiveTailCommand, StartLiveTailCommandOutput } from '@aws-sdk/client-cloudwatch-logs'
7-
import { LogStreamFilterResponse } from '../liveTailLogStreamSubmenu'
7+
import { LogStreamFilterResponse } from '../wizard/liveTailLogStreamSubmenu'
88
import { CloudWatchLogsSettings } from '../cloudWatchLogsUtils'
99
import { Settings, ToolkitError } from '../../../shared'
1010
import { createLiveTailURIFromArgs } from './liveTailSessionRegistry'
11+
import { getUserAgent } from '../../../shared/telemetry/util'
1112

1213
export type LiveTailSessionConfiguration = {
1314
logGroupName: string
@@ -35,7 +36,10 @@ export class LiveTailSession {
3536
this._logGroupName = configuration.logGroupName
3637
this.logStreamFilter = configuration.logStreamFilter
3738
this.liveTailClient = {
38-
cwlClient: new CloudWatchLogsClient({ region: configuration.region }),
39+
cwlClient: new CloudWatchLogsClient({
40+
region: configuration.region,
41+
customUserAgent: getUserAgent(),
42+
}),
3943
abortController: new AbortController(),
4044
}
4145
this._maxLines = LiveTailSession.settings.get('liveTailMaxEvents', 10000)

packages/core/src/awsService/cloudWatchLogs/liveTailLogStreamSubmenu.ts renamed to packages/core/src/awsService/cloudWatchLogs/wizard/liveTailLogStreamSubmenu.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5-
import { Prompter, PromptResult } from '../../shared/ui/prompter'
6-
import { DefaultCloudWatchLogsClient } from '../../shared/clients/cloudWatchLogsClient'
7-
import { createCommonButtons } from '../../shared/ui/buttons'
8-
import { createInputBox, InputBoxPrompter } from '../../shared/ui/inputPrompter'
9-
import { createQuickPick, DataQuickPickItem, QuickPickPrompter } from '../../shared/ui/pickerPrompter'
10-
import { pageableToCollection } from '../../shared/utilities/collectionUtils'
5+
import { Prompter, PromptResult } from '../../../shared/ui/prompter'
6+
import { DefaultCloudWatchLogsClient } from '../../../shared/clients/cloudWatchLogsClient'
7+
import { createCommonButtons } from '../../../shared/ui/buttons'
8+
import { createInputBox, InputBoxPrompter } from '../../../shared/ui/inputPrompter'
9+
import { createQuickPick, DataQuickPickItem, QuickPickPrompter } from '../../../shared/ui/pickerPrompter'
10+
import { pageableToCollection } from '../../../shared/utilities/collectionUtils'
1111
import { CloudWatchLogs } from 'aws-sdk'
12-
import { isValidResponse, StepEstimator } from '../../shared/wizards/wizard'
13-
import { isNonNullable } from '../../shared/utilities/tsUtils'
12+
import { isValidResponse, StepEstimator } from '../../../shared/wizards/wizard'
13+
import { isNonNullable } from '../../../shared/utilities/tsUtils'
1414
import {
1515
startLiveTailHelpUrl,
1616
startLiveTailLogStreamNamesHelpUrl,
1717
startLiveTailLogStreamPrefixHelpUrl,
18-
} from '../../shared/constants'
18+
} from '../../../shared/constants'
1919

2020
export type LogStreamFilterType = 'menu' | 'prefix' | 'specific' | 'all'
2121

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*!
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import * as nls from 'vscode-nls'
7+
import { ToolkitError } from '../../../shared'
8+
import { DefaultCloudWatchLogsClient } from '../../../shared/clients/cloudWatchLogsClient'
9+
import { cwlFilterPatternHelpUrl } from '../../../shared/constants'
10+
import { createBackButton, createExitButton, createHelpButton } from '../../../shared/ui/buttons'
11+
import { RegionSubmenu, RegionSubmenuResponse } from '../../../shared/ui/common/regionSubmenu'
12+
import { createInputBox } from '../../../shared/ui/inputPrompter'
13+
import { DataQuickPickItem } from '../../../shared/ui/pickerPrompter'
14+
import { Wizard } from '../../../shared/wizards/wizard'
15+
import { CloudWatchLogsGroupInfo } from '../registry/logDataRegistry'
16+
import { LogStreamFilterResponse, LogStreamFilterSubmenu } from './liveTailLogStreamSubmenu'
17+
18+
const localize = nls.loadMessageBundle()
19+
20+
export interface TailLogGroupWizardResponse {
21+
regionLogGroupSubmenuResponse: RegionSubmenuResponse<string>
22+
logStreamFilter: LogStreamFilterResponse
23+
filterPattern: string
24+
}
25+
26+
export class TailLogGroupWizard extends Wizard<TailLogGroupWizardResponse> {
27+
public constructor(logGroupInfo?: CloudWatchLogsGroupInfo) {
28+
super({
29+
initState: {
30+
regionLogGroupSubmenuResponse: logGroupInfo
31+
? {
32+
data: logGroupInfo.groupName,
33+
region: logGroupInfo.regionName,
34+
}
35+
: undefined,
36+
},
37+
})
38+
this.form.regionLogGroupSubmenuResponse.bindPrompter(createRegionLogGroupSubmenu)
39+
this.form.logStreamFilter.bindPrompter((state) => {
40+
if (!state.regionLogGroupSubmenuResponse?.data) {
41+
throw new ToolkitError('LogGroupName is null')
42+
}
43+
return new LogStreamFilterSubmenu(
44+
state.regionLogGroupSubmenuResponse.data,
45+
state.regionLogGroupSubmenuResponse.region
46+
)
47+
})
48+
this.form.filterPattern.bindPrompter((state) => createFilterPatternPrompter())
49+
}
50+
}
51+
52+
export function createRegionLogGroupSubmenu(): RegionSubmenu<string> {
53+
return new RegionSubmenu(
54+
getLogGroupQuickPickOptions,
55+
{
56+
title: localize('AWS.cwl.tailLogGroup.logGroupPromptTitle', 'Select Log Group to tail'),
57+
buttons: [createExitButton()],
58+
},
59+
{ title: localize('AWS.cwl.tailLogGroup.regionPromptTitle', 'Select Region for Log Group') },
60+
'LogGroups'
61+
)
62+
}
63+
64+
async function getLogGroupQuickPickOptions(regionCode: string): Promise<DataQuickPickItem<string>[]> {
65+
const client = new DefaultCloudWatchLogsClient(regionCode)
66+
const logGroups = client.describeLogGroups()
67+
68+
const logGroupsOptions: DataQuickPickItem<string>[] = []
69+
70+
for await (const logGroupObject of logGroups) {
71+
if (!logGroupObject.arn || !logGroupObject.logGroupName) {
72+
throw new ToolkitError('LogGroupObject name or arn undefined')
73+
}
74+
75+
logGroupsOptions.push({
76+
label: logGroupObject.logGroupName,
77+
data: formatLogGroupArn(logGroupObject.arn),
78+
})
79+
}
80+
81+
return logGroupsOptions
82+
}
83+
84+
function formatLogGroupArn(logGroupArn: string): string {
85+
return logGroupArn.endsWith(':*') ? logGroupArn.substring(0, logGroupArn.length - 2) : logGroupArn
86+
}
87+
88+
export function createFilterPatternPrompter() {
89+
const helpUri = cwlFilterPatternHelpUrl
90+
return createInputBox({
91+
title: 'Provide log event filter pattern',
92+
placeholder: 'filter pattern (case sensitive; empty matches all)',
93+
prompt: 'Optional pattern to use to filter the results to include only log events that match the pattern.',
94+
buttons: [createHelpButton(helpUri), createBackButton(), createExitButton()],
95+
})
96+
}

packages/core/src/test/awsService/cloudWatchLogs/liveTailLogStreamSubmenu.test.ts renamed to packages/core/src/test/awsService/cloudWatchLogs/wizard/liveTailLogStreamSubmenu.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
*/
55

66
import assert from 'assert'
7-
import { LogStreamFilterSubmenu } from '../../../awsService/cloudWatchLogs/liveTailLogStreamSubmenu'
8-
import { createQuickPickPrompterTester, QuickPickPrompterTester } from '../../shared/ui/testUtils'
9-
import { getTestWindow } from '../../shared/vscode/window'
7+
import { LogStreamFilterSubmenu } from '../../../../awsService/cloudWatchLogs/wizard/liveTailLogStreamSubmenu'
8+
import { createQuickPickPrompterTester, QuickPickPrompterTester } from '../../../shared/ui/testUtils'
9+
import { getTestWindow } from '../../../shared/vscode/window'
1010

1111
describe('liveTailLogStreamSubmenu', async function () {
1212
let logStreamFilterSubmenu: LogStreamFilterSubmenu

packages/core/src/test/awsService/cloudWatchLogs/commands/tailLogGroup.test.ts renamed to packages/core/src/test/awsService/cloudWatchLogs/wizard/tailLogGroupWizard.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { TailLogGroupWizard } from '../../../../awsService/cloudWatchLogs/commands/tailLogGroup'
6+
import { TailLogGroupWizard } from '../../../../awsService/cloudWatchLogs/wizard/tailLogGroupWizard'
77
import { createWizardTester } from '../../../shared/wizards/wizardTestUtils'
88

99
describe('TailLogGroupWizard', async function () {

0 commit comments

Comments
 (0)