toGitINI
lib.generators.toGitINI
Docs pulled from | This Revision | 20 minutes ago
Generate a git-config file from an attrset.
It has two major differences from the regular INI format:
- values are indented with tabs
- sections can have sub-sections
Further: https://git-scm.com/docs/git-config#EXAMPLES
Examples
lib.generators.toGitINI
usage example
generators.toGitINI {
url."ssh://[email protected]/".insteadOf = "https://github.com";
user.name = "edolstra";
}
> [url "ssh://[email protected]/"]
> insteadOf = "https://github.com"
>
> [user]
> name = "edolstra"
Inputs
attrs
-
Key-value pairs to be converted to a git-config file. See: https://git-scm.com/docs/git-config#_variables for possible values.
Noogle detected
Implementation
The following is the current implementation of this function.
toGitINI = attrs:
let
mkSectionName = name:
let
containsQuote = hasInfix ''"'' name;
sections = splitString "." name;
section = head sections;
subsections = tail sections;
subsection = concatStringsSep "." subsections;
in if containsQuote || subsections == [ ] then
name
else
''${section} "${subsection}"'';
mkValueString = v:
let
escapedV = ''
"${
replaceStrings [ "\n" " " ''"'' "\\" ] [ "\\n" "\\t" ''\"'' "\\\\" ] v
}"'';
in mkValueStringDefault { } (if isString v then escapedV else v);
# generation for multiple ini values
mkKeyValue = k: v:
let mkKeyValue = mkKeyValueDefault { inherit mkValueString; } " = " k;
in concatStringsSep "\n" (map (kv: "\t" + mkKeyValue kv) (toList v));
# converts { a.b.c = 5; } to { "a.b".c = 5; } for toINI
gitFlattenAttrs = let
recurse = path: value:
if isAttrs value && !isDerivation value then
mapAttrsToList (name: value: recurse ([ name ] ++ path) value) value
else if length path > 1 then {
${concatStringsSep "." (reverseList (tail path))}.${head path} = value;
} else {
${head path} = value;
};
in attrs: foldl recursiveUpdate { } (flatten (recurse [ ] attrs));
toINI_ = toINI { inherit mkKeyValue mkSectionName; };
in
toINI_ (gitFlattenAttrs attrs);