Merge branch 'feature/determinePacmanDatabaseDirectory' into dev

This commit is contained in:
ProfessionalUwU 2023-06-06 01:29:18 +02:00
commit a61c3fbfd9
Signed by: ProfessionalUwU
GPG Key ID: 013AD77C0A9DD3F2
2 changed files with 29 additions and 0 deletions

View File

@ -0,0 +1,12 @@
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

@ -26,6 +26,23 @@ public class PacserverUtils {
public static string pacmanDatabaseDirectory { get; set; } = string.Empty;
public static string determinePacmanDatabaseDirectory() {
string defaultPacmanDatabaseDirectory = "/var/lib/pacman/";
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);
}
}
}
file.Close();
return pacmanDatabaseDirectory;
}