4
4
using System . IO ;
5
5
using CommandLine ;
6
6
using System . Reflection ;
7
+ using System . Net ;
8
+ using System . Text ;
9
+ using System . Xml . XPath ;
10
+ using System . Xml ;
7
11
8
12
namespace CLARiNET
9
13
{
@@ -56,16 +60,15 @@ static void Main(string[] args)
56
60
// Option UI
57
61
OptionsUI ( ) ;
58
62
63
+ // Post Init and UI Option Handling
59
64
switch ( options . Command )
60
65
{
61
66
case Command . CLAR_UPLOAD :
62
- cloudCollection = options . Parameters ;
67
+ case Command . CLAR_DOWNLOAD :
63
68
break ;
64
69
case Command . DRIVE_UPLOAD :
65
- soapUrl = SoapUrlBuild ( ) ;
66
- break ;
67
70
case Command . DRIVE_TRASH :
68
- soapUrl = SoapUrlBuild ( ) ;
71
+ soapUrl = SoapUrlBuild ( options . Command ) ;
69
72
break ;
70
73
}
71
74
@@ -76,12 +79,20 @@ static void Main(string[] args)
76
79
77
80
files = Directory . GetFiles ( options . Path , searchPattern , new EnumerationOptions { MatchCasing = MatchCasing . CaseInsensitive } ) ;
78
81
79
- if ( options . Command == Command . DRIVE_TRASH )
80
- {
81
- if ( files . Length > 0 )
82
- {
83
- files = File . ReadAllLines ( files [ 0 ] ) ;
84
- }
82
+ // Special file handling
83
+ switch ( options . Command )
84
+ {
85
+ case Command . CLAR_DOWNLOAD :
86
+ files = new string [ ] { Path . Combine ( options . Path , options . Parameters + "." + DateTime . Now . ToString ( "s" ) . Replace ( ":" , "." ) + ".clar" ) } ;
87
+ break ;
88
+ case Command . DRIVE_TRASH :
89
+ if ( files . Length > 0 )
90
+ {
91
+ files = File . ReadAllLines ( files [ 0 ] ) ;
92
+ }
93
+ break ;
94
+ default :
95
+ break ;
85
96
}
86
97
87
98
foreach ( string file in files )
@@ -93,7 +104,18 @@ static void Main(string[] args)
93
104
case Command . CLAR_UPLOAD :
94
105
bytes = File . ReadAllBytes ( file ) ;
95
106
Console . WriteLine ( "\n \n Deploying the CLAR and awaiting the result...\n \n " ) ;
96
- result = WDWebService . CallRest ( options . Tenant , options . Username + "@" + options . Tenant , options . Password , restUrl , "PUT" , bytes ) ;
107
+ result = Encoding . Default . GetString ( WDWebService . CallRest ( options . Tenant , options . Username + "@" + options . Tenant , options . Password , restUrl , WebRequestMethods . Http . Put , bytes ) ) ;
108
+ break ;
109
+ case Command . CLAR_DOWNLOAD :
110
+ Console . WriteLine ( "\n \n Downloading the CLAR and awaiting the result...\n \n " ) ;
111
+ bytes = WDWebService . CallRest ( options . Tenant , options . Username + "@" + options . Tenant , options . Password , restUrl + "?fmt=clar" , WebRequestMethods . Http . Get , null ) ;
112
+ File . WriteAllBytes ( file , bytes ) ;
113
+ result = Encoding . Default . GetString ( WDWebService . CallRest ( options . Tenant , options . Username + "@" + options . Tenant , options . Password , restUrl , WebRequestMethods . Http . Get , null ) ) ;
114
+ File . WriteAllText ( file . Replace ( ".clar" , ".xml" ) , result ) ;
115
+ XDocument xDoc = XDocument . Parse ( result ) ;
116
+ XmlNamespaceManager xnm = new XmlNamespaceManager ( new NameTable ( ) ) ;
117
+ xnm . AddNamespace ( "default" , "urn:com.workday/esb/cloud/10.0" ) ;
118
+ result = "Last Uploaded to Workday: " + DateTime . Parse ( xDoc . XPathSelectElement ( "//default:deployed-since" , xnm ) . Value ) . ToLocalTime ( ) . ToString ( "s" ) ;
97
119
break ;
98
120
case Command . DRIVE_UPLOAD :
99
121
bytes = File . ReadAllBytes ( file ) ;
@@ -222,13 +244,25 @@ static bool InitOptions(string[] args)
222
244
options . Command = options . Command . Trim ( ) . ToUpper ( ) ;
223
245
}
224
246
225
- // Set search pattern if parameters are included.
247
+ // Set search pattern and cloud collection if parameters are included.
226
248
if ( options != null && options . Parameters != null )
227
249
{
228
- if ( options . Command != Command . CLAR_UPLOAD )
250
+ switch ( options . Command )
229
251
{
230
- searchPattern = options . Parameters ;
231
- }
252
+ case Command . CLAR_UPLOAD :
253
+ case Command . CLAR_DOWNLOAD :
254
+ cloudCollection = options . Parameters ;
255
+ if ( options . Command == Command . CLAR_DOWNLOAD )
256
+ {
257
+ searchPattern = "" ;
258
+ options . Parameters = Path . GetFileName ( Path . TrimEndingDirectorySeparator ( options . Parameters ) ) ;
259
+ }
260
+ break ;
261
+ case Command . DRIVE_UPLOAD :
262
+ case Command . DRIVE_TRASH :
263
+ searchPattern = options . Parameters ;
264
+ break ;
265
+ }
232
266
}
233
267
234
268
return true ;
@@ -282,9 +316,10 @@ static void CommandOption()
282
316
// Check for valid commands
283
317
switch ( options . Command )
284
318
{
285
- case Command . DRIVE_UPLOAD :
286
- case Command . DRIVE_TRASH :
287
319
case Command . CLAR_UPLOAD :
320
+ case Command . CLAR_DOWNLOAD :
321
+ case Command . DRIVE_UPLOAD :
322
+ case Command . DRIVE_TRASH :
288
323
break ;
289
324
default :
290
325
throw new Exception ( "Invalid command. Please use --help for a list of valid commands." ) ;
@@ -301,6 +336,7 @@ static void PathOption()
301
336
switch ( options . Command )
302
337
{
303
338
case Command . CLAR_UPLOAD :
339
+ case Command . CLAR_DOWNLOAD :
304
340
options . Path = appDir ;
305
341
break ;
306
342
case Command . DRIVE_UPLOAD :
@@ -321,11 +357,16 @@ static void ParameterOption()
321
357
switch ( options . Command )
322
358
{
323
359
case Command . CLAR_UPLOAD :
360
+ case Command . CLAR_DOWNLOAD :
324
361
Console . WriteLine ( "Enter the Cloud Collection:\n " ) ;
325
362
cloudCollection = Console . ReadLine ( ) . Trim ( ) ;
326
- Console . WriteLine ( "" ) ;
363
+ Console . WriteLine ( "" ) ;
327
364
options . Parameters = cloudCollection ;
328
365
searchPattern = "*.clar" ;
366
+ if ( options . Command == Command . CLAR_DOWNLOAD )
367
+ {
368
+ searchPattern = "" ;
369
+ }
329
370
break ;
330
371
case Command . DRIVE_UPLOAD :
331
372
options . Parameters = "*.*" ;
@@ -337,6 +378,7 @@ static void ParameterOption()
337
378
break ;
338
379
}
339
380
}
381
+
340
382
Console . WriteLine ( "Using parameters: " + options . Parameters + "\n " ) ;
341
383
}
342
384
@@ -357,7 +399,17 @@ static void FilesCheck()
357
399
}
358
400
else
359
401
{
360
- throw new Exception ( "No files found." ) ;
402
+ if ( options . Command == Command . CLAR_DOWNLOAD )
403
+ {
404
+ if ( ! Directory . Exists ( options . Path ) )
405
+ {
406
+ throw new Exception ( "Directory does not exist." ) ;
407
+ }
408
+ }
409
+ else
410
+ {
411
+ throw new Exception ( "No files found." ) ;
412
+ }
361
413
}
362
414
}
363
415
@@ -430,11 +482,18 @@ static void PasswordOption()
430
482
}
431
483
}
432
484
433
- static string SoapUrlBuild ( )
485
+ static string SoapUrlBuild ( string command )
434
486
{
435
487
string soapUrl = ccxUrl . Replace ( "{host}" , host ) ;
436
488
soapUrl = WDWebService . GetServiceURL ( soapUrl , options . Tenant , options . Username , options . Password ) ;
437
- soapUrl += "/{tenant}/Drive/{version}" ;
489
+ switch ( options . Command )
490
+ {
491
+ case Command . DRIVE_UPLOAD :
492
+ case Command . DRIVE_TRASH :
493
+ soapUrl += "/{tenant}/Drive/{version}" ;
494
+ break ;
495
+ }
496
+
438
497
return soapUrl ;
439
498
}
440
499
0 commit comments