Refactor pacserver
This commit is contained in:
		| @@ -4,10 +4,15 @@ namespace Pacserver.Tests; | ||||
|  | ||||
| public class TranserFilesTest { | ||||
|     [Fact] | ||||
|     public void TransferPacmanCacheTest() { | ||||
|         string result = PacserverUtils.determinePacmanCacheDirectory(); | ||||
|         PacserverUtils.TransferPacmanCache(); | ||||
|     public void transferPacmanCache_doesNotFail() { | ||||
|         // Arrange | ||||
|         PacserverUtils utils = new PacserverUtils(); | ||||
|  | ||||
|         // Act | ||||
|         utils.readPacmanConfig(); | ||||
|         utils.transferPacmanCache(); | ||||
|  | ||||
|         // Assert | ||||
|         //Assert.NotEmpty(Directory.GetFiles("/home/rene/test/")); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -1,13 +0,0 @@ | ||||
| using Pacserver.Utils; | ||||
|  | ||||
| namespace Pacserver.Tests; | ||||
|  | ||||
| public class pacmanCache_Test { | ||||
|     [Fact] | ||||
|     public void doesPacmanCacheExist() { | ||||
|         string result = PacserverUtils.determinePacmanCacheDirectory(); | ||||
|  | ||||
|         Assert.Equivalent(result, "/var/cache/pacman/pkg/"); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @@ -1,12 +0,0 @@ | ||||
| using Pacserver.Utils; | ||||
|  | ||||
| namespace Pacserver.Tests; | ||||
|  | ||||
| public class pacmanDatabase_Test { | ||||
|     [Fact] | ||||
|     public void doesPacmanDatabaseExist() { | ||||
|         string result = PacserverUtils.determinePacmanDatabaseDirectory(); | ||||
|  | ||||
|         Assert.Equivalent(result, "/var/lib/pacman/"); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										18
									
								
								src/Pacserver.Tests/readPacmanConfigTest.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/Pacserver.Tests/readPacmanConfigTest.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | ||||
| using Pacserver.Utils; | ||||
|  | ||||
| namespace Pacserver.Tests; | ||||
|  | ||||
| public class readPacmanConfigTest { | ||||
|     [Fact] | ||||
|     public void readPacmanConfig_returnsNoException() { | ||||
|         // Arrange | ||||
|         PacserverUtils utils = new PacserverUtils(); | ||||
|  | ||||
|         // Act | ||||
|         var exception = Record.Exception(() => utils.readPacmanConfig()); | ||||
|  | ||||
|         // Assert | ||||
|         Assert.Null(exception); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @@ -3,69 +3,58 @@ using System.Text.RegularExpressions; | ||||
|  | ||||
| namespace Pacserver.Utils; | ||||
| public class PacserverUtils { | ||||
|     public static string pacmanCacheDirectory { get; set; } = string.Empty; | ||||
|     public static string determinePacmanCacheDirectory() { | ||||
|         string defaultPacmanCacheDirectory = "/var/cache/pacman/pkg/"; | ||||
|  | ||||
|         Regex regex = new Regex(@"\/(?:[\w.-]+\/)*[\w.-]+(?:\.\w+)*\/?$"); // https://regex101.com/r/GwWeui/2 | ||||
|         string? line; | ||||
|         StreamReader file = new StreamReader("/etc/pacman.conf"); | ||||
|         while ((line = file.ReadLine()) is not null) { | ||||
|             if (line.Contains("CacheDir")) { | ||||
|                 Match match = regex.Match(line); | ||||
|                 if (match.Success) { | ||||
|                     pacmanCacheDirectory = match.ToString(); | ||||
|                 } else { | ||||
|                     throw new Exception("Could not determine where pacman cache is! Would normally be found here " + defaultPacmanCacheDirectory); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|         file.Close(); | ||||
|  | ||||
|         return pacmanCacheDirectory; | ||||
|     } | ||||
|  | ||||
|     public string pacmanCacheDirectory { get; set; } = string.Empty; | ||||
|     public static string pacmanDatabaseDirectory { get; set; } = string.Empty; | ||||
|     public static string determinePacmanDatabaseDirectory() { | ||||
|         string defaultPacmanDatabaseDirectory = "/var/lib/pacman/"; | ||||
|     public static List<String> pathsToDetermine = new List<String>() { "CacheDir", "DBPath" }; | ||||
|     public void readPacmanConfig() { | ||||
|         using (StreamReader file = new StreamReader("/etc/pacman.conf")) { | ||||
|             Regex regex = new Regex(@"\/(?:[\w.-]+\/)*[\w.-]+(?:\.\w+)*\/?$"); // https://regex101.com/r/GwWeui/2 | ||||
|             string? line; | ||||
|  | ||||
|         Regex regex = new Regex(@"\/(?:[\w.-]+\/)*[\w.-]+(?:\.\w+)*\/?$"); // https://regex101.com/r/GwWeui/2 | ||||
|         string? line; | ||||
|         StreamReader file = new StreamReader("/etc/pacman.conf"); | ||||
|         while ((line = file.ReadLine()) is not null) { | ||||
|             if (line.Contains("DBPath")) { | ||||
|                 Match match = regex.Match(line); | ||||
|                 if (match.Success) { | ||||
|                     pacmanDatabaseDirectory = match.ToString(); | ||||
|                 } else { | ||||
|                     throw new Exception("Could not determine where pacman database is! Would normally be found here " + defaultPacmanDatabaseDirectory); | ||||
|             while ((line = file.ReadLine()) is not null) { | ||||
|                 foreach (string path in pathsToDetermine) { | ||||
|                     if (line.Contains(path)) { | ||||
|                         Match match = regex.Match(line); | ||||
|                         if (match.Success) { | ||||
|                             switch (path) { | ||||
|                                 case "CacheDir": | ||||
|                                     pacmanCacheDirectory = match.ToString(); | ||||
|                                     break; | ||||
|                                 case "DBPath": | ||||
|                                     pacmanDatabaseDirectory = match.ToString(); | ||||
|                                     break; | ||||
|                                 default: | ||||
|                                     throw new Exception("Could not deal with " + match.ToString()); | ||||
|                             } | ||||
|                         } else { | ||||
|                             string pathsToDetermineString = string.Join(",", pathsToDetermine); | ||||
|                             throw new Exception("Could not determine the necessary file paths: " + pathsToDetermineString); | ||||
|                         } | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|         file.Close(); | ||||
|  | ||||
|         return pacmanDatabaseDirectory; | ||||
|     } | ||||
|  | ||||
|     public static void checkForNewerPackagesAndDatabases() { | ||||
|     public void checkForNewerPackagesAndDatabases() { | ||||
|  | ||||
|     } | ||||
|  | ||||
|     private static List<String> NewerPackagesAndDatabases = new List<String>(); | ||||
|     public static async void TransferPacmanCache() { | ||||
|     private static List<String> newerPackagesAndDatabases = new List<String>(); | ||||
|     public async void transferPacmanCache() { | ||||
|         HttpClient client = new HttpClient(); | ||||
|         HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://192.168.0.69:12000/upload?path=/"); | ||||
|         MultipartFormDataContent content = new MultipartFormDataContent(); | ||||
|  | ||||
|         foreach (String PkgOrDb in NewerPackagesAndDatabases) { | ||||
|             content.Add(new ByteArrayContent(File.ReadAllBytes(pacmanCacheDirectory + PkgOrDb)), "path", Path.GetFileName(pacmanCacheDirectory + PkgOrDb)); | ||||
|         foreach (string pkgOrDb in newerPackagesAndDatabases) { | ||||
|             content.Add(new ByteArrayContent(File.ReadAllBytes(pacmanCacheDirectory + pkgOrDb)), "path", Path.GetFileName(pacmanCacheDirectory + pkgOrDb)); | ||||
|         } | ||||
|         request.Content = content; | ||||
|  | ||||
|         await client.SendAsync(request); | ||||
|     } | ||||
|  | ||||
|     public static void transferPacmanDatabases() { | ||||
|     public void transferPacmanDatabases() { | ||||
|  | ||||
|     } | ||||
| } | ||||
| @@ -12,7 +12,9 @@ public class Program { | ||||
|  | ||||
|         switch (args[0]) { | ||||
|             case "determinePacmanCacheDirectory": | ||||
|                 Console.WriteLine(PacserverUtils.determinePacmanCacheDirectory()); | ||||
|                 PacserverUtils utils = new PacserverUtils(); | ||||
|                 utils.readPacmanConfig(); | ||||
|                 Console.WriteLine(utils.pacmanCacheDirectory); | ||||
|                 break; | ||||
|             default: | ||||
|                 Console.ForegroundColor = ConsoleColor.Red; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user