Merge branch 'feature/determinePacmanDatabaseDirectory' into dev
This commit is contained in:
12
src/Pacserver.Tests/pacmanDatabase_Test.cs
Normal file
12
src/Pacserver.Tests/pacmanDatabase_Test.cs
Normal 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/");
|
||||||
|
}
|
||||||
|
}
|
@ -26,6 +26,23 @@ public class PacserverUtils {
|
|||||||
|
|
||||||
public static string pacmanDatabaseDirectory { get; set; } = string.Empty;
|
public static string pacmanDatabaseDirectory { get; set; } = string.Empty;
|
||||||
public static string determinePacmanDatabaseDirectory() {
|
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;
|
return pacmanDatabaseDirectory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user