Skip to content

Commit b000621

Browse files
hannah-taub-1taub
andauthored
ACNA-3510: validate app config by default (#897)
* add config-validation flag and check for it every time getFullConfig is called * add unit tests for config-validate and fix formatting * update config-validation flag message, make sure all commands that use it inherit BaseCommand flags --------- Co-authored-by: taub <taub@adobe.com>
1 parent 9993f59 commit b000621

23 files changed

Lines changed: 105 additions & 69 deletions

src/BaseCommand.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class BaseCommand extends Command {
6666
}
6767

6868
async getAppExtConfigs (flags, options = {}) {
69-
const all = (await this.getFullConfig(options)).all
69+
const all = (await this.getFullConfig(options, flags)).all
7070

7171
// default case: no flags, return all
7272
let ret = all
@@ -96,41 +96,41 @@ class BaseCommand extends Command {
9696
return ret
9797
}
9898

99-
async getRuntimeManifestConfigFile (implName) {
99+
async getRuntimeManifestConfigFile (implName, flags) {
100100
let configKey
101101
if (implName === APPLICATION_CONFIG_KEY) {
102102
configKey = APPLICATION_CONFIG_KEY
103103
} else {
104104
configKey = `${EXTENSIONS_CONFIG_KEY}.${implName}`
105105
}
106-
let configData = await this.getConfigFileForKey(`${configKey}.runtimeManifest`)
106+
let configData = await this.getConfigFileForKey(`${configKey}.runtimeManifest`, flags)
107107
if (!configData.file) {
108108
// first action manifest is not defined
109-
configData = await this.getConfigFileForKey(`${configKey}`)
109+
configData = await this.getConfigFileForKey(`${configKey}`, flags)
110110
configData.key = configData.key + '.runtimeManifest'
111111
}
112112
return configData
113113
}
114114

115-
async getEventsConfigFile (implName) {
115+
async getEventsConfigFile (implName, flags) {
116116
let configKey
117117
if (implName === APPLICATION_CONFIG_KEY) {
118118
configKey = APPLICATION_CONFIG_KEY
119119
} else {
120120
configKey = `${EXTENSIONS_CONFIG_KEY}.${implName}`
121121
}
122-
let configData = await this.getConfigFileForKey(`${configKey}.events`)
122+
let configData = await this.getConfigFileForKey(`${configKey}.events`, flags)
123123
if (!configData.file) {
124124
// first events manifest is not defined
125-
configData = await this.getConfigFileForKey(`${configKey}`)
125+
configData = await this.getConfigFileForKey(`${configKey}`, flags)
126126
configData.key = configData.key + '.events'
127127
}
128128
return configData
129129
}
130130

131-
async getConfigFileForKey (fullKey) {
131+
async getConfigFileForKey (fullKey, flags = {}) {
132132
// NOTE: the index returns undefined if the key is loaded from a legacy configuration file
133-
const fullConfig = await this.getFullConfig()
133+
const fullConfig = await this.getFullConfig({}, flags)
134134
// full key like 'extensions.dx/excshell/1.runtimeManifest'
135135
// returns { key: relKey, file: configFile}
136136
const configData = fullConfig.includeIndex[fullKey]
@@ -142,12 +142,12 @@ class BaseCommand extends Command {
142142
return configData || {}
143143
}
144144

145-
async getFullConfig (options = {}) {
146-
// validate appConfig defaults to false for now
147-
const validateAppConfig = options.validateAppConfig === true
145+
async getFullConfig (options = {}, flags = {}) {
146+
// validate appConfig defaults to true unless flag is explicitly set to off
147+
const validateAppConfig = flags['config-validation'] !== false
148+
aioLogger.debug(`validateAppConfig=${validateAppConfig}`)
148149

149150
if (!this.appConfig) {
150-
// this will explicitly set validateAppConfig=false if not set
151151
this.appConfig = await appConfig.load({ ...options, validateAppConfig })
152152
}
153153
return this.appConfig
@@ -191,7 +191,12 @@ class BaseCommand extends Command {
191191

192192
BaseCommand.flags = {
193193
verbose: Flags.boolean({ char: 'v', description: 'Verbose output' }),
194-
version: Flags.boolean({ description: 'Show version' })
194+
version: Flags.boolean({ description: 'Show version' }),
195+
'config-validation': Flags.boolean({
196+
description: '[default: true] Validate the app configuration file(s) before continuing.',
197+
default: true,
198+
allowNo: true
199+
})
195200
}
196201

197202
BaseCommand.args = {}

src/commands/app/add/action.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class AddActionCommand extends TemplatesCommand {
3232
const config = entries[0][1]
3333

3434
const actionFolder = path.relative(config.root, config.actions.src)
35-
const configData = await this.getRuntimeManifestConfigFile(configName)
35+
const configData = await this.getRuntimeManifestConfigFile(configName, flags)
3636

3737
const projectOrgId = aioConfigLoader.get('project.org.id')
3838
if (!projectOrgId) {

src/commands/app/add/event.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class AddEventCommand extends TemplatesCommand {
3131
const configName = entries[0][0]
3232
const config = entries[0][1]
3333
const actionFolder = path.relative(config.root, config.actions.src)
34-
const runtimeManifestData = await this.getRuntimeManifestConfigFile(configName)
35-
const eventsData = await this.getEventsConfigFile(configName)
34+
const runtimeManifestData = await this.getRuntimeManifestConfigFile(configName, flags)
35+
const eventsData = await this.getEventsConfigFile(configName, flags)
3636
const templateOptions = {
3737
'skip-prompt': false,
3838
'action-folder': actionFolder,

src/commands/app/add/extension.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class AddExtensionCommand extends TemplatesCommand {
2424
this.error('--extension= must also be provided when using --yes')
2525
}
2626

27-
const fullConfig = await this.getFullConfig({ allowNoImpl: true })
27+
const fullConfig = await this.getFullConfig({ allowNoImpl: true }, flags)
2828
const alreadyImplemented = fullConfig.implements
2929

3030
if (flags.extension) {

src/commands/app/add/web-assets.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class AddWebAssetsCommand extends TemplatesCommand {
1919
const { flags } = await this.parse(AddWebAssetsCommand)
2020
aioLogger.debug(`add web-assets with flags: ${JSON.stringify(flags)}`)
2121

22-
const projectName = (await this.getFullConfig()).packagejson.name
22+
const projectName = (await this.getFullConfig({}, flags)).packagejson.name
2323
// guaranteed to have at least one, otherwise would throw in config load or in matching the ext name
2424
const entries = Object.entries(await this.getAppExtConfigs(flags))
2525
if (entries.length > 1) {

src/commands/app/config/get/log-forwarding.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const { setRuntimeApiHostAndAuthHandler } = require('../../../../lib/auth-helper
1515

1616
class LogForwardingCommand extends BaseCommand {
1717
async run () {
18-
let aioConfig = (await this.getFullConfig()).aio
18+
const { flags } = await this.parse(LogForwardingCommand)
19+
let aioConfig = (await this.getFullConfig({}, flags)).aio
1920
aioConfig = setRuntimeApiHostAndAuthHandler(aioConfig)
2021
const lf = await LogForwarding.init(aioConfig)
2122

@@ -50,4 +51,8 @@ class LogForwardingCommand extends BaseCommand {
5051
LogForwardingCommand.description = 'Get log forwarding destination configuration'
5152
LogForwardingCommand.aliases = ['app:config:get:log-forwarding', 'app:config:get:lf']
5253

54+
LogForwardingCommand.flags = {
55+
...BaseCommand.flags
56+
}
57+
5358
module.exports = LogForwardingCommand

src/commands/app/config/get/log-forwarding/errors.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ const { setRuntimeApiHostAndAuthHandler } = require('../../../../../lib/auth-hel
1616

1717
class ErrorsCommand extends BaseCommand {
1818
async run () {
19+
const { flags } = await this.parse(ErrorsCommand)
1920
const spinner = ora()
20-
const lf = await this.getLogForwarding()
21+
const lf = await this.getLogForwarding(flags)
2122
spinner.start('Checking for errors...')
2223
const res = await lf.getErrors()
2324
const destinationMessage = res.configured_forwarder !== undefined
@@ -30,8 +31,8 @@ class ErrorsCommand extends BaseCommand {
3031
}
3132
}
3233

33-
async getLogForwarding () {
34-
let aioConfig = (await this.getFullConfig()).aio
34+
async getLogForwarding (flags) {
35+
let aioConfig = (await this.getFullConfig({}, flags)).aio
3536
aioConfig = setRuntimeApiHostAndAuthHandler(aioConfig)
3637

3738
const runtimeConfig = aioConfig.runtime
@@ -46,5 +47,8 @@ class ErrorsCommand extends BaseCommand {
4647

4748
ErrorsCommand.description = 'Get log forwarding errors'
4849
ErrorsCommand.aliases = ['app:config:get:log-forwarding:errors', 'app:config:get:lf:errors']
50+
ErrorsCommand.flags = {
51+
...BaseCommand.flags
52+
}
4953

5054
module.exports = ErrorsCommand

src/commands/app/config/set/log-forwarding.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const { setRuntimeApiHostAndAuthHandler } = require('../../../../lib/auth-helper
1616

1717
class LogForwardingCommand extends BaseCommand {
1818
async run () {
19-
let aioConfig = (await this.getFullConfig()).aio
19+
const { flags } = await this.parse(LogForwardingCommand)
20+
let aioConfig = (await this.getFullConfig({}, flags)).aio
2021
aioConfig = setRuntimeApiHostAndAuthHandler(aioConfig)
2122
const lf = await LogForwarding.init(aioConfig)
2223

@@ -50,5 +51,8 @@ class LogForwardingCommand extends BaseCommand {
5051

5152
LogForwardingCommand.description = 'Set log forwarding destination configuration'
5253
LogForwardingCommand.aliases = ['app:config:set:log-forwarding', 'app:config:set:lf']
54+
LogForwardingCommand.flags = {
55+
...BaseCommand.flags
56+
}
5357

5458
module.exports = LogForwardingCommand

src/commands/app/delete/action.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class DeleteActionCommand extends BaseCommand {
3030
this.error('<action-name> must also be provided when using --yes')
3131
}
3232

33-
const fullConfig = await this.getFullConfig()
34-
const { actions, actionsByImpl } = await this.getAllActions(fullConfig)
33+
const fullConfig = await this.getFullConfig({}, flags)
34+
const { actions, actionsByImpl } = await this.getAllActions(fullConfig, flags)
3535
if (actions.length <= 0) {
3636
this.error('There are no actions in this project!')
3737
}
@@ -108,7 +108,7 @@ class DeleteActionCommand extends BaseCommand {
108108
)))
109109
}
110110

111-
async getAllActions (config) {
111+
async getAllActions (config, flags = {}) {
112112
const actions = []
113113
const actionsByImpl = {}
114114
const allConfigEntries = Object.entries(config.all)
@@ -121,7 +121,7 @@ class DeleteActionCommand extends BaseCommand {
121121
for (const [actionName, action] of actionEntries) {
122122
const fullActionName = `${pkgName}/${actionName}`
123123
const startKey = implName === 'application' ? 'application' : `extensions.${implName}`
124-
const configData = await this.getConfigFileForKey(`${startKey}.runtimeManifest.packages.${pkgName}.actions.${actionName}`)
124+
const configData = await this.getConfigFileForKey(`${startKey}.runtimeManifest.packages.${pkgName}.actions.${actionName}`, flags)
125125
const actionObj = {
126126
// assumes path is not relative
127127
path: action.function,
@@ -157,13 +157,13 @@ DeleteActionCommand.flags = {
157157
}
158158

159159
DeleteActionCommand.args =
160-
{
161-
'action-name': Args.string({
162-
description: 'Action `pkg/name` to delete, you can specify multiple actions via a comma separated list',
163-
default: '',
164-
required: false
165-
})
166-
}
160+
{
161+
'action-name': Args.string({
162+
description: 'Action `pkg/name` to delete, you can specify multiple actions via a comma separated list',
163+
default: '',
164+
required: false
165+
})
166+
}
167167

168168
DeleteActionCommand.aliases = ['app:delete:actions']
169169

src/commands/app/delete/extension.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class DeleteExtensionCommand extends BaseCommand {
2828
this.error('--extension= must also be provided when using --yes')
2929
}
3030

31-
const fullConfig = await this.getFullConfig({ allowNoImpl: true })
31+
const fullConfig = await this.getFullConfig({ allowNoImpl: true }, flags)
3232
const configs = await this.selectOrGetConfigsToDelete(flags, fullConfig)
3333

3434
const resConfirm = await this.prompt([
@@ -44,7 +44,7 @@ class DeleteExtensionCommand extends BaseCommand {
4444
this.error('aborting..')
4545
}
4646

47-
await this.deleteImplementations(configs)
47+
await this.deleteImplementations(configs, flags)
4848

4949
this.log(chalk.bold(chalk.green(
5050
`✔ Successfully deleted implementation(s) '${Object.keys(configs)}'` + EOL +
@@ -71,7 +71,7 @@ class DeleteExtensionCommand extends BaseCommand {
7171
return await this.getAppExtConfigs(flags)
7272
}
7373

74-
async deleteImplementations (configs) {
74+
async deleteImplementations (configs, flags) {
7575
for (const [id, c] of Object.entries(configs)) {
7676
// delete actions
7777
if (c.app.hasBackend) {
@@ -89,12 +89,12 @@ class DeleteExtensionCommand extends BaseCommand {
8989
// delete config
9090
// try to find another config file => case of init extension in another folder
9191
const configKey = id === 'application' ? 'application' : `extensions.${id}`
92-
const configDataOp = await this.getConfigFileForKey(configKey + '.operations')
92+
const configDataOp = await this.getConfigFileForKey(configKey + '.operations', flags)
9393
if (configDataOp.file) {
9494
fs.removeSync(configDataOp.file)
9595
}
9696
// delete config in parent config file
97-
const configData = await this.getConfigFileForKey(configKey)
97+
const configData = await this.getConfigFileForKey(configKey, flags)
9898
deleteUserConfig(configData)
9999
}
100100
}

0 commit comments

Comments
 (0)