-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProgram.cs
More file actions
335 lines (273 loc) · 12.3 KB
/
Copy pathProgram.cs
File metadata and controls
335 lines (273 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
namespace SyncPro.Cmd
{
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using SyncPro.Adapters;
using SyncPro.Adapters.MicrosoftOneDrive;
using SyncPro.Data;
using SyncPro.OAuth;
using SyncPro.Runtime;
using SyncPro.Utility;
class Program
{
static void Main(string[] arguments)
{
Dictionary<string, string> args = CommandLineHelper.ParseCommandLineArgs(arguments);
try
{
if (args.ContainsKey("dumpConfig"))
{
DumpConfig();
}
if (args.ContainsKey("dumpDatabase"))
{
DumpDatabase(args);
}
else if (args.ContainsKey("extractToken"))
{
ExtractToken(args);
}
else if (args.ContainsKey("setToken"))
{
SetToken(args);
}
else if (args.ContainsKey("reset"))
{
Reset(args);
}
else
{
throw new Exception("Invalid command line syntax");
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
private static void Reset(Dictionary<string, string> args)
{
Global.Initialize(false);
SyncRelationship relationship = GetRelationship(args);
using (var db = relationship.GetDatabase())
{
var rootEntry = db.Entries.First(e => e.ParentId == null || e.ParentId == 0);
var entryCount = db.Entries.Count();
Console.WriteLine("Removing {0} entries", entryCount);
foreach (SyncEntry syncEntry in db.Entries.Where(e => e.ParentId != null && e.ParentId != 0))
{
db.Entries.Remove(syncEntry);
}
var entryACount = db.AdapterEntries.Count();
Console.WriteLine("Removing {0} adapter entries", entryACount);
foreach (SyncEntryAdapterData adapterData in db.AdapterEntries.Where(e => e.SyncEntryId != rootEntry.Id))
{
db.AdapterEntries.Remove(adapterData);
}
var historyCount = db.History.Count();
Console.WriteLine("Removing {0} histories", historyCount);
db.Database.ExecuteSqlCommand("TRUNCATE TABLE [HistoryEntries]");
var historyEntryCount = db.HistoryEntries.Count();
Console.WriteLine("Removing {0} history entries", historyEntryCount);
db.Database.ExecuteSqlCommand("TRUNCATE TABLE [History]");
db.SaveChanges();
}
}
private static void DumpConfig()
{
Global.Initialize(false);
string localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string appDataRoot = Path.Combine(localAppDataPath, "SyncPro");
DirectoryInfo appDataRootDir = new DirectoryInfo(appDataRoot);
foreach (DirectoryInfo relationshipDir in appDataRootDir.GetDirectories())
{
Guid guid;
if (!Guid.TryParse(relationshipDir.Name, out guid))
{
WriteWarning("Failed to parse relationship directory '{0}' as a GUID", relationshipDir.Name);
continue;
}
SyncRelationship relationship = SyncRelationship.Load(guid);
//relationship.BeginInitialize();
// TODO: Do we really need to initialize the relationship in order to dump the configuration?
//relationship.InitializeAsync().Wait();
Console.WriteLine("---------------------------- [Relationship] ----------------------------");
Console.WriteLine("RelationshipId: " + relationship.Configuration.RelationshipId);
Console.WriteLine("Name: " + relationship.Configuration.Name);
Console.WriteLine("Description: " + relationship.Configuration.Description);
Console.WriteLine("Scope: " + relationship.Configuration.Scope);
Console.WriteLine("SyncAttributes: " + relationship.Configuration.SyncAttributes);
Console.WriteLine("TriggerType: " + relationship.Configuration.TriggerConfiguration.TriggerType);
Console.WriteLine("SourceAdapter: " + relationship.Configuration.SourceAdapterId);
Console.WriteLine("DestinationAdapter: " + relationship.Configuration.DestinationAdapterId);
Console.WriteLine();
Console.WriteLine("---------- [Adapters] ---------- ");
foreach (AdapterBase adapter in relationship.Adapters)
{
Console.WriteLine("Id: " + adapter.Configuration.Id);
Console.WriteLine("AdapterTypeId: " + adapter.Configuration.AdapterTypeId);
Console.WriteLine("AdapterTypeName: " +
AdapterRegistry.GetRegistrationByTypeId(adapter.Configuration.AdapterTypeId).AdapterType.Name);
Console.WriteLine("IsOriginator: " + adapter.Configuration.IsOriginator);
Console.WriteLine("Flags: " +
string.Join(",", StringExtensions.GetSetFlagNames<Data.AdapterFlags>(adapter.Configuration.Flags)));
Console.WriteLine();
}
Console.WriteLine();
}
}
private static void DumpDatabase(Dictionary<string, string> args)
{
Global.Initialize(false);
SyncRelationship relationship = GetRelationship(args);
using (var db = relationship.GetDatabase())
{
int i = 0;
foreach (SyncEntry syncEntry in db.Entries)
{
i++;
Console.WriteLine(
"| {0} | {1} | {2} | {3} | {4} ",
syncEntry.Id,
syncEntry.ParentId,
syncEntry.Name,
syncEntry.State,
syncEntry.Type);
}
Console.WriteLine("Total Entries: " + i);
Console.WriteLine("----------------------------------");
var historyList = db.History.ToList();
foreach (var history in historyList)
{
Console.WriteLine(
"History: {0}, {1}, {2}",
history.Id,
history.TotalFiles,
history.TotalBytes);
i = 0;
foreach (SyncHistoryEntryData entryData in db.HistoryEntries.Where(e => e.SyncHistoryId == history.Id))
{
i++;
Console.WriteLine(
"| {0} | {1} | {2} | {3} | {4} ",
entryData.Id,
entryData.PathNew,
entryData.Result,
entryData.Flags,
entryData.SyncEntryId);
}
Console.WriteLine("Total history entries: " + i);
Console.WriteLine("----------------------------------");
}
}
}
private static void ExtractToken(Dictionary<string, string> args)
{
Global.Initialize(false);
AdapterBase adapter = GetAdapter(args);
bool formatToken = args.ContainsKey("formatToken");
string file;
args.TryGetValue("file", out file);
if (adapter.Configuration.AdapterTypeId == OneDriveAdapter.TargetTypeId)
{
TokenResponse token = ((OneDriveAdapterConfiguration) adapter.Configuration).CurrentToken;
if (args.ContainsKey("decrypt"))
{
token.Unprotect();
}
string formattedToken = JsonConvert.SerializeObject(token, formatToken ? Formatting.Indented : Formatting.None);
if (string.IsNullOrEmpty(file))
{
Console.WriteLine(formattedToken);
}
else
{
File.WriteAllText(file, formattedToken);
}
}
else
{
AdapterRegistration registration =
AdapterRegistry.GetRegistrationByTypeId(adapter.Configuration.AdapterTypeId);
throw new Exception(
string.Format("Cannot extract token from adapter with type {0} ({1})",
registration.AdapterType.Name, adapter.Configuration.AdapterTypeId));
}
}
private static void SetToken(Dictionary<string, string> args)
{
Global.Initialize(false);
AdapterBase adapter = GetAdapter(args);
string file;
if (!args.TryGetValue("file", out file))
{
throw new Exception("/file is required");
}
if (adapter.Configuration.AdapterTypeId == OneDriveAdapter.TargetTypeId)
{
string tokenContent = File.ReadAllText(file);
TokenResponse token = JsonConvert.DeserializeObject<TokenResponse>(tokenContent);
// Encrypt the token if not already encrypted
token.Protect();
((OneDriveAdapterConfiguration) adapter.Configuration).CurrentToken = token;
adapter.SaveConfiguration();
}
else
{
AdapterRegistration registration =
AdapterRegistry.GetRegistrationByTypeId(adapter.Configuration.AdapterTypeId);
throw new Exception(
string.Format("Cannot set token from adapter with type {0} ({1})",
registration.AdapterType.Name, adapter.Configuration.AdapterTypeId));
}
}
private static SyncRelationship GetRelationship(Dictionary<string, string> args)
{
string strRelationshipId;
if (!args.TryGetValue("relationshipId", out strRelationshipId))
{
throw new Exception("/relationshipId parameter not provided.");
}
Guid relationshipId = Guid.Parse(strRelationshipId);
string localAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string appDataRoot = Path.Combine(localAppDataPath, "SyncPro");
DirectoryInfo appDataRootDir = new DirectoryInfo(appDataRoot);
foreach (DirectoryInfo relationshipDir in appDataRootDir.GetDirectories())
{
Guid guid;
if (Guid.TryParse(relationshipDir.Name, out guid) && guid == relationshipId)
{
SyncRelationship relationship = SyncRelationship.Load(guid);
return relationship;
}
}
throw new Exception("No relationship found with ID " + relationshipId);
}
private static AdapterBase GetAdapter(Dictionary<string, string> args)
{
SyncRelationship relationship = GetRelationship(args);
string strAdapterId;
if (!args.TryGetValue("adapterId", out strAdapterId))
{
throw new Exception("/adapterId parameter not provided.");
}
int adapterId = int.Parse(strAdapterId);
AdapterBase adapter = relationship.Adapters.FirstOrDefault(a => a.Configuration.Id == adapterId);
if (adapter == null)
{
throw new Exception("No adapter found with ID " + adapterId);
}
return adapter;
}
private static void WriteWarning(string format, params object[] args)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(format, args);
Console.ResetColor();
}
}
}