Skip to content

Commit 42ef558

Browse files
authored
Merge pull request #13 from TechieGuy12/fixes
Code fixes
2 parents 6f023fa + 27238de commit 42ef558

28 files changed

+414
-261
lines changed

FileWatcher/Configuration/Actions/Action.cs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ namespace TE.FileWatcher.Configuration.Actions
1515
public class Action : RunnableBase
1616
{
1717
// The placeholders for the destination path
18-
private Dictionary<string, string> _destinationPlaceholders
19-
= new Dictionary<string, string>();
18+
//private Dictionary<string, string> _destinationPlaceholders
19+
// = new Dictionary<string, string>();
2020

2121
// The placeholders for the source path
22-
private Dictionary<string, string> _sourcePlaceholders
23-
= new Dictionary<string, string>();
22+
//private Dictionary<string, string> _sourcePlaceholders
23+
// = new Dictionary<string, string>();
2424

2525
/// <summary>
2626
/// The type of action to perform.
@@ -64,7 +64,7 @@ public enum ActionType
6464
/// Gets or sets the destination of the action.
6565
/// </summary>
6666
[XmlElement("destination")]
67-
public string Destination { get; set; }
67+
public string? Destination { get; set; }
6868

6969
/// <summary>
7070
/// Gets or sets the verify flag.
@@ -101,8 +101,13 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
101101
return;
102102
}
103103

104-
string source = GetSource(watchPath, fullPath);
105-
string destination = GetDestination(watchPath, fullPath);
104+
string? source = GetSource(watchPath, fullPath);
105+
string? destination = GetDestination(watchPath, fullPath);
106+
107+
if (string.IsNullOrWhiteSpace(source))
108+
{
109+
return;
110+
}
106111

