-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathLinuxConfigParser.cs
More file actions
62 lines (48 loc) · 1.8 KB
/
LinuxConfigParser.cs
File metadata and controls
62 lines (48 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace GitCredentialManager.Interop.Linux;
public class LinuxConfigParser
{
#if NETFRAMEWORK
private const string SQ = "'";
private const string DQ = "\"";
private const string Hash = "#";
#else
private const char SQ = '\'';
private const char DQ = '"';
private const char Hash = '#';
#endif
private static readonly Regex LineRegex = new(@"^\s*(?<key>[a-zA-Z0-9\.-]+)\s*=\s*(?<value>.+?)\s*(?:#.*)?$");
private readonly ITrace _trace;
public LinuxConfigParser(ITrace trace)
{
EnsureArgument.NotNull(trace, nameof(trace));
_trace = trace;
}
public IDictionary<string, string> Parse(string content)
{
var result = new Dictionary<string, string>(GitConfigurationKeyComparer.Instance);
IEnumerable<string> lines = content.Split(['\n'], StringSplitOptions.RemoveEmptyEntries);
foreach (string line in lines)
{
// Ignore empty lines or full-line comments
var trimmedLine = line.Trim();
if (string.IsNullOrEmpty(trimmedLine) || trimmedLine.StartsWith(Hash))
continue;
var match = LineRegex.Match(trimmedLine);
if (!match.Success)
{
_trace.WriteLine($"Invalid config line format: {line}");
continue;
}
string key = match.Groups["key"].Value;
string value = match.Groups["value"].Value;
// Remove enclosing quotes from the value, if any
if ((value.StartsWith(DQ) && value.EndsWith(DQ)) || (value.StartsWith(SQ) && value.EndsWith(SQ)))
value = value.Substring(1, value.Length - 2);
result[key] = value;
}
return result;
}
}