@@ -9,23 +9,47 @@ const { readdirSync, readFileSync } = require('fs');
99let config = { } ;
1010
1111class StackCloneCommand extends Command {
12+ /**
13+ * Determine authentication method based on user preference
14+ */
15+ determineAuthenticationMethod ( sourceManagementTokenAlias , destinationManagementTokenAlias ) {
16+ // Track authentication method
17+ let authenticationMethod = 'unknown' ;
18+
19+ // Determine authentication method based on user preference
20+ if ( sourceManagementTokenAlias || destinationManagementTokenAlias ) {
21+ authenticationMethod = 'Management Token' ;
22+ } else if ( isAuthenticated ( ) ) {
23+ // Check if user is authenticated via OAuth
24+ const isOAuthUser = configHandler . get ( 'authorisationType' ) === 'OAUTH' || false ;
25+ if ( isOAuthUser ) {
26+ authenticationMethod = 'OAuth' ;
27+ } else {
28+ authenticationMethod = 'Basic Auth' ;
29+ }
30+ } else {
31+ authenticationMethod = 'Basic Auth' ;
32+ }
33+
34+ return authenticationMethod ;
35+ }
36+
1237 /**
1338 * Create clone context object for logging
1439 */
15- createCloneContext ( ) {
40+ createCloneContext ( authenticationMethod ) {
1641 return {
1742 command : this . context ?. info ?. command || 'cm:stacks:clone' ,
1843 module : 'clone' ,
1944 email : configHandler . get ( 'email' ) || '' ,
2045 sessionId : this . context ?. sessionId || '' ,
21- authenticationMethod : configHandler . get ( ' authenticationMethod' ) || '' ,
46+ authenticationMethod : authenticationMethod || 'Basic Auth ' ,
2247 } ;
2348 }
2449
2550 async run ( ) {
2651 try {
2752 let self = this ;
28- const cloneContext = this . createCloneContext ( ) ;
2953 const { flags : cloneCommandFlags } = await self . parse ( StackCloneCommand ) ;
3054 const {
3155 yes,
@@ -44,8 +68,13 @@ class StackCloneCommand extends Command {
4468 } = cloneCommandFlags ;
4569
4670 const handleClone = async ( ) => {
47- log . debug ( 'Starting clone operation setup' , cloneContext ) ;
4871 const listOfTokens = configHandler . get ( 'tokens' ) ;
72+ const authenticationMethod = this . determineAuthenticationMethod (
73+ sourceManagementTokenAlias ,
74+ destinationManagementTokenAlias ,
75+ ) ;
76+ const cloneContext = this . createCloneContext ( authenticationMethod ) ;
77+ log . debug ( 'Starting clone operation setup' , cloneContext ) ;
4978
5079 if ( externalConfigPath ) {
5180 log . debug ( `Loading external configuration from: ${ externalConfigPath } ` , cloneContext ) ;
@@ -55,7 +84,8 @@ class StackCloneCommand extends Command {
5584 }
5685 config . forceStopMarketplaceAppsPrompt = yes ;
5786 config . skipAudit = cloneCommandFlags [ 'skip-audit' ] ;
58- log . debug ( 'Clone configuration prepared' , cloneContext , {
87+ log . debug ( 'Clone configuration prepared' , {
88+ ...cloneContext ,
5989 cloneType : config . cloneType ,
6090 skipAudit : config . skipAudit ,
6191 forceStopMarketplaceAppsPrompt : config . forceStopMarketplaceAppsPrompt
@@ -123,7 +153,7 @@ class StackCloneCommand extends Command {
123153 cloneHandler . setClient ( managementAPIClient ) ;
124154 log . debug ( 'Starting clone operation' , cloneContext ) ;
125155 cloneHandler . execute ( ) . catch ( ( error ) => {
126- log . error ( 'Clone operation failed' , cloneContext , { error } ) ;
156+ log . error ( 'Clone operation failed' , { ... cloneContext , error } ) ;
127157 } ) ;
128158 } ;
129159
@@ -132,7 +162,7 @@ class StackCloneCommand extends Command {
132162 if ( isAuthenticated ( ) ) {
133163 handleClone ( ) ;
134164 } else {
135- log . warn ( 'Please login to execute this command, csdx auth:login' , cloneContext ) ;
165+ log . error ( 'Please login to execute this command, csdx auth:login' , cloneContext ) ;
136166 this . exit ( 1 ) ;
137167 }
138168 } else {
@@ -141,13 +171,13 @@ class StackCloneCommand extends Command {
141171 } else if ( isAuthenticated ( ) ) {
142172 handleClone ( ) ;
143173 } else {
144- log . warn ( 'Please login to execute this command, csdx auth:login' , cloneContext ) ;
174+ log . error ( 'Please login to execute this command, csdx auth:login' , cloneContext ) ;
145175 this . exit ( 1 ) ;
146176 }
147177 } catch ( error ) {
148178 if ( error ) {
149179 await this . cleanUp ( pathdir , null , cloneContext ) ;
150- log . error ( 'Stack clone command failed' , cloneContext , { error : error . message || error } ) ;
180+ log . error ( 'Stack clone command failed' , { ... cloneContext , error : error ? .message || error } ) ;
151181 }
152182 }
153183 }
@@ -156,37 +186,37 @@ class StackCloneCommand extends Command {
156186
157187 async removeContentDirIfNotEmptyBeforeClone ( dir , cloneContext ) {
158188 try {
159- log . debug ( 'Checking if content directory is empty' , cloneContext , { dir } ) ;
189+ log . debug ( 'Checking if content directory is empty' , { ... cloneContext , dir } ) ;
160190 const dirNotEmpty = readdirSync ( dir ) . length ;
161191
162192 if ( dirNotEmpty ) {
163- log . debug ( 'Content directory is not empty, cleaning up' , cloneContext , { dir } ) ;
193+ log . debug ( 'Content directory is not empty, cleaning up' , { ... cloneContext , dir } ) ;
164194 await this . cleanUp ( dir , null , cloneContext ) ;
165195 }
166196 } catch ( error ) {
167197 const omit = [ 'ENOENT' ] ; // NOTE add emittable error codes in the array
168198
169199 if ( ! omit . includes ( error . code ) ) {
170- log . error ( 'Error checking content directory' , cloneContext , { error : error . message , code : error . code } ) ;
200+ log . error ( 'Error checking content directory' , { ... cloneContext , error : error ? .message , code : error . code } ) ;
171201 }
172202 }
173203 }
174204
175205 async cleanUp ( pathDir , message , cloneContext ) {
176206 try {
177- log . debug ( 'Starting cleanup' , cloneContext , { pathDir } ) ;
207+ log . debug ( 'Starting cleanup' , { ... cloneContext , pathDir } ) ;
178208 await rimraf ( pathDir ) ;
179209 if ( message ) {
180210 log . info ( message , cloneContext ) ;
181211 }
182- log . debug ( 'Cleanup completed' , cloneContext , { pathDir } ) ;
212+ log . debug ( 'Cleanup completed' , { ... cloneContext , pathDir } ) ;
183213 } catch ( err ) {
184214 if ( err ) {
185215 log . debug ( 'Cleaning up' , cloneContext ) ;
186216 const skipCodeArr = [ 'ENOENT' , 'EBUSY' , 'EPERM' , 'EMFILE' , 'ENOTEMPTY' ] ;
187217
188218 if ( skipCodeArr . includes ( err . code ) ) {
189- log . debug ( 'Cleanup error code is in skip list, exiting' , cloneContext , { code : err . code } ) ;
219+ log . debug ( 'Cleanup error code is in skip list, exiting' , { ... cloneContext , code : err ? .code } ) ;
190220 process . exit ( ) ;
191221 }
192222 }
@@ -205,12 +235,12 @@ class StackCloneCommand extends Command {
205235
206236 if ( exitOrError instanceof Promise ) {
207237 exitOrError . catch ( ( error ) => {
208- log . error ( 'Error during cleanup' , cloneContext , { error : ( error && error . message ) || '' } ) ;
238+ log . error ( 'Error during cleanup' , { ... cloneContext , error : ( error && error ? .message ) || '' } ) ;
209239 } ) ;
210240 } else if ( exitOrError . message ) {
211- log . error ( 'Cleanup error' , cloneContext , { error : exitOrError . message } ) ;
241+ log . error ( 'Cleanup error' , { ... cloneContext , error : exitOrError ? .message } ) ;
212242 } else if ( exitOrError . errorMessage ) {
213- log . error ( 'Cleanup error' , cloneContext , { error : exitOrError . message } ) ;
243+ log . error ( 'Cleanup error' , { ... cloneContext , error : exitOrError ?. errorMessage } ) ;
214244 }
215245
216246 if ( exitOrError === true ) process . exit ( ) ;
0 commit comments