Debian Codegraph @

ansible_collections/community/general/plugins/modules/git_config_info.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2023, Guenther Grill <[email protected]>
4 #
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 from __future__ import annotations
9
10 DOCUMENTATION = r"""
11 module: git_config_info
12 author:
13 - Guenther Grill (@guenhter)
14 version_added: 8.1.0
15 requirements: ['git']
16 short_description: Read git configuration
17 description:
18 - The M(community.general.git_config_info) module reads the git configuration by invoking C(git config).
19 extends_documentation_fragment:
20 - community.general._attributes
21 - community.general._attributes.info_module
22 options:
23 name:
24 description:
25 - The name of the setting to read.
26 - If not provided, all settings are returned as RV(config_values).
27 type: str
28 path:
29 description:
30 - Path to a git repository or file for reading values from a specific repo.
31 - If O(scope) is V(local), this must point to a repository to read from.
32 - If O(scope) is V(file), this must point to specific git config file to read from.
33 - Otherwise O(path) is ignored if set.
34 type: path
35 scope:
36 description:
37 - Specify which scope to read values from.
38 - If set to V(global), the global git config is used. O(path) is ignored.
39 - If set to V(system), the system git config is used. O(path) is ignored.
40 - If set to V(local), O(path) must be set to the repo to read from.
41 - If set to V(file), O(path) must be set to the config file to read from.
42 choices: ["global", "system", "local", "file"]
43 default: "system"
44 type: str
45 """
46
47 EXAMPLES = r"""
48 - name: Read a system wide config
49 community.general.git_config_info:
50 name: core.editor
51 register: result
52
53 - name: Show value of core.editor
54 ansible.builtin.debug:
55 msg: "{{ result.config_value | default('(not set)', true) }}"
56
57 - name: Read a global config from ~/.gitconfig
58 community.general.git_config_info:
59 name: alias.remotev
60 scope: global
61
62 - name: Read a project specific config
63 community.general.git_config_info:
64 name: color.ui
65 scope: local
66 path: /etc
67
68 - name: Read all global values
69 community.general.git_config_info:
70 scope: global
71
72 - name: Read all system wide values
73 community.general.git_config_info:
74
75 - name: Read all values of a specific file
76 community.general.git_config_info:
77 scope: file
78 path: /etc/gitconfig
79 """
80
81 RETURN = r"""
82 config_value:
83 description: >-
84 When O(name) is set, a string containing the value of the setting in name. If O(name) is not set, empty. If a config key
85 such as V(push.pushoption) has more then one entry, just the first one is returned here.
86 returned: success if O(name) is set
87 type: str
88 sample: "vim"
89
90 config_values:
91 description:
92 - This is a dictionary mapping a git configuration setting to a list of its values.
93 - When O(name) is not set, all configuration settings are returned here.
94 - When O(name) is set, only the setting specified in O(name) is returned here. If that setting is not set, the key is
95 still present, and its value is an empty list.
96 returned: success
97 type: dict
98 sample:
99 core.editor: ["vim"]
100 color.ui: ["auto"]
101 push.pushoption: ["merge_request.create", "merge_request.draft"]
102 alias.remotev: ["remote -v"]
103 """
104
105 from ansible.module_utils.basic import AnsibleModule
106
107
108 def main():
109 module = AnsibleModule(
110 argument_spec=dict(
111 name=dict(type="str"),
112 path=dict(type="path"),
113 scope=dict(type="str", default="system", choices=["global", "system", "local", "file"]),
114 ),
115 required_if=[
116 ("scope", "local", ["path"]),
117 ("scope", "file", ["path"]),
118 ],
119 required_one_of=[],
120 supports_check_mode=True,
121 )
122
123 # We check error message for a pattern, so we need to make sure the messages appear in the form we're expecting.
124 # Set the locale to C to ensure consistent messages.
125 module.run_command_environ_update = dict(LANGUAGE="C", LC_ALL="C")
126
127 name = module.params["name"]
128 path = module.params["path"]
129 scope = module.params["scope"]
130
131 run_cwd = path if scope == "local" else "/"
132 args = build_args(module, name, path, scope)
133
134 (rc, out, err) = module.run_command(args, cwd=run_cwd, expand_user_and_vars=False)
135
136 if rc == 128 and "unable to read config file" in err:
137 # This just means nothing has been set at the given scope
138 pass
139 elif rc >= 2:
140 # If the return code is 1, it just means the option hasn't been set yet, which is fine.
141 module.fail_json(rc=rc, msg=err, cmd=" ".join(args))
142
143 output_lines = out.strip("\0").split("\0") if out else []
144
145 if name:
146 first_value = output_lines[0] if output_lines else ""
147 config_values = {name: output_lines}
148 module.exit_json(changed=False, msg="", config_value=first_value, config_values=config_values)
149 else:
150 config_values = text_to_dict(output_lines)
151 module.exit_json(changed=False, msg="", config_value="", config_values=config_values)
152
153
154 def build_args(module, name, path, scope):
155 git_path = module.get_bin_path("git", True)
156 args = [git_path, "config", "--includes", "--null", f"--{scope}"]
157
158 if scope == "file":
159 args.append(path)
160
161 if name:
162 args.extend(["--get-all", name])
163 else:
164 args.append("--list")
165
166 return args
167
168
169 def text_to_dict(text_lines):
170 config_values = {}
171 for value in text_lines:
172 k, v = value.split("\n", 1)
173 if k in config_values:
174 config_values[k].append(v)
175 else:
176 config_values[k] = [v]
177 return config_values
178
179
180 if __name__ == "__main__":
181 main()