Merge pull request #250220

lib.generators.toGitINI: escape string values in configuration

authored by

Robert Helgesson and committed by
GitHub
54bc9b45 804d0108

+56 -3
+11 -3
lib/generators.nix
··· 189 * } 190 * 191 *> [url "ssh://git@github.com/"] 192 - *> insteadOf = https://github.com/ 193 *> 194 *> [user] 195 - *> name = edolstra 196 */ 197 toGitINI = attrs: 198 with builtins; ··· 209 else 210 ''${section} "${subsection}"''; 211 212 # generation for multiple ini values 213 mkKeyValue = k: v: 214 - let mkKeyValue = mkKeyValueDefault { } " = " k; 215 in concatStringsSep "\n" (map (kv: "\t" + mkKeyValue kv) (lib.toList v)); 216 217 # converts { a.b.c = 5; } to { "a.b".c = 5; } for toINI
··· 189 * } 190 * 191 *> [url "ssh://git@github.com/"] 192 + *> insteadOf = "https://github.com" 193 *> 194 *> [user] 195 + *> name = "edolstra" 196 */ 197 toGitINI = attrs: 198 with builtins; ··· 209 else 210 ''${section} "${subsection}"''; 211 212 + mkValueString = v: 213 + let 214 + escapedV = '' 215 + "${ 216 + replaceStrings [ "\n" " " ''"'' "\\" ] [ "\\n" "\\t" ''\"'' "\\\\" ] v 217 + }"''; 218 + in mkValueStringDefault { } (if isString v then escapedV else v); 219 + 220 # generation for multiple ini values 221 mkKeyValue = k: v: 222 + let mkKeyValue = mkKeyValueDefault { inherit mkValueString; } " = " k; 223 in concatStringsSep "\n" (map (kv: "\t" + mkKeyValue kv) (lib.toList v)); 224 225 # converts { a.b.c = 5; } to { "a.b".c = 5; } for toINI
+45
lib/tests/misc.nix
··· 948 ''; 949 }; 950 951 /* right now only invocation check */ 952 testToJSONSimple = 953 let val = {
··· 948 ''; 949 }; 950 951 + testToGitINI = { 952 + expr = generators.toGitINI { 953 + user = { 954 + email = "user@example.org"; 955 + name = "John Doe"; 956 + signingKey = "00112233445566778899AABBCCDDEEFF"; 957 + }; 958 + gpg.program = "path-to-gpg"; 959 + tag.gpgSign = true; 960 + include.path = "~/path/to/config.inc"; 961 + includeIf."gitdif:~/src/dir".path = "~/path/to/conditional.inc"; 962 + extra = { 963 + boolean = true; 964 + integer = 38; 965 + name = "value"; 966 + subsection.value = "test"; 967 + };}; 968 + expected = '' 969 + [extra] 970 + ${"\t"}boolean = true 971 + ${"\t"}integer = 38 972 + ${"\t"}name = "value" 973 + 974 + [extra "subsection"] 975 + ${"\t"}value = "test" 976 + 977 + [gpg] 978 + ${"\t"}program = "path-to-gpg" 979 + 980 + [include] 981 + ${"\t"}path = "~/path/to/config.inc" 982 + 983 + [includeIf "gitdif:~/src/dir"] 984 + ${"\t"}path = "~/path/to/conditional.inc" 985 + 986 + [tag] 987 + ${"\t"}gpgSign = true 988 + 989 + [user] 990 + ${"\t"}email = "user@example.org" 991 + ${"\t"}name = "John Doe" 992 + ${"\t"}signingKey = "00112233445566778899AABBCCDDEEFF" 993 + ''; 994 + }; 995 + 996 /* right now only invocation check */ 997 testToJSONSimple = 998 let val = {