-
Notifications
You must be signed in to change notification settings - Fork 20
/
parse-git-config.nix
63 lines (62 loc) · 1.69 KB
/
parse-git-config.nix
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
63
# Basic git INI-like file format parser
#
# Probably not feature complete anytime soon...
#
# Notable omissions:
# - multiline values (if supported??)
# - proper subsections
# - includes
# - conditional includes
# - keys with embedded whitespace
#
# Low hanging fruit:
# - group by section if you need to query the file often
#
# Unknowns:
# - whitespace before section header?
# - what if no section is specified before first item?
#
{ lib ? import <nixpkgs/lib>, ... }:
let
inherit (lib.strings) splitString hasPrefix removePrefix removeSuffix replaceStrings;
inherit (lib.lists) foldl' head tail;
parseIniText = text:
let
rawLines = splitString "\n" text;
folded = foldl' step zero rawLines;
zero = { section = "";
items = [];
};
step = r@{ section, items }: line:
if hasPrefix "[" line
then r // {
section = removePrefix "[" (removeSuffix "]" line);
}
else if builtins.match ".*=.*" line != null then
let
s = splitString "=" line;
s0 = head s;
key = replaceStrings [" " "\t"] ["" ""] s0;
v = removePrefix "${s0}=" line;
value = lstrip v;
in
r // {
items = items ++ [{ inherit section key value; }];
}
else
r
;
in
folded.items
;
lstrip = s: if hasPrefix " " s then lstrip (removePrefix " " s)
else if hasPrefix "\t" s then lstrip (removePrefix "\t" s)
else s;
parseIniFile = p:
builtins.addErrorContext ("while parsing INI file " + toString p) (
parseIniText (builtins.readFile p)
)
;
in {
inherit parseIniText parseIniFile;
}