Implement determinePacmanCacheDirectory

Used a StreamReader to read pacman.conf and then read one line until a line contains CacheDir
Then used a regex to only get the cache directory
This commit is contained in:
ProfessionalUwU 2023-06-03 12:11:32 +02:00
parent 20599a03ba
commit 04048c4cb9
Signed by: ProfessionalUwU
GPG Key ID: 013AD77C0A9DD3F2
2 changed files with 19 additions and 1 deletions

View File

@ -6,7 +6,8 @@
Console.ResetColor(); Console.ResetColor();
} else { } else {
switch (args[0]) { switch (args[0]) {
case "": case "determinePacmanCacheDirectory":
Pacserver.determinePacmanCacheDirectory();
break; break;
default: default:
break; break;

View File

@ -1,6 +1,23 @@
using System.Text.RegularExpressions;
public class Pacserver { public class Pacserver {
public static string pacmanCacheDirectory { get; set; } = string.Empty; public static string pacmanCacheDirectory { get; set; } = string.Empty;
public static string determinePacmanCacheDirectory() { 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();
}
}
}
file.Close();
return pacmanCacheDirectory; return pacmanCacheDirectory;
} }