11import chalk from 'chalk'
22import fs from 'fs'
33import path from 'path'
4- import { cloudGraphPlugin , Opts , pluginMap } from '@cloudgraph/sdk'
4+ import { Opts , pluginMap , PluginType , StorageEngine } from '@cloudgraph/sdk'
55import { range } from 'lodash'
66
77import 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 )
@@ -263,15 +291,15 @@ export default class Scan extends Command {
263291
264292 this . logger . successSpinner ( 'Data insertion into Dgraph complete' )
265293
266- // Execute plugins
267- for ( const plugin of configuredPlugins ) {
268- await plugin . execute ( {
269- storageRunning,
270- storageEngine,
271- processConnectionsBetweenEntities,
272- } )
273- }
294+ await this . plugins ( {
295+ flags : flags as { [ flag : string ] : any } ,
296+ storage : {
297+ isRunning : storageRunning ,
298+ engine : storageEngine ,
299+ } ,
300+ } )
274301 }
302+
275303 scanReport . print ( )
276304
277305 this . logger . success (
0 commit comments