sshcnfgr 0.1.0b1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sshcnfgr/__init__.py +5 -0
- sshcnfgr/cli.py +182 -0
- sshcnfgr/dedup.py +363 -0
- sshcnfgr/dedup_screen.py +466 -0
- sshcnfgr/generator.py +83 -0
- sshcnfgr/gui/__init__.py +29 -0
- sshcnfgr/gui/dedup_dialog.py +462 -0
- sshcnfgr/gui/main_window.py +1225 -0
- sshcnfgr/identities.py +95 -0
- sshcnfgr/importer.py +152 -0
- sshcnfgr/logging_conf.py +45 -0
- sshcnfgr/model.py +231 -0
- sshcnfgr/paths.py +37 -0
- sshcnfgr/settings.py +87 -0
- sshcnfgr/store.py +222 -0
- sshcnfgr/tui.py +471 -0
- sshcnfgr/tui.tcss +105 -0
- sshcnfgr-0.1.0b1.dist-info/METADATA +205 -0
- sshcnfgr-0.1.0b1.dist-info/RECORD +22 -0
- sshcnfgr-0.1.0b1.dist-info/WHEEL +4 -0
- sshcnfgr-0.1.0b1.dist-info/entry_points.txt +2 -0
- sshcnfgr-0.1.0b1.dist-info/licenses/LICENSE +21 -0
sshcnfgr/dedup_screen.py
ADDED
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import copy
|
|
4
|
+
|
|
5
|
+
from textual.app import ComposeResult
|
|
6
|
+
from textual.containers import Horizontal, Vertical, VerticalScroll
|
|
7
|
+
from textual.screen import Screen
|
|
8
|
+
from textual.widgets import (
|
|
9
|
+
Button,
|
|
10
|
+
Checkbox,
|
|
11
|
+
DataTable,
|
|
12
|
+
Footer,
|
|
13
|
+
Header,
|
|
14
|
+
Input,
|
|
15
|
+
Label,
|
|
16
|
+
Select,
|
|
17
|
+
Static,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
from .dedup import (
|
|
21
|
+
DuplicateGroup,
|
|
22
|
+
FieldConflict,
|
|
23
|
+
MergeDisposition,
|
|
24
|
+
MergePlan,
|
|
25
|
+
analyze_group_conflicts,
|
|
26
|
+
apply_merge,
|
|
27
|
+
build_merged_host,
|
|
28
|
+
default_conflict_choices,
|
|
29
|
+
find_duplicate_groups,
|
|
30
|
+
)
|
|
31
|
+
from .model import HostDatabase, HostEntry
|
|
32
|
+
from .store import save_database
|
|
33
|
+
|
|
34
|
+
EMPTY_OPTION_VALUE = "__SSHCNFGR_EMPTY__"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class DuplicateReviewScreen(Screen[HostDatabase | None]):
|
|
38
|
+
"""Dedizierte TUI-Ansicht zum Finden und interaktiven Zusammenfuehren von Host-Dubletten."""
|
|
39
|
+
|
|
40
|
+
BINDINGS = [
|
|
41
|
+
("escape", "close", "Zurueck"),
|
|
42
|
+
("ctrl+m", "merge_current", "Zusammenfuehren"),
|
|
43
|
+
("n", "next_group", "Naechste"),
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
def __init__(self, db: HostDatabase) -> None:
|
|
47
|
+
super().__init__()
|
|
48
|
+
self._db: HostDatabase = copy.deepcopy(db)
|
|
49
|
+
self._changed = False
|
|
50
|
+
self._include_inactive = False
|
|
51
|
+
self._groups: list[DuplicateGroup] = []
|
|
52
|
+
self._selected_index: int | None = None
|
|
53
|
+
self._current_conflicts: list[FieldConflict] = []
|
|
54
|
+
self._conflict_selects: dict[str, Select[str]] = {}
|
|
55
|
+
self._loading_group = False
|
|
56
|
+
|
|
57
|
+
def compose(self) -> ComposeResult:
|
|
58
|
+
yield Header(show_clock=True)
|
|
59
|
+
with Horizontal(id="dedup_toolbar"):
|
|
60
|
+
yield Label(
|
|
61
|
+
"Potentielle Dubletten (gleicher HostName + Port + User)",
|
|
62
|
+
id="dedup_title",
|
|
63
|
+
)
|
|
64
|
+
yield Checkbox(
|
|
65
|
+
"Inaktive einschliessen",
|
|
66
|
+
value=False,
|
|
67
|
+
id="dedup_include_inactive",
|
|
68
|
+
)
|
|
69
|
+
with Horizontal(id="dedup_body"):
|
|
70
|
+
with Vertical(id="dedup_left"):
|
|
71
|
+
yield Label("Dubletten-Gruppen", id="dedup_groups_label")
|
|
72
|
+
yield DataTable(id="dedup_groups_table", cursor_type="row")
|
|
73
|
+
with Horizontal(id="dedup_left_actions"):
|
|
74
|
+
yield Button("Zurueck (Esc)", id="btn_dedup_close")
|
|
75
|
+
yield Button("Naechste", id="btn_dedup_next")
|
|
76
|
+
with VerticalScroll(id="dedup_right"):
|
|
77
|
+
yield Label("Kandidaten in ausgewaehlter Gruppe", id="dedup_members_label")
|
|
78
|
+
yield DataTable(id="dedup_members_table", cursor_type="row")
|
|
79
|
+
|
|
80
|
+
yield Label("Basis-Eintrag behalten:")
|
|
81
|
+
yield Select(
|
|
82
|
+
[],
|
|
83
|
+
prompt="Keine Gruppe ausgewaehlt",
|
|
84
|
+
id="dedup_primary_select",
|
|
85
|
+
allow_blank=True,
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
yield Label("Ziel-Alias:")
|
|
89
|
+
yield Input(
|
|
90
|
+
placeholder="Alias fuer zusammengefuehrten Host",
|
|
91
|
+
id="dedup_target_name",
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
yield Label("Abweichende Felder (Konflikte):", id="dedup_conflicts_label")
|
|
95
|
+
yield Vertical(id="dedup_conflicts_container")
|
|
96
|
+
|
|
97
|
+
yield Label("Uebrige Dubletten behandeln als:")
|
|
98
|
+
yield Select(
|
|
99
|
+
[
|
|
100
|
+
("Deaktivieren (enabled=false, empfohlen)", "disable"),
|
|
101
|
+
("Endgueltig loeschen", "delete"),
|
|
102
|
+
],
|
|
103
|
+
value="disable",
|
|
104
|
+
allow_blank=False,
|
|
105
|
+
id="dedup_disposition",
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
yield Label("Vorschau:")
|
|
109
|
+
yield Static("", id="dedup_preview")
|
|
110
|
+
|
|
111
|
+
with Horizontal(id="dedup_merge_actions"):
|
|
112
|
+
yield Button(
|
|
113
|
+
"Gruppe zusammenfuehren",
|
|
114
|
+
id="btn_dedup_merge",
|
|
115
|
+
variant="success",
|
|
116
|
+
)
|
|
117
|
+
yield Button("Ueberspringen", id="btn_dedup_skip")
|
|
118
|
+
|
|
119
|
+
yield Static("", id="dedup_status")
|
|
120
|
+
yield Footer()
|
|
121
|
+
|
|
122
|
+
async def on_mount(self) -> None:
|
|
123
|
+
groups_table = self.query_one("#dedup_groups_table", DataTable)
|
|
124
|
+
groups_table.add_columns("Ziel", "Anzahl", "Aliase", "Konflikte")
|
|
125
|
+
|
|
126
|
+
members_table = self.query_one("#dedup_members_table", DataTable)
|
|
127
|
+
members_table.add_columns(
|
|
128
|
+
"Alias",
|
|
129
|
+
"HostName",
|
|
130
|
+
"User",
|
|
131
|
+
"Port",
|
|
132
|
+
"Identity",
|
|
133
|
+
"Groups",
|
|
134
|
+
"Extra",
|
|
135
|
+
"Aktiv",
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
await self._refresh_groups()
|
|
139
|
+
|
|
140
|
+
def _set_status(self, message: str) -> None:
|
|
141
|
+
self.query_one("#dedup_status", Static).update(message)
|
|
142
|
+
|
|
143
|
+
def _current_group(self) -> DuplicateGroup | None:
|
|
144
|
+
if self._selected_index is None:
|
|
145
|
+
return None
|
|
146
|
+
if not (0 <= self._selected_index < len(self._groups)):
|
|
147
|
+
return None
|
|
148
|
+
return self._groups[self._selected_index]
|
|
149
|
+
|
|
150
|
+
async def _refresh_groups(self, preferred_index: int = 0) -> None:
|
|
151
|
+
self._groups = find_duplicate_groups(
|
|
152
|
+
self._db,
|
|
153
|
+
include_inactive=self._include_inactive,
|
|
154
|
+
)
|
|
155
|
+
self.query_one("#dedup_groups_label", Label).update(
|
|
156
|
+
f"Dubletten-Gruppen ({len(self._groups)})"
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
groups_table = self.query_one("#dedup_groups_table", DataTable)
|
|
160
|
+
groups_table.clear(columns=False)
|
|
161
|
+
|
|
162
|
+
for idx, group in enumerate(self._groups):
|
|
163
|
+
conflicts = analyze_group_conflicts(group)
|
|
164
|
+
aliases_text = ", ".join(group.aliases)
|
|
165
|
+
groups_table.add_row(
|
|
166
|
+
group.label,
|
|
167
|
+
str(len(group.hosts)),
|
|
168
|
+
aliases_text,
|
|
169
|
+
str(len(conflicts)),
|
|
170
|
+
key=str(idx),
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
if not self._groups:
|
|
174
|
+
self._selected_index = None
|
|
175
|
+
await self._load_group_details(None)
|
|
176
|
+
self._set_status("Keine potentiellen Dubletten gefunden.")
|
|
177
|
+
return
|
|
178
|
+
|
|
179
|
+
target_index = min(max(preferred_index, 0), len(self._groups) - 1)
|
|
180
|
+
self._selected_index = target_index
|
|
181
|
+
groups_table.move_cursor(row=target_index)
|
|
182
|
+
await self._load_group_details(self._groups[target_index])
|
|
183
|
+
|
|
184
|
+
async def _load_group_details(self, group: DuplicateGroup | None) -> None:
|
|
185
|
+
self._loading_group = True
|
|
186
|
+
try:
|
|
187
|
+
members_table = self.query_one("#dedup_members_table", DataTable)
|
|
188
|
+
members_table.clear(columns=False)
|
|
189
|
+
|
|
190
|
+
primary_select = self.query_one("#dedup_primary_select", Select)
|
|
191
|
+
target_name_input = self.query_one("#dedup_target_name", Input)
|
|
192
|
+
conflicts_container = self.query_one("#dedup_conflicts_container", Vertical)
|
|
193
|
+
merge_button = self.query_one("#btn_dedup_merge", Button)
|
|
194
|
+
|
|
195
|
+
await conflicts_container.remove_children()
|
|
196
|
+
self._conflict_selects.clear()
|
|
197
|
+
self._current_conflicts = []
|
|
198
|
+
|
|
199
|
+
if group is None:
|
|
200
|
+
self.query_one("#dedup_members_label", Label).update(
|
|
201
|
+
"Kandidaten in ausgewaehlter Gruppe"
|
|
202
|
+
)
|
|
203
|
+
primary_select.set_options([])
|
|
204
|
+
target_name_input.value = ""
|
|
205
|
+
merge_button.disabled = True
|
|
206
|
+
await conflicts_container.mount(
|
|
207
|
+
Static("Keine Dubletten-Gruppe ausgewaehlt.", classes="dedup-hint")
|
|
208
|
+
)
|
|
209
|
+
self.query_one("#dedup_preview", Static).update("-")
|
|
210
|
+
return
|
|
211
|
+
|
|
212
|
+
merge_button.disabled = False
|
|
213
|
+
self.query_one("#dedup_members_label", Label).update(
|
|
214
|
+
f"Kandidaten fuer {group.label} ({len(group.hosts)} Eintraege)"
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
for host in group.hosts:
|
|
218
|
+
identity_display = (
|
|
219
|
+
host.identity_file
|
|
220
|
+
or host.identity
|
|
221
|
+
or self._db.resolve_identity_file(host)
|
|
222
|
+
or ""
|
|
223
|
+
)
|
|
224
|
+
extra_summary = ", ".join(
|
|
225
|
+
f"{k}={v}"
|
|
226
|
+
for k, v in sorted(host.extra_options.items(), key=lambda item: item[0].lower())
|
|
227
|
+
)
|
|
228
|
+
members_table.add_row(
|
|
229
|
+
host.name,
|
|
230
|
+
host.host_name,
|
|
231
|
+
host.user or f"({self._db.resolve_user(host) or '-'})",
|
|
232
|
+
str(host.port) if host.port is not None else f"({self._db.resolve_port(host)})",
|
|
233
|
+
identity_display,
|
|
234
|
+
",".join(host.groups),
|
|
235
|
+
extra_summary,
|
|
236
|
+
"yes" if host.enabled else "no",
|
|
237
|
+
key=host.id,
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
primary_options = [
|
|
241
|
+
(f"{host.name} ({'aktiv' if host.enabled else 'inaktiv'})", host.id)
|
|
242
|
+
for host in group.hosts
|
|
243
|
+
]
|
|
244
|
+
default_primary = group.hosts[0]
|
|
245
|
+
primary_select.set_options(primary_options)
|
|
246
|
+
primary_select.value = default_primary.id
|
|
247
|
+
target_name_input.value = default_primary.name
|
|
248
|
+
|
|
249
|
+
conflicts = analyze_group_conflicts(group)
|
|
250
|
+
self._current_conflicts = conflicts
|
|
251
|
+
defaults = default_conflict_choices(group, default_primary.id, conflicts)
|
|
252
|
+
|
|
253
|
+
if not conflicts:
|
|
254
|
+
await conflicts_container.mount(
|
|
255
|
+
Static(
|
|
256
|
+
"Keine Feld-Konflikte: fehlende Felder, Gruppen, Tags und "
|
|
257
|
+
"Extra-Optionen werden automatisch vereint.",
|
|
258
|
+
classes="dedup-hint",
|
|
259
|
+
)
|
|
260
|
+
)
|
|
261
|
+
else:
|
|
262
|
+
for conflict in conflicts:
|
|
263
|
+
options: list[tuple[str, str]] = [
|
|
264
|
+
(opt.display_label, opt.value)
|
|
265
|
+
for opt in conflict.options
|
|
266
|
+
if opt.value is not None
|
|
267
|
+
]
|
|
268
|
+
if conflict.allow_empty:
|
|
269
|
+
options.append(("(leer)", EMPTY_OPTION_VALUE))
|
|
270
|
+
|
|
271
|
+
default_value = defaults.get(conflict.field)
|
|
272
|
+
initial = default_value if default_value is not None else EMPTY_OPTION_VALUE
|
|
273
|
+
|
|
274
|
+
select_widget: Select[str] = Select(
|
|
275
|
+
options,
|
|
276
|
+
value=initial,
|
|
277
|
+
allow_blank=False,
|
|
278
|
+
)
|
|
279
|
+
self._conflict_selects[conflict.field] = select_widget
|
|
280
|
+
await conflicts_container.mount(Label(f"{conflict.label}:"), select_widget)
|
|
281
|
+
finally:
|
|
282
|
+
self._loading_group = False
|
|
283
|
+
|
|
284
|
+
self._update_preview()
|
|
285
|
+
|
|
286
|
+
def _collect_resolved_fields(self) -> dict[str, str | None]:
|
|
287
|
+
resolved: dict[str, str | None] = {}
|
|
288
|
+
for field_name, select_widget in self._conflict_selects.items():
|
|
289
|
+
if select_widget.is_blank() or select_widget.value == EMPTY_OPTION_VALUE:
|
|
290
|
+
resolved[field_name] = None
|
|
291
|
+
else:
|
|
292
|
+
resolved[field_name] = str(select_widget.value)
|
|
293
|
+
return resolved
|
|
294
|
+
|
|
295
|
+
def _current_primary_id(self) -> str | None:
|
|
296
|
+
primary_select = self.query_one("#dedup_primary_select", Select)
|
|
297
|
+
if primary_select.is_blank():
|
|
298
|
+
return None
|
|
299
|
+
return str(primary_select.value)
|
|
300
|
+
|
|
301
|
+
def _current_disposition(self) -> MergeDisposition:
|
|
302
|
+
disposition_select = self.query_one("#dedup_disposition", Select)
|
|
303
|
+
if disposition_select.value == "delete":
|
|
304
|
+
return "delete"
|
|
305
|
+
return "disable"
|
|
306
|
+
|
|
307
|
+
def _format_host_preview(self, merged: HostEntry, group: DuplicateGroup) -> str:
|
|
308
|
+
disposition = self._current_disposition()
|
|
309
|
+
others = [h.name for h in group.hosts if h.id != merged.id]
|
|
310
|
+
action_text = "werden deaktiviert" if disposition == "disable" else "werden geloescht"
|
|
311
|
+
|
|
312
|
+
eff_user = merged.user or self._db.resolve_user(merged) or "-"
|
|
313
|
+
eff_port = merged.port if merged.port is not None else self._db.resolve_port(merged)
|
|
314
|
+
groups_str = ", ".join(merged.groups) if merged.groups else "-"
|
|
315
|
+
tags_str = ", ".join(merged.tags) if merged.tags else "-"
|
|
316
|
+
extra_str = (
|
|
317
|
+
", ".join(f"{k}={v}" for k, v in sorted(merged.extra_options.items()))
|
|
318
|
+
if merged.extra_options
|
|
319
|
+
else "-"
|
|
320
|
+
)
|
|
321
|
+
|
|
322
|
+
lines = [
|
|
323
|
+
f"Host: {merged.name or '<leer>'} -> {eff_user}@{merged.host_name}:{eff_port}",
|
|
324
|
+
f"Identity: {merged.identity or '-'} | IdentityFile: {merged.identity_file or '-'}",
|
|
325
|
+
f"Groups: {groups_str} | Tags: {tags_str}",
|
|
326
|
+
f"Extra: {extra_str}",
|
|
327
|
+
f"Uebrige ({action_text}): {', '.join(others)}",
|
|
328
|
+
]
|
|
329
|
+
return "\n".join(lines)
|
|
330
|
+
|
|
331
|
+
def _update_preview(self) -> None:
|
|
332
|
+
preview = self.query_one("#dedup_preview", Static)
|
|
333
|
+
group = self._current_group()
|
|
334
|
+
primary_id = self._current_primary_id()
|
|
335
|
+
if group is None or primary_id is None:
|
|
336
|
+
preview.update("-")
|
|
337
|
+
return
|
|
338
|
+
|
|
339
|
+
target_name = self.query_one("#dedup_target_name", Input).value.strip()
|
|
340
|
+
resolved = self._collect_resolved_fields()
|
|
341
|
+
|
|
342
|
+
plan = MergePlan(
|
|
343
|
+
group=group,
|
|
344
|
+
primary_host_id=primary_id,
|
|
345
|
+
target_name=target_name,
|
|
346
|
+
resolved_fields=resolved,
|
|
347
|
+
disposition=self._current_disposition(),
|
|
348
|
+
)
|
|
349
|
+
merged = build_merged_host(
|
|
350
|
+
group=group,
|
|
351
|
+
primary_host_id=primary_id,
|
|
352
|
+
target_name=target_name,
|
|
353
|
+
resolved_fields=resolved,
|
|
354
|
+
)
|
|
355
|
+
_, _, errors = apply_merge(self._db, plan)
|
|
356
|
+
|
|
357
|
+
text = self._format_host_preview(merged, group)
|
|
358
|
+
if errors:
|
|
359
|
+
text += f"\nHinweis: {'; '.join(errors)}"
|
|
360
|
+
preview.update(text)
|
|
361
|
+
|
|
362
|
+
async def on_data_table_row_selected(self, event: DataTable.RowSelected) -> None:
|
|
363
|
+
if event.data_table.id != "dedup_groups_table":
|
|
364
|
+
return
|
|
365
|
+
|
|
366
|
+
raw_key = str(getattr(event.row_key, "value", event.row_key))
|
|
367
|
+
try:
|
|
368
|
+
idx = int(raw_key)
|
|
369
|
+
except ValueError:
|
|
370
|
+
return
|
|
371
|
+
|
|
372
|
+
if 0 <= idx < len(self._groups):
|
|
373
|
+
self._selected_index = idx
|
|
374
|
+
await self._load_group_details(self._groups[idx])
|
|
375
|
+
self._set_status(f"Gruppe ausgewaehlt: {self._groups[idx].label}")
|
|
376
|
+
|
|
377
|
+
async def on_checkbox_changed(self, event: Checkbox.Changed) -> None:
|
|
378
|
+
if event.checkbox.id != "dedup_include_inactive":
|
|
379
|
+
return
|
|
380
|
+
self._include_inactive = event.value
|
|
381
|
+
await self._refresh_groups(preferred_index=0)
|
|
382
|
+
|
|
383
|
+
def on_select_changed(self, event: Select.Changed) -> None:
|
|
384
|
+
if self._loading_group:
|
|
385
|
+
return
|
|
386
|
+
|
|
387
|
+
group = self._current_group()
|
|
388
|
+
if event.select.id == "dedup_primary_select" and group is not None:
|
|
389
|
+
primary_id = self._current_primary_id()
|
|
390
|
+
if primary_id:
|
|
391
|
+
primary = group.get_host_by_id(primary_id)
|
|
392
|
+
if primary is not None:
|
|
393
|
+
self.query_one("#dedup_target_name", Input).value = primary.name
|
|
394
|
+
|
|
395
|
+
self._update_preview()
|
|
396
|
+
|
|
397
|
+
def on_input_changed(self, event: Input.Changed) -> None:
|
|
398
|
+
if self._loading_group:
|
|
399
|
+
return
|
|
400
|
+
if event.input.id == "dedup_target_name":
|
|
401
|
+
self._update_preview()
|
|
402
|
+
|
|
403
|
+
async def on_button_pressed(self, event: Button.Pressed) -> None:
|
|
404
|
+
button_id = event.button.id
|
|
405
|
+
if button_id == "btn_dedup_close":
|
|
406
|
+
self.action_close()
|
|
407
|
+
elif button_id in ("btn_dedup_next", "btn_dedup_skip"):
|
|
408
|
+
await self.action_next_group()
|
|
409
|
+
elif button_id == "btn_dedup_merge":
|
|
410
|
+
await self.action_merge_current()
|
|
411
|
+
|
|
412
|
+
def action_close(self) -> None:
|
|
413
|
+
self.dismiss(self._db if self._changed else None)
|
|
414
|
+
|
|
415
|
+
async def action_next_group(self) -> None:
|
|
416
|
+
if not self._groups:
|
|
417
|
+
self._set_status("Keine weiteren Dubletten-Gruppen vorhanden.")
|
|
418
|
+
return
|
|
419
|
+
|
|
420
|
+
current = self._selected_index if self._selected_index is not None else -1
|
|
421
|
+
next_idx = (current + 1) % len(self._groups)
|
|
422
|
+
self._selected_index = next_idx
|
|
423
|
+
self.query_one("#dedup_groups_table", DataTable).move_cursor(row=next_idx)
|
|
424
|
+
group = self._groups[next_idx]
|
|
425
|
+
await self._load_group_details(group)
|
|
426
|
+
self._set_status(f"Gruppe {next_idx + 1}/{len(self._groups)}: {group.label}")
|
|
427
|
+
|
|
428
|
+
async def action_merge_current(self) -> None:
|
|
429
|
+
group = self._current_group()
|
|
430
|
+
primary_id = self._current_primary_id()
|
|
431
|
+
if group is None or primary_id is None:
|
|
432
|
+
self._set_status("Keine Dubletten-Gruppe ausgewaehlt.")
|
|
433
|
+
return
|
|
434
|
+
|
|
435
|
+
target_name = self.query_one("#dedup_target_name", Input).value.strip()
|
|
436
|
+
disposition = self._current_disposition()
|
|
437
|
+
plan = MergePlan(
|
|
438
|
+
group=group,
|
|
439
|
+
primary_host_id=primary_id,
|
|
440
|
+
target_name=target_name,
|
|
441
|
+
resolved_fields=self._collect_resolved_fields(),
|
|
442
|
+
disposition=disposition,
|
|
443
|
+
)
|
|
444
|
+
|
|
445
|
+
candidate_db, merged, errors = apply_merge(self._db, plan)
|
|
446
|
+
if errors:
|
|
447
|
+
self._set_status(f"Fehler: {'; '.join(errors)}")
|
|
448
|
+
return
|
|
449
|
+
|
|
450
|
+
try:
|
|
451
|
+
save_database(candidate_db)
|
|
452
|
+
except OSError as exc:
|
|
453
|
+
self._set_status(f"Speicherfehler: {exc}")
|
|
454
|
+
return
|
|
455
|
+
|
|
456
|
+
self._db = candidate_db
|
|
457
|
+
self._changed = True
|
|
458
|
+
|
|
459
|
+
duplicate_count = len(group.hosts) - 1
|
|
460
|
+
verb = "deaktiviert" if disposition == "disable" else "geloescht"
|
|
461
|
+
current_idx = self._selected_index or 0
|
|
462
|
+
await self._refresh_groups(preferred_index=current_idx)
|
|
463
|
+
self._set_status(
|
|
464
|
+
f"Zusammengefuehrt: {merged.name} ({duplicate_count} Dublette(n) {verb}, "
|
|
465
|
+
f"Backup erstellt)"
|
|
466
|
+
)
|
sshcnfgr/generator.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from .model import HostDatabase, HostEntry, get_option_ci
|
|
7
|
+
from .paths import GENERATED_CONFIG_PATH, SSH_DIR, ensure_private_dir, set_private_permissions
|
|
8
|
+
from .store import create_backup
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def render_host_block(host: HostEntry, db: HostDatabase) -> str:
|
|
12
|
+
lines: list[str] = [f"Host {host.name}"]
|
|
13
|
+
lines.append(f" HostName {host.host_name}")
|
|
14
|
+
|
|
15
|
+
if host.user:
|
|
16
|
+
lines.append(f" User {host.user}")
|
|
17
|
+
if host.port is not None:
|
|
18
|
+
lines.append(f" Port {host.port}")
|
|
19
|
+
resolved_identity_file = db.resolve_identity_file(host)
|
|
20
|
+
global_identity_file = get_option_ci(db.global_options, "IdentityFile")
|
|
21
|
+
# Der globale IdentityFile-Default wird schon im Host-*-Block ausgegeben
|
|
22
|
+
# und muss nicht in jeden Host-Block wiederholt werden.
|
|
23
|
+
if resolved_identity_file and resolved_identity_file != global_identity_file:
|
|
24
|
+
lines.append(f" IdentityFile {resolved_identity_file}")
|
|
25
|
+
|
|
26
|
+
for key, value in sorted(host.extra_options.items(), key=lambda item: item[0].lower()):
|
|
27
|
+
lines.append(f" {key} {value}")
|
|
28
|
+
|
|
29
|
+
return "\n".join(lines)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def generate_config(db: HostDatabase, *, include_timestamp: bool = True) -> str:
|
|
33
|
+
header = ["# Generated by sshcnfgr"]
|
|
34
|
+
if include_timestamp:
|
|
35
|
+
header.append(f"# Timestamp: {datetime.now().isoformat(timespec='seconds')}")
|
|
36
|
+
header.append("")
|
|
37
|
+
|
|
38
|
+
# Globale Include-Direktiven gehoeren vor den ersten Host-Block, nicht in Host *.
|
|
39
|
+
include_lines: list[str] = []
|
|
40
|
+
wildcard_options: list[tuple[str, str]] = []
|
|
41
|
+
for key, value in sorted(db.global_options.items(), key=lambda item: item[0].lower()):
|
|
42
|
+
if key.lower() == "include":
|
|
43
|
+
include_lines.append(f"{key} {value}")
|
|
44
|
+
else:
|
|
45
|
+
wildcard_options.append((key, value))
|
|
46
|
+
|
|
47
|
+
blocks: list[str] = []
|
|
48
|
+
if include_lines:
|
|
49
|
+
blocks.append("\n".join(include_lines))
|
|
50
|
+
|
|
51
|
+
blocks.extend(render_host_block(host, db) for host in db.sorted_hosts() if host.enabled)
|
|
52
|
+
|
|
53
|
+
# OpenSSH verwendet pro Parameter den ersten gefundenen Wert, daher gehoeren
|
|
54
|
+
# globale Defaults (Host *) ans Dateiende, sonst schlagen sie Host-Werte.
|
|
55
|
+
if wildcard_options:
|
|
56
|
+
global_lines = ["Host *"]
|
|
57
|
+
for key, value in wildcard_options:
|
|
58
|
+
global_lines.append(f" {key} {value}")
|
|
59
|
+
blocks.append("\n".join(global_lines))
|
|
60
|
+
|
|
61
|
+
return "\n\n".join(["\n".join(header).rstrip(), *blocks]).strip() + "\n"
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def write_generated_config(
|
|
65
|
+
db: HostDatabase,
|
|
66
|
+
output: Path = GENERATED_CONFIG_PATH,
|
|
67
|
+
*,
|
|
68
|
+
backup: bool = True,
|
|
69
|
+
) -> Path:
|
|
70
|
+
if output.parent == SSH_DIR:
|
|
71
|
+
ensure_private_dir(output.parent)
|
|
72
|
+
else:
|
|
73
|
+
output.parent.mkdir(parents=True, exist_ok=True)
|
|
74
|
+
if backup:
|
|
75
|
+
create_backup(output)
|
|
76
|
+
|
|
77
|
+
content = generate_config(db)
|
|
78
|
+
|
|
79
|
+
tmp_path = output.with_name(output.name + ".tmp")
|
|
80
|
+
tmp_path.write_text(content, encoding="utf-8")
|
|
81
|
+
set_private_permissions(tmp_path)
|
|
82
|
+
tmp_path.replace(output)
|
|
83
|
+
return output
|
sshcnfgr/gui/__init__.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""Optionale Qt6 Desktop-GUI (PySide6) fuer SSHCnfgr (Wayland und X11)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def run_gui() -> None:
|
|
9
|
+
"""Startet die Qt6-GUI.
|
|
10
|
+
|
|
11
|
+
Qt6 waehlt unter Linux automatisch das passende Plattform-Plugin
|
|
12
|
+
(Wayland oder X11/xcb).
|
|
13
|
+
"""
|
|
14
|
+
from PySide6.QtWidgets import QApplication
|
|
15
|
+
|
|
16
|
+
from .main_window import SSHCnfgrWindow
|
|
17
|
+
|
|
18
|
+
app = QApplication.instance()
|
|
19
|
+
created_app = app is None
|
|
20
|
+
if app is None:
|
|
21
|
+
app = QApplication(sys.argv)
|
|
22
|
+
app.setApplicationName("SSHCnfgr")
|
|
23
|
+
app.setOrganizationName("SSHCnfgr")
|
|
24
|
+
|
|
25
|
+
window = SSHCnfgrWindow()
|
|
26
|
+
window.show()
|
|
27
|
+
|
|
28
|
+
if created_app:
|
|
29
|
+
sys.exit(app.exec())
|