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
# Naming Conventions
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
dotnet_naming_style.camel_case.capitalization = camel_case
# New line preferences
csharp_new_line_before_open_brace = none

View File

@ -17,15 +17,15 @@ run:
@dotnet run
build:
@dotnet build src/Pacserver/pacserver.csproj
@dotnet build src/Pacserver.Tests/Pacserver.Tests.csproj
@dotnet build src/{{uppercase_project_name}}/{{project_name}}.csproj
@dotnet build src/{{uppercase_project_name}}.Tests/{{uppercase_project_name}}.Tests.csproj
publish: format
@dotnet publish --configuration Release src/Pacserver/pacserver.csproj
@dotnet publish --configuration Release src/{{uppercase_project_name}}/{{project_name}}.csproj
format:
@dotnet format src/Pacserver
@dotnet format src/Pacserver.Tests
@dotnet format src/{{uppercase_project_name}}
@dotnet format src/{{uppercase_project_name}}.Tests
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 {
[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/"));
}
}

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;
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;
StreamReader file = new StreamReader("/etc/pacman.conf");
while ((line = file.ReadLine()) is not null) {
if (line.Contains("DBPath")) {
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 {
throw new Exception("Could not determine where pacman database is! Would normally be found here " + defaultPacmanDatabaseDirectory);
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() {
}
}

View File

@ -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;