@@ -22,13 +22,28 @@ const { getPipedData } = require('@adobe/aio-lib-core-config')
2222const _ = require ( 'lodash' )
2323
2424class BaseVariablesCommand extends Command {
25- outputTable ( result , flags ) {
25+ getFlagDefs ( ) {
26+ return {
27+ variable : {
28+ type : 'string' ,
29+ } ,
30+ secret : {
31+ type : 'secretString' ,
32+ } ,
33+ delete : {
34+ action : 'delete' ,
35+ } ,
36+ }
37+ }
38+
39+ outputTable ( result , flags , extraColumns = { } ) {
2640 cli . table ( result , {
2741 name : { } ,
2842 type : { } ,
2943 value : {
3044 get : ( item ) => item . type === 'secretString' ? '****' : item . value ,
3145 } ,
46+ ...extraColumns ,
3247 } , {
3348 output : getOutputFormat ( flags ) ,
3449 } )
@@ -63,44 +78,55 @@ class BaseVariablesCommand extends Command {
6378
6479 async prepareVariableList ( flags , currentVariablesList ) {
6580 const currentVariableTypes = { }
66- currentVariablesList . forEach ( variable => ( currentVariableTypes [ variable . name ] = variable . type ) )
81+ currentVariablesList . forEach ( variable => {
82+ const tempName = variable . service ? `${ variable . service } :${ variable . name } ` : variable . name
83+ currentVariableTypes [ tempName ] = variable . type
84+ } )
6785
6886 const variables = [ ]
69- if ( flags . variable ) {
70- // each --param flag expects two values ( a key and a value ). Multiple --parm flags can be passed
71- // For example : aio runtime:action:create --param name "foo" --param city "bar"
72- const parsedVariables = createKeyValueObjectFromFlag ( flags . variable )
73- for ( const key in parsedVariables ) {
74- variables . push ( {
75- name : key ,
76- value : parsedVariables [ key ] ,
77- type : 'string' ,
78- } )
79- }
80- }
81- if ( flags . secret ) {
82- const parsedSecrets = createKeyValueObjectFromFlag ( flags . secret )
83- for ( const key in parsedSecrets ) {
84- variables . push ( {
85- name : key ,
86- value : parsedSecrets [ key ] ,
87- type : 'secretString' ,
88- } )
87+
88+ const flagDefs = this . getFlagDefs ( )
89+
90+ Object . keys ( flagDefs ) . forEach ( flagName => {
91+ const flagDef = flagDefs [ flagName ]
92+ if ( ! flags [ flagName ] ) {
93+ return
8994 }
90- }
91- if ( flags . delete ) {
92- flags . delete . forEach ( key => {
93- if ( currentVariableTypes [ key ] ) {
94- variables . push ( {
95- name : key ,
96- type : currentVariableTypes [ key ] ,
97- value : '' ,
95+ switch ( flagDef . action ) {
96+ case 'delete' :
97+ flags [ flagName ] . forEach ( key => {
98+ const currentVariableKey = flagDef . service ? `${ flagDef . service } :${ key } ` : key
99+ if ( currentVariableTypes [ currentVariableKey ] ) {
100+ const newVar = {
101+ name : key ,
102+ type : currentVariableTypes [ currentVariableKey ] ,
103+ value : '' ,
104+ }
105+ if ( flagDef . service ) {
106+ newVar . service = flagDef . service
107+ }
108+ variables . push ( newVar )
109+ } else {
110+ this . warn ( `Variable ${ key } not found. Will not try to delete.` )
111+ }
98112 } )
99- } else {
100- this . warn ( `Variable ${ key } not found. Will not try to delete.` )
113+ break
114+ default : {
115+ const parsedFlag = createKeyValueObjectFromFlag ( flags [ flagName ] )
116+ for ( const key in parsedFlag ) {
117+ const newVar = {
118+ name : key ,
119+ value : parsedFlag [ key ] ,
120+ type : flagDef . type ,
121+ }
122+ if ( flagDef . service ) {
123+ newVar . service = flagDef . service
124+ }
125+ variables . push ( newVar )
126+ }
101127 }
102- } )
103- }
128+ }
129+ } )
104130
105131 if ( flags . jsonStdin ) {
106132 const rawStdinData = await getPipedData ( )
@@ -129,18 +155,19 @@ class BaseVariablesCommand extends Command {
129155 if ( ! item . type ) {
130156 item . type = 'string'
131157 }
132- if ( currentVariableTypes [ item . name ] && ! item . value ) {
133- item . type = currentVariableTypes [ item . name ]
158+ const currentVariableKey = item . service ? `${ item . service } :${ item . name } ` : item . name
159+ if ( currentVariableTypes [ currentVariableKey ] && ! item . value ) {
160+ item . type = currentVariableTypes [ currentVariableKey ]
134161 }
135- if ( ! variables . find ( variable => variable . name === item . name ) ) {
162+ if ( ! variables . find ( variable => variable . name === item . name && variables . services === item . service ) ) {
136163 variables . push ( item )
137164 }
138165 }
139166 } )
140167 }
141168}
142169
143- BaseVariablesCommand . setterFlags = {
170+ BaseVariablesCommand . coreSetterFlags = {
144171 variable : flags . string ( {
145172 char : 'v' ,
146173 description : 'variable values in KEY VALUE format' ,
@@ -156,6 +183,10 @@ BaseVariablesCommand.setterFlags = {
156183 description : 'variables/secrets to delete' ,
157184 multiple : true ,
158185 } ) ,
186+ }
187+
188+ BaseVariablesCommand . setterFlags = {
189+ ...BaseVariablesCommand . coreSetterFlags ,
159190 jsonStdin : flags . boolean ( {
160191 default : false ,
161192 description : 'if set, read variables from a JSON array provided as standard input; variables set through --variable or --secret flag will take precedence' ,
0 commit comments