Skip to content

Commit 9061911

Browse files
committed
[SVLS-8314] refactor renderers
1 parent 9384a89 commit 9061911

File tree

7 files changed

+53
-63
lines changed

7 files changed

+53
-63
lines changed

packages/plugin-lambda/src/commands/disable-cloudwatch.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {IAMClient} from '@aws-sdk/client-iam'
22
import {LambdaClient, LambdaClientConfig} from '@aws-sdk/client-lambda'
33
import {AwsCredentialIdentity} from '@aws-sdk/types'
4-
import {LambdaDisableCloudwatchCommand} from '@datadog/datadog-ci-base/commands/lambda/disableCloudwatch'
4+
import {LambdaDisableCloudwatchCommand} from '@datadog/datadog-ci-base/commands/lambda/disable-cloudwatch'
55
import {FIPS_ENV_VAR, FIPS_IGNORE_ERROR_ENV_VAR} from '@datadog/datadog-ci-base/constants'
66
import {toBoolean} from '@datadog/datadog-ci-base/helpers/env'
77
import {enableFips} from '@datadog/datadog-ci-base/helpers/fips'
@@ -63,13 +63,13 @@ export class PluginCommand extends LambdaDisableCloudwatchCommand {
6363
if (hasSpecifiedRegExPattern) {
6464
if (hasSpecifiedFunctions) {
6565
this.context.stdout.write(
66-
cloudwatchRenderer.renderFunctionsAndFunctionsRegexOptionsBothSetError(this.functions.length !== 0)
66+
commonRenderer.renderFunctionsAndFunctionsRegexOptionsBothSetError(this.functions.length !== 0)
6767
)
6868

6969
return 1
7070
}
7171
if (this.regExPattern!.match(':')) {
72-
this.context.stdout.write(cloudwatchRenderer.renderRegexSetWithARNError())
72+
this.context.stdout.write(commonRenderer.renderRegexSetWithARNError())
7373

7474
return 1
7575
}
@@ -112,7 +112,7 @@ export class PluginCommand extends LambdaDisableCloudwatchCommand {
112112
this.region || this.config.region
113113
)
114114
} catch (err) {
115-
this.context.stdout.write(cloudwatchRenderer.renderCouldntGroupFunctionsError(err))
115+
this.context.stdout.write(commonRenderer.renderCouldntGroupFunctionsError(err))
116116

117117
return 1
118118
}

packages/plugin-lambda/src/commands/enable-cloudwatch.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {IAMClient} from '@aws-sdk/client-iam'
22
import {LambdaClient, LambdaClientConfig} from '@aws-sdk/client-lambda'
33
import {AwsCredentialIdentity} from '@aws-sdk/types'
4-
import {LambdaEnableCloudwatchCommand} from '@datadog/datadog-ci-base/commands/lambda/enableCloudwatch'
4+
import {LambdaEnableCloudwatchCommand} from '@datadog/datadog-ci-base/commands/lambda/enable-cloudwatch'
55
import {FIPS_ENV_VAR, FIPS_IGNORE_ERROR_ENV_VAR} from '@datadog/datadog-ci-base/constants'
66
import {toBoolean} from '@datadog/datadog-ci-base/helpers/env'
77
import {enableFips} from '@datadog/datadog-ci-base/helpers/fips'
@@ -63,13 +63,13 @@ export class PluginCommand extends LambdaEnableCloudwatchCommand {
6363
if (hasSpecifiedRegExPattern) {
6464
if (hasSpecifiedFunctions) {
6565
this.context.stdout.write(
66-
cloudwatchRenderer.renderFunctionsAndFunctionsRegexOptionsBothSetError(this.functions.length !== 0)
66+
commonRenderer.renderFunctionsAndFunctionsRegexOptionsBothSetError(this.functions.length !== 0)
6767
)
6868

6969
return 1
7070
}
7171
if (this.regExPattern!.match(':')) {
72-
this.context.stdout.write(cloudwatchRenderer.renderRegexSetWithARNError())
72+
this.context.stdout.write(commonRenderer.renderRegexSetWithARNError())
7373

7474
return 1
7575
}
@@ -112,7 +112,7 @@ export class PluginCommand extends LambdaEnableCloudwatchCommand {
112112
this.region || this.config.region
113113
)
114114
} catch (err) {
115-
this.context.stdout.write(cloudwatchRenderer.renderCouldntGroupFunctionsError(err))
115+
this.context.stdout.write(commonRenderer.renderCouldntGroupFunctionsError(err))
116116

117117
return 1
118118
}

packages/plugin-lambda/src/commands/instrument.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,13 @@ export class PluginCommand extends LambdaInstrumentCommand {
189189
if (hasSpecifiedRegExPattern) {
190190
if (hasSpecifiedFunctions) {
191191
this.context.stdout.write(
192-
instrumentRenderer.renderFunctionsAndFunctionsRegexOptionsBothSetError(this.functions.length !== 0)
192+
commonRenderer.renderFunctionsAndFunctionsRegexOptionsBothSetError(this.functions.length !== 0)
193193
)
194194

195195
return 1
196196
}
197197
if (this.regExPattern!.match(':')) {
198-
this.context.stdout.write(instrumentRenderer.renderRegexSetWithARNError())
198+
this.context.stdout.write(commonRenderer.renderRegexSetWithARNError())
199199

200200
return 1
201201
}
@@ -247,7 +247,7 @@ export class PluginCommand extends LambdaInstrumentCommand {
247247
region
248248
)
249249
} catch (err) {
250-
this.context.stdout.write(instrumentRenderer.renderCouldntGroupFunctionsError(err))
250+
this.context.stdout.write(commonRenderer.renderCouldntGroupFunctionsError(err))
251251

252252
return 1
253253
}

packages/plugin-lambda/src/commands/uninstrument.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@ export class PluginCommand extends LambdaUninstrumentCommand {
130130
if (hasSpecifiedRegExPattern) {
131131
if (hasSpecifiedFunctions) {
132132
this.context.stdout.write(
133-
instrumentRenderer.renderFunctionsAndFunctionsRegexOptionsBothSetError(this.functions.length !== 0)
133+
commonRenderer.renderFunctionsAndFunctionsRegexOptionsBothSetError(this.functions.length !== 0)
134134
)
135135

136136
return 1
137137
}
138138
if (this.regExPattern!.match(':')) {
139-
this.context.stdout.write(instrumentRenderer.renderRegexSetWithARNError())
139+
this.context.stdout.write(commonRenderer.renderRegexSetWithARNError())
140140

141141
return 1
142142
}
@@ -186,7 +186,7 @@ export class PluginCommand extends LambdaUninstrumentCommand {
186186
this.region || this.config.region
187187
)
188188
} catch (err) {
189-
this.context.stdout.write(instrumentRenderer.renderCouldntGroupFunctionsError(err))
189+
this.context.stdout.write(commonRenderer.renderCouldntGroupFunctionsError(err))
190190

191191
return 1
192192
}

packages/plugin-lambda/src/renderers/cloudwatch-renderer.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,6 @@ export const renderCloudwatchHeader = (action: 'disable' | 'enable', isDryRun: b
1212
export const renderNoFunctionsSpecifiedError = () =>
1313
renderError('No functions specified. Use -f, --function, or --functions-regex.')
1414

15-
export const renderFunctionsAndFunctionsRegexOptionsBothSetError = (functionsCommandUsed: boolean) => {
16-
const usedCommand = functionsCommandUsed ? '"--functions"' : 'Functions in config file'
17-
18-
return renderError(`${usedCommand} and "--functions-regex" should not be used at the same time.`)
19-
}
20-
21-
export const renderRegexSetWithARNError = () => renderError(`"--functions-regex" isn't meant to be used with ARNs.`)
22-
23-
export const renderCouldntGroupFunctionsError = (error: unknown) => renderError(`Couldn't group functions. ${error}`)
24-
2515
export const renderDryRunFunctionAction = (action: 'disable' | 'enable', functionName: string, roleName: string) => {
2616
const verb = action === 'disable' ? 'Attach' : 'Remove'
2717

packages/plugin-lambda/src/renderers/common-renderer.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,42 @@ export const renderNoAWSCredentialsFound = () =>
2020
`${renderSoftWarning(
2121
"No AWS credentials found, let's set them up! Or you can re-run the command and supply the AWS credentials in the same way when you invoke the AWS CLI."
2222
)}`
23+
24+
/**
25+
* @param functionsCommandUsed a boolean indicating which command was used for the specified functions.
26+
* @returns a message indicating that option `--functions-regex`
27+
* is being used along with either `--functions` or the parameter
28+
* `functions` in a config file.
29+
*
30+
* ```txt
31+
* [Error] "--functions" and "--functions-regex" should not be used at the same time.
32+
* or
33+
* [Error] Functions in config file and "--functions-regex" should not be used at the same time.
34+
* ```
35+
*/
36+
export const renderFunctionsAndFunctionsRegexOptionsBothSetError = (functionsCommandUsed: boolean) => {
37+
const usedCommand = functionsCommandUsed ? '"--functions"' : 'Functions in config file'
38+
39+
return renderError(`${usedCommand} and "--functions-regex" should not be used at the same time.`)
40+
}
41+
42+
/**
43+
* @returns a message indicating that `--functions-regex` argument contains `:` which is mainly used with ARNs.
44+
*
45+
* ```txt
46+
* [Error] "--functions-regex" isn't meant to be used with ARNs.
47+
* ```
48+
*/
49+
export const renderRegexSetWithARNError = () => renderError(`"--functions-regex" isn't meant to be used with ARNs.`)
50+
51+
/**
52+
* @param error an error message or an object of type `unknown`*.
53+
* @returns a message indicating that an error occurred while grouping functions.
54+
*
55+
* * Using unknown since we're not type guarding.
56+
*
57+
* ```txt
58+
* [Error] Couldn't group functions. The provided error goes here!
59+
* ```
60+
*/
61+
export const renderCouldntGroupFunctionsError = (error: unknown) => renderError(`Couldn't group functions. ${error}`)

packages/plugin-lambda/src/renderers/instrument-uninstrument-renderer.ts

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -60,45 +60,6 @@ export const renderNoFunctionsSpecifiedError = (commandType: InstrumentCommand |
6060
export const renderExtensionAndForwarderOptionsBothSetError = () =>
6161
renderError('"extensionVersion" and "forwarder" should not be used at the same time.')
6262

63-
/**
64-
* @param functionsCommandUsed a boolean indicating which command was used for the specified functions.
65-
* @returns a message indicating that option `--functions-regex`
66-
* is being used along with either `--functions` or the parameter
67-
* `functions` in a config file.
68-
*
69-
* ```txt
70-
* [Error] "--functions" and "--functions-regex" should not be used at the same time.
71-
* or
72-
* [Error] Functions in config file and "--functions-regex" should not be used at the same time.
73-
* ```
74-
*/
75-
export const renderFunctionsAndFunctionsRegexOptionsBothSetError = (functionsCommandUsed: boolean) => {
76-
const usedCommand = functionsCommandUsed ? '"--functions"' : 'Functions in config file'
77-
78-
return renderError(`${usedCommand} and "--functions-regex" should not be used at the same time.`)
79-
}
80-
81-
/**
82-
* @returns a message indicating that `--functions-regex` argument contains `:` which is mainly used with ARNs.
83-
*
84-
* ```txt
85-
* [Error] "--functions-regex" isn't meant to be used with ARNs.
86-
* ```
87-
*/
88-
export const renderRegexSetWithARNError = () => renderError(`"--functions-regex" isn't meant to be used with ARNs.`)
89-
90-
/**
91-
* @param error an error message or an object of type `unknown`*.
92-
* @returns a message indicating that an error occurred while grouping functions.
93-
*
94-
* * Using unknown since we're not type guarding.
95-
*
96-
* ```txt
97-
* [Error] Couldn't group functions. The provided error goes here!
98-
* ```
99-
*/
100-
export const renderCouldntGroupFunctionsError = (error: unknown) => renderError(`Couldn't group functions. ${error}`)
101-
10263
/**
10364
* @param error an error message or an object of type `unknown`*.
10465
* @returns a message indicating that an error occurred while updating.

0 commit comments

Comments
 (0)