107112
try
108113
{
@@ -111,23 +116,47 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
111116
case ActionType.Copy:
112117
if (TEFS.File.IsValid(source))
113118
{
119+
if (string.IsNullOrWhiteSpace(destination))
120+
{
121+
Logger.WriteLine($"The file '{source}' could not be copied because the destination file was not specified.");
122+
return;
123+
}
124+
114125
File.Copy(source, destination, Verify);
115126
Logger.WriteLine($"Copied {source} to {destination}.");
116127
}
128+
else
129+
{
130+
Logger.WriteLine($"The file '{source}' could not be copied because the path was not valid, the file doesn't exists, or it was in use.");
131+
}
117132
break;
118133
case ActionType.Move:
119134
if (TEFS.File.IsValid(source))
120135
{
136+
if (string.IsNullOrWhiteSpace(destination))
137+
{
138+
Logger.WriteLine($"The file '{source}' could not be moved because the destination file was not specified.");
139+
return;
140+
}
141+
121142
File.Move(source, destination, Verify);
122143
Logger.WriteLine($"Moved {source} to {destination}.");
123144
}
145+
else
146+
{
147+
Logger.WriteLine($"The file '{source}' could not be moved because the path was not valid, the file doesn't exists, or it was in use.");
148+
}
124149
break;
125150
case ActionType.Delete:
126151
if (TEFS.File.IsValid(source))
127152
{
128153
File.Delete(source);
129154
Logger.WriteLine($"Deleted {source}.");
130155
}
156+
else
157+
{
158+
Logger.WriteLine($"The file '{source}' could not be deleted because the path was not valid, the file doesn't exists, or it was in use.");
159+
}
131160
break;
132161
}
133162
}
@@ -152,7 +181,7 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
152181
/// <returns>
153182
/// The destination string value.
154183
/// </returns>
155-
private string GetDestination(string watchPath, string fullPath)
184+
private string? GetDestination(string watchPath, string fullPath)
156185
{
157186
if (string.IsNullOrWhiteSpace(Destination))
158187
{
@@ -175,7 +204,7 @@ private string GetDestination(string watchPath, string fullPath)
175204
/// <returns>
176205
/// The source string value.
177206
/// </returns>
178-
private string GetSource(string watchPath, string fullPath)
207+
private string? GetSource(string watchPath, string fullPath)
179208
{
180209
if (string.IsNullOrWhiteSpace(Source))
181210
{

FileWatcher/Configuration/Actions/Actions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class Actions
1515
/// Gets or sets the list of actions to perform.
1616
/// </summary>
1717
[XmlElement("action")]
18-
public List<Action> ActionList { get; set; }
18+
public List<Action>? ActionList { get; set; }
1919

2020
/// <summary>
2121
/// Runs all the actions for the watch.

FileWatcher/Configuration/Commands/Command.cs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ namespace TE.FileWatcher.Configuration.Commands
1717
public class Command : RunnableBase
1818
{
1919
// The process that will run the command
20-
private Process _process;
20+
private Process? _process;
2121

2222
// A queue containing the information to start the command process
23-
private ConcurrentQueue<ProcessStartInfo> _processInfo;
23+
private ConcurrentQueue<ProcessStartInfo>? _processInfo;
2424

2525
// Flag indicating that a process is running
2626
private bool _isProcessRunning = false;
@@ -29,13 +29,13 @@ public class Command : RunnableBase
2929
/// Gets or sets the arguments associated with the file to execute.
3030
/// </summary>
3131
[XmlElement("arguments")]
32-
public string Arguments { get; set; }
32+
public string? Arguments { get; set; }
3333

3434
/// <summary>
3535
/// Gets or sets the full path to the file to executed.
3636
/// </summary>
3737
[XmlElement("path")]
38-
public string Path { get; set; }
38+
public string? Path { get; set; }
3939

4040
/// <summary>
4141
/// Gets or sets the triggers of the action.
@@ -72,8 +72,14 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
7272
return;
7373
}
7474

75-
string commandPath = GetCommand(watchPath, fullPath);
76-
string arguments = GetArguments(watchPath, fullPath);
75+
string? commandPath = GetCommand(watchPath, fullPath);
76+
string? arguments = GetArguments(watchPath, fullPath);
77+
78+
if (string.IsNullOrWhiteSpace(commandPath))
79+
{
80+
Logger.WriteLine($"The command was not provided. Command was not run.");
81+
return;
82+
}
7783

7884
if (!File.Exists(commandPath))
7985
{
@@ -88,9 +94,16 @@ public override void Run(string watchPath, string fullPath, TriggerType trigger)
8894
_processInfo = new ConcurrentQueue<ProcessStartInfo>();
8995
}
9096

91-
ProcessStartInfo startInfo = new ProcessStartInfo();
92-
startInfo.FileName = commandPath;
93-
startInfo.Arguments = arguments;
97+
ProcessStartInfo startInfo = new()
98+
{
99+
FileName = commandPath
100+
};
101+
102+
if (arguments != null)
103+
{
104+
startInfo.Arguments = arguments;
105+
}
106+
94107
_processInfo.Enqueue(startInfo);
95108

96109
// Execute the next process in the queue
@@ -119,12 +132,14 @@ private void Execute()
119132

120133
try
121134
{
122-
if (_processInfo.TryDequeue(out ProcessStartInfo startInfo))
135+
if (_processInfo.TryDequeue(out ProcessStartInfo? startInfo))
123136
{
124137
if (File.Exists(startInfo.FileName))
125138
{
126-
_process = new Process();
127-
_process.StartInfo = startInfo;
139+
_process = new Process
140+
{
141+
StartInfo = startInfo
142+
};
128143
_process.StartInfo.CreateNoWindow = true;
129144
_process.StartInfo.UseShellExecute = false;
130145
_process.EnableRaisingEvents = true;
@@ -142,7 +157,14 @@ private void Execute()
142157
}
143158
catch (Exception ex)
144159
{
145-
Logger.WriteLine($"Could not run the command '{_process.StartInfo.FileName} {_process.StartInfo.Arguments}'. Reason: {ex.Message}");
160+
if (_process != null)
161+
{
162+
Logger.WriteLine($"Could not run the command '{_process.StartInfo.FileName} {_process.StartInfo.Arguments}'. Reason: {ex.Message}");
163+
}
164+
else
165+
{
166+
Logger.WriteLine($"Could not run the command. Reason: {ex.Message}");
167+
}
146168
}
147169
}
148170

@@ -159,7 +181,7 @@ private void Execute()
159181
/// <returns>
160182
/// The command path string value.
161183
/// </returns>
162-
private string GetArguments(string watchPath, string fullPath)
184+
private string? GetArguments(string watchPath, string fullPath)
163185
{
164186
if (string.IsNullOrWhiteSpace(Arguments))
165187
{
@@ -182,7 +204,7 @@ private string GetArguments(string watchPath, string fullPath)
182204
/// <returns>
183205
/// The command path string value.
184206
/// </returns>
185-
private string GetCommand(string watchPath, string fullPath)
207+
private string? GetCommand(string watchPath, string fullPath)
186208
{
187209
if (string.IsNullOrWhiteSpace(Path))
188210
{
@@ -201,7 +223,7 @@ private string GetCommand(string watchPath, string fullPath)
201223
/// <param name="args">
202224
/// The event arguments.
203225
/// </param>
204-
private void OnProcessExit(object sender, EventArgs args)
226+
private void OnProcessExit(object? sender, EventArgs args)
205227
{
206228
_isProcessRunning = false;
207229

FileWatcher/Configuration/Commands/Commands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class Commands
1717
/// Gets or sets the list of actions to perform.
1818
/// </summary>
1919
[XmlElement("command")]
20-
public List<Command> CommandList { get; set; }
20+
public List<Command>? CommandList { get; set; }
2121

2222
/// <summary>
2323
/// Runs all the commands for the watch.

FileWatcher/Configuration/Data/Attributes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public HashSet<FileAttributes> Attribute
2727
{
2828
get
2929
{
30-
HashSet<FileAttributes> attributes = new HashSet<FileAttributes>(AttributeStrings.Count);
30+
HashSet<FileAttributes> attributes = new(AttributeStrings.Count);
3131
foreach (string attribute in AttributeStrings)
3232
{
3333
try

0 commit comments

Comments
 (0)