-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
149 lines (133 loc) · 6.53 KB
/
Program.cs
File metadata and controls
149 lines (133 loc) · 6.53 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
using FoldSync.Methods;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace FoldSync
{
class Program
{
public static async Task Main()
{
#region class instances
CommonTools commonTools = new CommonTools();
FolderTools folderTools = new FolderTools();
Synchronizer synchronizer = new Synchronizer();
Logger logger = new Logger();
#endregion
#region variables declaration
string sourcePath = string.Empty,
targetPath = string.Empty,
tempStr = string.Empty,
result = string.Empty;
int schedulerTime = -1;
DateTime dtNow = new DateTime();
string[,] sourceFiles;
#endregion
#region header
commonTools.PrintMessage(@".-------------------------. .-----------------------------------.
| | This program will synchronize | |
| ____ ____ ____ ____ | two given directories | |
| ||F |||O |||L |||D || | (source with target) | |
| ||__|||__|||__|||__|| | in given scheduler period. | ____ ____ ____ ____ ____ ____ |
| |/__\|/__\|/__\|/__\| | | ||E |||N |||J |||O |||Y |||! || |
| ____ ____ ____ ____ | The log file with results | ||__|||__|||__|||__|||__|||__|| |
| ||S |||Y |||N |||C || | will be created in given directory. | |/__\|/__\|/__\|/__\|/__\|/__\| |
| ||__|||__|||__|||__|| | | |
| |/__\|/__\|/__\|/__\| | Please follow the instructions below | |
| | and... | |
'-------------------------' '-----------------------------------'");
#endregion
#region main program
#region LOG directory
logger.GenerateCommonLog("INFO", "OK", "start", message: "application start", console: true, file: false);
commonTools.PrintMessage(Environment.NewLine + ".-------------------------------------------------L-O-G-------------------------------------------------.");
commonTools.PrintMessage("Please provide absolute path to directory where the log file should be created:");
logger.CreateLogFile();
#endregion
#region SOURCE directory
commonTools.PrintMessage(Environment.NewLine + ".-----------------------------------------------S-O-U-R-C-E----------------------------------------------.");
commonTools.PrintMessage("Please provide below the full path to the SOURCE directory:");
do { sourcePath = commonTools.UserInput(); }
while (!folderTools.VerifyFolder(sourcePath));
#endregion
#region TARGET directory
commonTools.PrintMessage(Environment.NewLine + ".-----------------------------------------------T-A-R-G-E-T----------------------------------------------.");
commonTools.PrintMessage("Please provide below the full path to the TARGET directory:");
do targetPath = commonTools.UserInput();
while (!folderTools.CreateFolder(targetPath));
#endregion
#region setting the SHEDULER
commonTools.PrintMessage(Environment.NewLine + ".--------------------------------------------S-C-H-E-D-U-L-E-R-------------------------------------------.");
commonTools.PrintMessage("Please provide how often the task should repeat (in minutes, type 0 to skip scheduler):");
do {
result = commonTools.UserInput();
if (int.TryParse(result, out schedulerTime))
{
if (schedulerTime == 0)
{
logger.GenerateCommonLog("INFO", "OK", "scheduler", "scheduler not set", console: true, file: true);
commonTools.PrintMessage("Scheduler has not been set, the task will be performed one time");
}
else if (schedulerTime < 0)
{
logger.GenerateCommonLog("ERROR", "RETRY", "scheduler", "negative number entered", console: true, file: true);
commonTools.PrintMessage($"Negative value cannot be use to set scheduler time, please enter the value again:");
}
else
{
logger.GenerateCommonLog("INFO", "OK", "scheduler", $"scheduler set to '{schedulerTime}' minutes", console: true, file: true);
commonTools.PrintMessage($"Scheduler has been set, the task will be performed each '{schedulerTime}' minutes.");
}
}
else
{
logger.GenerateCommonLog("ERROR", "RETRY", "scheduler", "invalid number entered", console: true, file: true);
commonTools.PrintMessage($"'{result}' is not a valid number, please enter the value again.");
schedulerTime = -1;
}
} while (schedulerTime < 0);
#endregion
#region sync and repeat process
bool userEscaped = false;
do
{
dtNow = DateTime.Now;
sourceFiles = folderTools.GenerateFilesList(sourcePath);
synchronizer.SynchronizeFiles(sourceFiles, sourcePath, targetPath);
if (schedulerTime == 0)
break;
commonTools.PrintMessage(Environment.NewLine + ".----------------------------------W-A-I-T-I-N-G--F-O-R--N-E-X-T--R-U-N----------------------------------.");
commonTools.PrintMessage("Please press [Enter] to start next synchronization cycle or [Esc] to close the application.");
commonTools.PrintMessage(System.String.Join("", "Next synchronization cycle will start in ", schedulerTime, " minutes automatically (at ", DateTime.Now.AddMinutes(schedulerTime).ToString("HH:mm:ss"), ")"));
var delayTask = synchronizer.WaitForNextRun(schedulerTime, dtNow);
var keyTask = CommonTools.WaitForKeyPressAsync();
var finisedTask = await Task.WhenAny(delayTask, keyTask);
if (finisedTask == keyTask)
{
var key = keyTask.Result;
if (key == ConsoleKey.Escape)
{
userEscaped = true;
break;
}
}
} while (!userEscaped);
#endregion
#region ending
TimeSpan timeAmount = new TimeSpan();
timeAmount = DateTime.Now - synchronizer.syncStartDT;
tempStr = commonTools.ConvertTimeSpanToString(timeAmount);
logger.GenerateCommonLog("INFO", "OK", "end", System.String.Join(" ", "task completed, task take", tempStr), console: true, file: true);
commonTools.PrintMessage($"Current run of the task ended. Please [Esc] key to close the aplication.");
while (true)
{
var keyInfo = Console.ReadKey(intercept: true);
if (keyInfo.Key == ConsoleKey.Escape)
{
logger.GenerateCommonLog("INFO", "OK", "close", "application closed by user", console: true, file: true);
break;
}
}
#endregion
#endregion
}
}
}