Browse Source

validation refactor

Veloe 1 year ago
parent
commit
1799ce1a0e
2 changed files with 18 additions and 18 deletions
  1. 16 16
      StringReplacer/Program.cs
  2. 2 2
      StringReplacer/StringReplacer.csproj

+ 16 - 16
StringReplacer/Program.cs

@@ -48,9 +48,8 @@ public static class Program
                             if (!string.IsNullOrEmpty(path))
                                 _logger.Warning("Duplicating argument {0} {1}", args[argIndex], args[argIndex + 1]);
                             path = args[++argIndex];
-
-                            if (!Directory.Exists(path))
-                                throw new DirectoryNotFoundException(path);
+                            
+                            ValidatePath(path,true);
 
                             _logger.Information($"Path: {path}");
                             argIndex++;
@@ -60,8 +59,7 @@ public static class Program
                                 _logger.Warning("Duplicating argument {0} {1}", args[argIndex], args[argIndex + 1]);
                             word_Find = args[++argIndex];
 
-                            if (word_Find.StartsWith('-') || string.IsNullOrEmpty(word_Find))
-                                throw new ArgumentException(word_Find);
+                            ValidateArg(word_Find, true, nameof(word_Find));
 
                             _logger.Information($"Find: {word_Find}");
                             argIndex++;
@@ -71,8 +69,7 @@ public static class Program
                                 _logger.Warning("Duplicating argument {0} {1}", args[argIndex], args[argIndex + 1]);
                             word_Replace = args[++argIndex];
 
-                            if (word_Replace.StartsWith('-') || string.IsNullOrEmpty(word_Replace))
-                                throw new ArgumentException(word_Replace);
+                            ValidateArg(word_Find, true, nameof(word_Replace));
 
                             _logger.Information($"Replace to: {word_Replace}");
                             argIndex++;
@@ -82,8 +79,7 @@ public static class Program
                                 _logger.Warning("Duplicating argument {0} {1}", args[argIndex], args[argIndex + 1]);
                             files_exts = args[++argIndex];
 
-                            if (files_exts.StartsWith('-') || string.IsNullOrEmpty(files_exts))
-                                throw new ArgumentException(files_exts);
+                            ValidateArg(files_exts, true, nameof(files_exts));
 
                             _logger.Information($"Files extentions: {files_exts}");
                             argIndex++;
@@ -101,9 +97,9 @@ public static class Program
                 do
                 {
                     Console.Write("Path:");
-                    path = Console.ReadLine().Trim() ?? string.Empty;
+                    path = Console.ReadLine()?.Trim() ?? string.Empty;
                 }
-                while (string.IsNullOrEmpty(path) || !Directory.Exists(path));
+                while (!ValidatePath(path));
                 _logger.Information($"Path: {path}");
             }
 
@@ -112,9 +108,9 @@ public static class Program
                 do
                 {
                     Console.Write("Find:");
-                    word_Find = Console.ReadLine().Trim() ?? string.Empty;
+                    word_Find = Console.ReadLine()?.Trim() ?? string.Empty;
                 }
-                while (string.IsNullOrEmpty(word_Find));
+                while (!ValidateArg(word_Find));
                 _logger.Information($"Find: {word_Find}");
             }
 
@@ -123,9 +119,9 @@ public static class Program
                 do
                 {
                     Console.Write("Replace:");
-                    word_Replace = Console.ReadLine().Trim() ?? string.Empty;
+                    word_Replace = Console.ReadLine()?.Trim() ?? string.Empty;
                 }
-                while (string.IsNullOrEmpty(word_Replace));
+                while (!ValidateArg(word_Replace));
                 _logger.Information($"Replace to: {word_Replace}");
             }
 
@@ -186,13 +182,17 @@ public static class Program
             {
                 _logger.Fatal(ex.Message);
                 if (!string.IsNullOrEmpty(ex.StackTrace))
-                    _logger.Error(ex.StackTrace);
+                    _logger.Fatal(ex.StackTrace);
 
                 innerException = innerException.InnerException;
             }
         }
     }
 
+    private static bool ValidateArg(string arg, bool throwException = false, string? argName = null) => !arg.StartsWith('-') && !string.IsNullOrEmpty(arg) || (throwException ? throw new ArgumentException(string.Format("Argument is not vaild {0}",arg),argName ?? nameof(arg)) : false);
+
+    private static bool ValidatePath(string path, bool throwException = false) => !string.IsNullOrEmpty(path) && Directory.Exists(path) || (throwException ? throw new DirectoryNotFoundException(string.Format("Path is not valid {0}", path)) : false);
+
     private static void ProcessEncode_OutputDataReceived(object sender, DataReceivedEventArgs e)
     {
         var filepath = e.Data;

+ 2 - 2
StringReplacer/StringReplacer.csproj

@@ -5,8 +5,8 @@
     <TargetFramework>net7.0</TargetFramework>
     <ImplicitUsings>enable</ImplicitUsings>
     <Nullable>enable</Nullable>
-    <AssemblyVersion>1.0.0.50</AssemblyVersion>
-    <FileVersion>1.0.0.50</FileVersion>
+    <AssemblyVersion>1.0.0.59</AssemblyVersion>
+    <FileVersion>1.0.0.59</FileVersion>
   </PropertyGroup>
 
   <ItemGroup>