Skip to content

Commit db0c731

Browse files
author
Marco Franceschi
committed
feat: Refactored Plugins execution after inserted scanned data
1 parent 953f519 commit db0c731

2 files changed

Lines changed: 76 additions & 47 deletions

File tree

src/commands/init.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ export default class Init extends Command {
197197
if (cloudGraphConfig) {
198198
configResult.cloudGraph = cloudGraphConfig
199199
}
200+
configResult.cloudGraph.plugins = {}
200201
this.saveCloudGraphConfigFile(configResult)
201202
this.logger.success(
202203
`Your config has been successfully stored at ${chalk.italic.green(

src/commands/scan.ts

Lines changed: 75 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import chalk from 'chalk'
22
import fs from 'fs'
33
import path from 'path'
4-
import { cloudGraphPlugin, Opts, pluginMap } from '@cloudgraph/sdk'
4+
import { Opts, pluginMap, PluginType, StorageEngine } from '@cloudgraph/sdk'
55
import { range } from 'lodash'
66

77
import Command from './base'
@@ -31,6 +31,66 @@ export default class Scan extends Command {
3131

3232
static args = Command.args
3333

34+
private async plugins({
35+
storage: { isRunning, engine },
36+
flags,
37+
}: {
38+
storage: {
39+
isRunning: boolean
40+
engine: StorageEngine
41+
}
42+
flags: {
43+
[flag: string]: any
44+
}
45+
}): Promise<void> {
46+
const config = this.getCGConfig('cloudGraph')
47+
const { plugins = {} } = config
48+
for (const pluginType in plugins) {
49+
if (pluginType) {
50+
try {
51+
// Get Plugin Interface
52+
const Plugin = pluginMap[pluginType]
53+
54+
// Execute Plugins by Provider
55+
for (const provider in this.providers) {
56+
if (provider) {
57+
const { schemasMap, serviceKey } = this.providers[provider]
58+
59+
// Initialize
60+
const PluginInstance = new Plugin({
61+
config,
62+
provider: {
63+
name: provider,
64+
schemasMap,
65+
serviceKey,
66+
},
67+
flags: flags as { [flag: string]: any },
68+
logger: this.logger,
69+
})
70+
71+
// Get the Plugin Manager
72+
const pluginManager = this.getPluginManager(
73+
pluginType as PluginType
74+
)
75+
76+
// Configure
77+
await PluginInstance.configure(pluginManager, plugins[pluginType])
78+
79+
// Execute plugins
80+
await PluginInstance.execute({
81+
storageRunning: isRunning,
82+
storageEngine: engine,
83+
processConnectionsBetweenEntities,
84+
})
85+
}
86+
}
87+
} catch (error) {
88+
this.logger.warn('Plugin not supported by CG')
89+
}
90+
}
91+
}
92+
}
93+
3494
async run() {
3595
const { argv, flags } = this.parse(Scan)
3696
const { dev: devMode } = flags as {
@@ -39,7 +99,6 @@ export default class Scan extends Command {
3999

40100
const { dataDir } = this.config
41101
const opts: Opts = { logger: this.logger, debug: true, devMode }
42-
const configuredPlugins = []
43102
let allProviders = argv
44103

45104
// Run dgraph health check
@@ -112,49 +171,18 @@ export default class Scan extends Command {
112171
this.logger.info(
113172
`Beginning ${chalk.italic.green('SCAN')} for ${provider}`
114173
)
115-
const { client: providerClient, schemasMap, serviceKey } =
116-
await this.getProviderClient(provider)
174+
175+
const providerPlugin = await this.getProviderClient(provider)
176+
const { client: providerClient, schemasMap } = providerPlugin
177+
117178
if (!providerClient) {
118179
failedProviderList.push(provider)
119180
this.logger.warn(`No valid client found for ${provider}, skipping...`)
120181
continue // eslint-disable-line no-continue
121182
}
122183
const config = this.getCGConfig(provider)
184+
this.providers[provider] = providerPlugin
123185

124-
// Configure installed plugins
125-
for (const key in config) {
126-
if (cloudGraphPlugin[key]) {
127-
try {
128-
// Get Plugin Interface
129-
const Plugin = pluginMap[cloudGraphPlugin[key]]
130-
131-
// Initialize
132-
const PluginInstance = new Plugin({
133-
config,
134-
provider: {
135-
name: provider,
136-
schemasMap,
137-
serviceKey,
138-
},
139-
flags: flags as { [flag: string]: any },
140-
logger: this.logger,
141-
})
142-
143-
// Get the Plugin Manager
144-
const pluginManager = this.getPluginManager(
145-
cloudGraphPlugin[key]
146-
)
147-
148-
// Configure
149-
await PluginInstance.configure(pluginManager)
150-
151-
// Add to Configured Plugins list
152-
configuredPlugins.push(PluginInstance)
153-
} catch (error) {
154-
this.logger.warn('Plugin not supported by CG')
155-
}
156-
}
157-
}
158186
this.logger.debug(config)
159187
if (!config) {
160188
failedProviderList.push(provider)
@@ -262,16 +290,16 @@ export default class Scan extends Command {
262290
await storageEngine.run()
263291

264292
this.logger.successSpinner('Data insertion into Dgraph complete')
265-
266-
// Execute plugins
267-
for (const plugin of configuredPlugins) {
268-
await plugin.execute({
269-
storageRunning,
270-
storageEngine,
271-
processConnectionsBetweenEntities,
272-
})
273-
}
274293
}
294+
295+
await this.plugins({
296+
flags: flags as { [flag: string]: any },
297+
storage: {
298+
isRunning: storageRunning,
299+
engine: storageEngine,
300+
},
301+
})
302+
275303
scanReport.print()
276304

277305
this.logger.success(

0 commit comments

Comments
 (0)