2
2
using System . Text . Json ;
3
3
using System . Text . RegularExpressions ;
4
4
using CommandLine ;
5
+ using Polly ;
5
6
using Spectre . Console ;
6
7
using SyncCodes ;
8
+ using Context = SyncCodes . Context ;
7
9
using Timer = System . Timers . Timer ;
8
10
9
11
var workBase = Directory . GetCurrentDirectory ( ) ;
30
32
31
33
var context = new Context ( workBase , app . Logger ) ;
32
34
35
+ context . InitializeFileSystemWatcher (
36
+ ( e ) =>
37
+ {
38
+ if ( context . Filter . ShouldIgnore ( e . FullPath ) == false )
39
+ app . Logger . LogInformation (
40
+ "[{time}] FileSystem Modified: {name}, {changeType} | {path}" ,
41
+ DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss" ) ,
42
+ e . Name ,
43
+ e . ChangeType ,
44
+ Path . GetRelativePath ( workBase , e . FullPath )
45
+ ) ;
46
+ context . RefreshFiles ( ) ;
47
+ }
48
+ ) ;
49
+
33
50
switch ( options . Job )
34
51
{
35
52
case Jobs . FetchCodes :
49
66
50
67
void RunServer ( Context context )
51
68
{
52
- context . InitializeFileSystemWatcher (
53
- ( e ) =>
54
- {
55
- if ( context . Filter . ShouldIgnore ( e . FullPath ) == false )
56
- app . Logger . LogInformation (
57
- "FileSystem Modified: {name}, {changeType} | {path}, [{time}]" ,
58
- e . Name ,
59
- e . ChangeType ,
60
- Path . GetRelativePath ( workBase , e . FullPath ) ,
61
- DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss" )
62
- ) ;
63
- context . RefreshFiles ( ) ;
64
- }
65
- ) ;
66
-
67
69
app . MapGet (
68
70
"/" ,
69
71
( ) => Results . Content (
@@ -98,8 +100,6 @@ void RunServer(Context context)
98
100
99
101
void RunClient ( Context context )
100
102
{
101
- context . InitializeFileSystemWatcher ( ( _ ) => context . RefreshFiles ( ) ) ;
102
-
103
103
var address = AnsiConsole . Prompt ( new TextPrompt < string > ( "Please input the server address: " )
104
104
{
105
105
Validator = ( s ) => ServerAddressRegex ( ) . IsMatch ( s )
@@ -138,20 +138,32 @@ await catalogResponse.Content.ReadAsStringAsync(),
138
138
var localCatalog = context . GetFiles ( ) ;
139
139
140
140
var differences = catalog . Concat ( localCatalog ) . GroupBy (
141
- f => f . Path ,
141
+ f => f . Path . RelatedTo ( workBase ) ,
142
142
f => f . Hash ,
143
143
( path , hashes ) => new
144
144
{
145
145
Path = path ,
146
146
Count = hashes . Count ( ) ,
147
- LocalHash = localCatalog . FirstOrDefault ( f => f . Path . Equals ( path ) ) ? . Hash ,
148
- RemoteHash = catalog . FirstOrDefault ( f => f . Path . Equals ( path ) ) ? . Hash ,
147
+ LocalHash = localCatalog . FirstOrDefault ( f => f . Path . RelatedTo ( workBase ) . Equals ( path ) ) ? . Hash ,
148
+ RemoteHash = catalog . FirstOrDefault ( f => f . Path . RelatedTo ( workBase ) . Equals ( path ) ) ? . Hash ,
149
149
}
150
150
) ;
151
151
152
- // app.Logger.LogInformation("Need to fetch: {json}", JsonSerializer.Serialize(needToFetch, jsonSerializerOptions));
153
- // app.Logger.LogInformation("Need to delete:: {json}", JsonSerializer.Serialize(needToDelete, jsonSerializerOptions));
154
- app . Logger . LogDebug ( "Differences: {json}" , JsonSerializer . Serialize ( differences ) ) ;
152
+ app . Logger . LogInformation (
153
+ "Remote: {json1}\n Local: {json2}\n Differences: {json3}" ,
154
+ JsonSerializer . Serialize (
155
+ differences . Where ( c => c . LocalHash is null && c . RemoteHash is not null ) ,
156
+ jsonSerializerOptions
157
+ ) ,
158
+ JsonSerializer . Serialize (
159
+ differences . Where ( c => c . RemoteHash is null && c . LocalHash is not null ) ,
160
+ jsonSerializerOptions
161
+ ) ,
162
+ JsonSerializer . Serialize (
163
+ differences . Where ( c => c . LocalHash is not null && c . RemoteHash is not null && ! c . LocalHash . Equals ( c . RemoteHash ) ) ,
164
+ jsonSerializerOptions
165
+ )
166
+ ) ;
155
167
} ;
156
168
timer . Start ( ) ;
157
169
@@ -165,6 +177,11 @@ await catalogResponse.Content.ReadAsStringAsync(),
165
177
)
166
178
) ;
167
179
180
+ app . MapGet (
181
+ "/catalog" ,
182
+ ( ) => context . GetFiles ( ) . Where ( f => f . FileLoaded )
183
+ ) ;
184
+
168
185
app . Run ( ) ;
169
186
}
170
187
@@ -198,3 +215,8 @@ partial class Program
198
215
[ GeneratedRegex ( @"([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)|(\[[0-9A-Fa-f:.]+\])|([a-zA-Z0-9.-]+)(:\d+)?" ) ]
199
216
private static partial Regex ServerAddressRegex ( ) ;
200
217
}
218
+
219
+ public static class Extensions
220
+ {
221
+ public static string RelatedTo ( this string path , string workBase ) => Path . GetRelativePath ( workBase , Path . Combine ( workBase , path ) ) ;
222
+ }
0 commit comments