refactor #1

Manually merged
ProfessionalUwU merged 3 commits from refactor into dev 2023-06-15 20:25:30 +02:00
8 changed files with 68 additions and 79 deletions

View File

@ -10,7 +10,7 @@ indent_style = space
tab_width = 4 tab_width = 4
# Naming Conventions # Naming Conventions
dotnet_naming_style.pascal_case_style.capitalization = pascal_case dotnet_naming_style.camel_case.capitalization = camel_case
# New line preferences # New line preferences
csharp_new_line_before_open_brace = none csharp_new_line_before_open_brace = none

View File

@ -17,15 +17,15 @@ run:
@dotnet run @dotnet run
build: build:
@dotnet build src/Pacserver/pacserver.csproj @dotnet build src/{{uppercase_project_name}}/{{project_name}}.csproj
@dotnet build src/Pacserver.Tests/Pacserver.Tests.csproj @dotnet build src/{{uppercase_project_name}}.Tests/{{uppercase_project_name}}.Tests.csproj
publish: format publish: format
@dotnet publish --configuration Release src/Pacserver/pacserver.csproj @dotnet publish --configuration Release src/{{uppercase_project_name}}/{{project_name}}.csproj
format: format:
@dotnet format src/Pacserver @dotnet format src/{{uppercase_project_name}}
@dotnet format src/Pacserver.Tests @dotnet format src/{{uppercase_project_name}}.Tests
test: build test: build
@dotnet test src/Pacserver.Tests @dotnet test src/{{uppercase_project_name}}.Tests

View File

@ -4,10 +4,15 @@ namespace Pacserver.Tests;
public class TranserFilesTest { public class TranserFilesTest {
[Fact] [Fact]
public void TransferPacmanCacheTest() { public void transferPacmanCache_doesNotFail() {
string result = PacserverUtils.determinePacmanCacheDirectory(); // Arrange
PacserverUtils.TransferPacmanCache(); PacserverUtils utils = new PacserverUtils();
// Act
utils.readPacmanConfig();
utils.transferPacmanCache();
// Assert
//Assert.NotEmpty(Directory.GetFiles("/home/rene/test/")); //Assert.NotEmpty(Directory.GetFiles("/home/rene/test/"));
} }
} }

View File

@ -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/");
}
}

View File

@ -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/");
}
}

View 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);
}
}

View File

@ -3,69 +3,58 @@ using System.Text.RegularExpressions;
namespace Pacserver.Utils; namespace Pacserver.Utils;
public class PacserverUtils { public class PacserverUtils {
public static string pacmanCacheDirectory { get; set; } = string.Empty; public 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 static string pacmanDatabaseDirectory { get; set; } = string.Empty; public static string pacmanDatabaseDirectory { get; set; } = string.Empty;
public static string determinePacmanDatabaseDirectory() { public static List<String> pathsToDetermine = new List<String>() { "CacheDir", "DBPath" };
string defaultPacmanDatabaseDirectory = "/var/lib/pacman/"; 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 while ((line = file.ReadLine()) is not null) {
string? line; foreach (string path in pathsToDetermine) {
StreamReader file = new StreamReader("/etc/pacman.conf"); if (line.Contains(path)) {
while ((line = file.ReadLine()) is not null) { Match match = regex.Match(line);
if (line.Contains("DBPath")) { if (match.Success) {
Match match = regex.Match(line); switch (path) {
if (match.Success) { case "CacheDir":
pacmanDatabaseDirectory = match.ToString(); pacmanCacheDirectory = match.ToString();
} else { break;
throw new Exception("Could not determine where pacman database is! Would normally be found here " + defaultPacmanDatabaseDirectory); 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>(); private static List<String> newerPackagesAndDatabases = new List<String>();
public static async void TransferPacmanCache() { public async void transferPacmanCache() {
HttpClient client = new HttpClient(); HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://192.168.0.69:12000/upload?path=/"); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://192.168.0.69:12000/upload?path=/");
MultipartFormDataContent content = new MultipartFormDataContent(); MultipartFormDataContent content = new MultipartFormDataContent();
foreach (String PkgOrDb in NewerPackagesAndDatabases) { foreach (string pkgOrDb in newerPackagesAndDatabases) {
content.Add(new ByteArrayContent(File.ReadAllBytes(pacmanCacheDirectory + PkgOrDb)), "path", Path.GetFileName(pacmanCacheDirectory + PkgOrDb)); content.Add(new ByteArrayContent(File.ReadAllBytes(pacmanCacheDirectory + pkgOrDb)), "path", Path.GetFileName(pacmanCacheDirectory + pkgOrDb));
} }
request.Content = content; request.Content = content;
await client.SendAsync(request); await client.SendAsync(request);
} }
public static void transferPacmanDatabases() { public void transferPacmanDatabases() {
} }
} }

View File

@ -12,7 +12,9 @@ public class Program {
switch (args[0]) { switch (args[0]) {
case "determinePacmanCacheDirectory": case "determinePacmanCacheDirectory":
Console.WriteLine(PacserverUtils.determinePacmanCacheDirectory()); PacserverUtils utils = new PacserverUtils();
utils.readPacmanConfig();
Console.WriteLine(utils.pacmanCacheDirectory);
break; break;
default: default:
Console.ForegroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Red;