gitacc-switcher 1.5.0__tar.gz → 1.7.0__tar.gz
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.
- {gitacc_switcher-1.5.0/gitacc_switcher.egg-info → gitacc_switcher-1.7.0}/PKG-INFO +26 -1
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/README.md +25 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/gitacc_switcher/__init__.py +1 -1
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/gitacc_switcher/account_manager.py +279 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/gitacc_switcher/cli.py +151 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/gitacc_switcher/ssh_manager.py +183 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0/gitacc_switcher.egg-info}/PKG-INFO +26 -1
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/pyproject.toml +1 -1
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/setup.py +1 -1
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/tests/test_account_manager.py +362 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/tests/test_cli.py +160 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/tests/test_ssh_manager.py +127 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/LICENSE +0 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/MANIFEST.in +0 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/gitacc_switcher/completion.py +0 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/gitacc_switcher/config_manager.py +0 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/gitacc_switcher/hook_manager.py +0 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/gitacc_switcher/rules_manager.py +0 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/gitacc_switcher/shell_hook.py +0 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/gitacc_switcher/utils.py +0 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/gitacc_switcher.egg-info/SOURCES.txt +0 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/gitacc_switcher.egg-info/dependency_links.txt +0 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/gitacc_switcher.egg-info/entry_points.txt +0 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/gitacc_switcher.egg-info/requires.txt +0 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/gitacc_switcher.egg-info/top_level.txt +0 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/requirements.txt +0 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/setup.cfg +0 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/tests/__init__.py +0 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/tests/test_completion.py +0 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/tests/test_config_manager.py +0 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/tests/test_hook_manager.py +0 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/tests/test_rules_manager.py +0 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/tests/test_shell_hook.py +0 -0
- {gitacc_switcher-1.5.0 → gitacc_switcher-1.7.0}/tests/test_utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: gitacc-switcher
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.7.0
|
|
4
4
|
Summary: Git Account Switcher - Manage multiple Git SSH accounts easily
|
|
5
5
|
Home-page: https://github.com/ktechhub/gitacc_switcher
|
|
6
6
|
Author: ktechhub
|
|
@@ -50,6 +50,7 @@ A Python CLI tool to easily switch between multiple Git SSH accounts. Manage mul
|
|
|
50
50
|
- Colored terminal output with active account indicator
|
|
51
51
|
- Pre-commit hook to prevent commits with the wrong account
|
|
52
52
|
- Opt-in directory-based auto-switching (`gitacc rule`) with a bash/zsh shell hook
|
|
53
|
+
- Per-account SSH config `Host` aliases (`gitacc alias`) for `git@<alias>:org/repo` clone URLs
|
|
53
54
|
- Shell autocomplete for bash and zsh
|
|
54
55
|
- Cross-platform support (Linux / macOS)
|
|
55
56
|
|
|
@@ -219,6 +220,29 @@ The hook is silent unless it actually needs to switch accounts: no matching rule
|
|
|
219
220
|
that isn't running, or an identity that's already correct all do nothing. This is entirely
|
|
220
221
|
opt-in — `gitacc rule add` alone never touches your shell config.
|
|
221
222
|
|
|
223
|
+
### `gitacc alias add <account> <host-alias> [--hostname HOSTNAME] [--user USER]` / `gitacc alias remove <host-alias>` / `gitacc alias list`
|
|
224
|
+
|
|
225
|
+
Bind an SSH config `Host` alias to an account's key by writing a clearly delimited, safely
|
|
226
|
+
add/updatable/removable block to `~/.ssh/config`. This lets you clone/push via a per-account host
|
|
227
|
+
alias (e.g. `git@github-work:org/repo`) without hand-editing your SSH config, and without the
|
|
228
|
+
account's key needing to be loaded into the SSH agent at all.
|
|
229
|
+
|
|
230
|
+
```bash
|
|
231
|
+
gitacc alias add work github-work # git@github-work:org/repo uses "work"'s key
|
|
232
|
+
gitacc alias add personal github-personal --hostname github.com
|
|
233
|
+
gitacc alias list
|
|
234
|
+
gitacc alias remove github-work
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
`--hostname` defaults to `github.com` and `--user` defaults to `git`. Running `alias add` again
|
|
238
|
+
for the same alias updates it in place; everything else already in `~/.ssh/config` (including
|
|
239
|
+
aliases for other accounts) is left untouched.
|
|
240
|
+
|
|
241
|
+
```bash
|
|
242
|
+
# clone using the "work" account's key, without ever switching the active identity
|
|
243
|
+
git clone git@github-work:myorg/myrepo.git
|
|
244
|
+
```
|
|
245
|
+
|
|
222
246
|
### `gitacc autocomplete install`
|
|
223
247
|
|
|
224
248
|
Install tab-completion for your shell (bash or zsh). After running, restart your shell or `source` your profile.
|
|
@@ -250,6 +274,7 @@ gitacc --version
|
|
|
250
274
|
| `~/.gitaccrules` | Directory → account rules for `gitacc rule` (opt-in auto-switch) |
|
|
251
275
|
| `~/.ssh/id_<type>_<account>` | Private SSH key per account |
|
|
252
276
|
| `~/.ssh/id_<type>_<account>.pub` | Public SSH key per account |
|
|
277
|
+
| `~/.ssh/config` | Holds gitacc-managed `Host` alias blocks (`gitacc alias`), clearly delimited from any other entries |
|
|
253
278
|
|
|
254
279
|
`~/.gitacc` format:
|
|
255
280
|
|
|
@@ -17,6 +17,7 @@ A Python CLI tool to easily switch between multiple Git SSH accounts. Manage mul
|
|
|
17
17
|
- Colored terminal output with active account indicator
|
|
18
18
|
- Pre-commit hook to prevent commits with the wrong account
|
|
19
19
|
- Opt-in directory-based auto-switching (`gitacc rule`) with a bash/zsh shell hook
|
|
20
|
+
- Per-account SSH config `Host` aliases (`gitacc alias`) for `git@<alias>:org/repo` clone URLs
|
|
20
21
|
- Shell autocomplete for bash and zsh
|
|
21
22
|
- Cross-platform support (Linux / macOS)
|
|
22
23
|
|
|
@@ -186,6 +187,29 @@ The hook is silent unless it actually needs to switch accounts: no matching rule
|
|
|
186
187
|
that isn't running, or an identity that's already correct all do nothing. This is entirely
|
|
187
188
|
opt-in — `gitacc rule add` alone never touches your shell config.
|
|
188
189
|
|
|
190
|
+
### `gitacc alias add <account> <host-alias> [--hostname HOSTNAME] [--user USER]` / `gitacc alias remove <host-alias>` / `gitacc alias list`
|
|
191
|
+
|
|
192
|
+
Bind an SSH config `Host` alias to an account's key by writing a clearly delimited, safely
|
|
193
|
+
add/updatable/removable block to `~/.ssh/config`. This lets you clone/push via a per-account host
|
|
194
|
+
alias (e.g. `git@github-work:org/repo`) without hand-editing your SSH config, and without the
|
|
195
|
+
account's key needing to be loaded into the SSH agent at all.
|
|
196
|
+
|
|
197
|
+
```bash
|
|
198
|
+
gitacc alias add work github-work # git@github-work:org/repo uses "work"'s key
|
|
199
|
+
gitacc alias add personal github-personal --hostname github.com
|
|
200
|
+
gitacc alias list
|
|
201
|
+
gitacc alias remove github-work
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
`--hostname` defaults to `github.com` and `--user` defaults to `git`. Running `alias add` again
|
|
205
|
+
for the same alias updates it in place; everything else already in `~/.ssh/config` (including
|
|
206
|
+
aliases for other accounts) is left untouched.
|
|
207
|
+
|
|
208
|
+
```bash
|
|
209
|
+
# clone using the "work" account's key, without ever switching the active identity
|
|
210
|
+
git clone git@github-work:myorg/myrepo.git
|
|
211
|
+
```
|
|
212
|
+
|
|
189
213
|
### `gitacc autocomplete install`
|
|
190
214
|
|
|
191
215
|
Install tab-completion for your shell (bash or zsh). After running, restart your shell or `source` your profile.
|
|
@@ -217,6 +241,7 @@ gitacc --version
|
|
|
217
241
|
| `~/.gitaccrules` | Directory → account rules for `gitacc rule` (opt-in auto-switch) |
|
|
218
242
|
| `~/.ssh/id_<type>_<account>` | Private SSH key per account |
|
|
219
243
|
| `~/.ssh/id_<type>_<account>.pub` | Public SSH key per account |
|
|
244
|
+
| `~/.ssh/config` | Holds gitacc-managed `Host` alias blocks (`gitacc alias`), clearly delimited from any other entries |
|
|
220
245
|
|
|
221
246
|
`~/.gitacc` format:
|
|
222
247
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""Account management operations."""
|
|
2
2
|
|
|
3
3
|
import getpass
|
|
4
|
+
import json
|
|
4
5
|
import subprocess
|
|
5
6
|
from pathlib import Path
|
|
6
7
|
from typing import Optional, Dict, List
|
|
@@ -384,6 +385,72 @@ class AccountManager:
|
|
|
384
385
|
|
|
385
386
|
return True
|
|
386
387
|
|
|
388
|
+
def add_host_alias(
|
|
389
|
+
self,
|
|
390
|
+
account_name: str,
|
|
391
|
+
host_alias: str,
|
|
392
|
+
hostname: str = "github.com",
|
|
393
|
+
ssh_user: str = "git",
|
|
394
|
+
) -> bool:
|
|
395
|
+
"""Bind an SSH config `Host` alias to an account's key.
|
|
396
|
+
|
|
397
|
+
Args:
|
|
398
|
+
account_name: Account whose key the alias should use
|
|
399
|
+
host_alias: The `Host` alias to create/update (e.g. "github-work")
|
|
400
|
+
hostname: Real host to connect to (default: "github.com")
|
|
401
|
+
ssh_user: SSH user for the connection (default: "git")
|
|
402
|
+
|
|
403
|
+
Returns:
|
|
404
|
+
True if successful, False otherwise
|
|
405
|
+
"""
|
|
406
|
+
account_info = self.config_manager.get_account(account_name)
|
|
407
|
+
if not account_info:
|
|
408
|
+
echo_color("r", f'Account "{account_name}" not found!')
|
|
409
|
+
return False
|
|
410
|
+
|
|
411
|
+
private_key = account_info.get("private_key")
|
|
412
|
+
if not private_key:
|
|
413
|
+
echo_color("r", f'Account "{account_name}" has no private key on record!')
|
|
414
|
+
return False
|
|
415
|
+
|
|
416
|
+
success = self.ssh_manager.add_host_alias(
|
|
417
|
+
host_alias, hostname, private_key, ssh_user
|
|
418
|
+
)
|
|
419
|
+
if success:
|
|
420
|
+
echo_color(
|
|
421
|
+
"g",
|
|
422
|
+
f'Host alias "{host_alias}" → account "{account_name}" '
|
|
423
|
+
f"({ssh_user}@{hostname})",
|
|
424
|
+
)
|
|
425
|
+
else:
|
|
426
|
+
echo_color("r", f'Failed to write host alias "{host_alias}"!')
|
|
427
|
+
return success
|
|
428
|
+
|
|
429
|
+
def remove_host_alias(self, host_alias: str) -> bool:
|
|
430
|
+
"""Remove a managed SSH config host alias.
|
|
431
|
+
|
|
432
|
+
Args:
|
|
433
|
+
host_alias: The `Host` alias to remove
|
|
434
|
+
|
|
435
|
+
Returns:
|
|
436
|
+
True if a block was found and removed, False otherwise
|
|
437
|
+
"""
|
|
438
|
+
success = self.ssh_manager.remove_host_alias(host_alias)
|
|
439
|
+
if success:
|
|
440
|
+
echo_color("g", f'Host alias "{host_alias}" removed.')
|
|
441
|
+
else:
|
|
442
|
+
echo_color("y", f'No managed host alias "{host_alias}" was found.')
|
|
443
|
+
return success
|
|
444
|
+
|
|
445
|
+
def list_host_aliases(self) -> Dict[str, Dict[str, str]]:
|
|
446
|
+
"""List all gitacc-managed SSH config host aliases.
|
|
447
|
+
|
|
448
|
+
Returns:
|
|
449
|
+
Dictionary mapping host alias to its hostname, ssh user, and
|
|
450
|
+
identity file path.
|
|
451
|
+
"""
|
|
452
|
+
return self.ssh_manager.list_host_aliases()
|
|
453
|
+
|
|
387
454
|
def run_doctor(self) -> Dict[str, object]:
|
|
388
455
|
"""Run read-only health checks on the SSH agent and managed key files.
|
|
389
456
|
|
|
@@ -563,3 +630,215 @@ class AccountManager:
|
|
|
563
630
|
echo_color("y", f"Please switch to the correct account:")
|
|
564
631
|
echo_color("y", f" gitacc switch {expected_account}")
|
|
565
632
|
return False
|
|
633
|
+
|
|
634
|
+
def export_accounts(self, output_path: Path, include_keys: bool = False) -> bool:
|
|
635
|
+
"""Export registered accounts' metadata to a portable JSON file.
|
|
636
|
+
|
|
637
|
+
Metadata-only by default (name, email, sign preference, key *paths*,
|
|
638
|
+
and any host aliases bound to each account's key) — no key material
|
|
639
|
+
is read or written unless `include_keys` is explicitly set.
|
|
640
|
+
|
|
641
|
+
Args:
|
|
642
|
+
output_path: File to write the export to
|
|
643
|
+
include_keys: If True, also embed each account's private/public
|
|
644
|
+
SSH key file contents in the export. The resulting file will
|
|
645
|
+
contain private key material in plain text; it is written
|
|
646
|
+
with 0600 permissions, but the caller is responsible for
|
|
647
|
+
keeping it secure afterwards.
|
|
648
|
+
|
|
649
|
+
Returns:
|
|
650
|
+
True if successful, False otherwise
|
|
651
|
+
"""
|
|
652
|
+
accounts = self.config_manager.read_accounts()
|
|
653
|
+
if not accounts:
|
|
654
|
+
echo_color("y", "No accounts registered yet. Nothing to export.")
|
|
655
|
+
return False
|
|
656
|
+
|
|
657
|
+
host_aliases = self.ssh_manager.list_host_aliases()
|
|
658
|
+
|
|
659
|
+
exported_accounts = {}
|
|
660
|
+
for account_name, info in accounts.items():
|
|
661
|
+
private_key = info.get("private_key", "")
|
|
662
|
+
public_key = info.get("public_key", "")
|
|
663
|
+
|
|
664
|
+
entry = {
|
|
665
|
+
"name": info.get("name", account_name),
|
|
666
|
+
"email": info.get("email", ""),
|
|
667
|
+
"sign": info.get("sign", "false"),
|
|
668
|
+
"private_key": private_key,
|
|
669
|
+
"public_key": public_key,
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
matching_aliases = {
|
|
673
|
+
alias: {"hostname": alias_info["hostname"], "user": alias_info["user"]}
|
|
674
|
+
for alias, alias_info in host_aliases.items()
|
|
675
|
+
if private_key and alias_info.get("identity_file") == private_key
|
|
676
|
+
}
|
|
677
|
+
if matching_aliases:
|
|
678
|
+
entry["host_aliases"] = matching_aliases
|
|
679
|
+
|
|
680
|
+
if include_keys:
|
|
681
|
+
if public_key:
|
|
682
|
+
public_key_content = self.ssh_manager.get_public_key_content(
|
|
683
|
+
public_key
|
|
684
|
+
)
|
|
685
|
+
if public_key_content:
|
|
686
|
+
entry["public_key_content"] = public_key_content
|
|
687
|
+
if private_key:
|
|
688
|
+
private_key_content = self.ssh_manager.get_private_key_content(
|
|
689
|
+
private_key
|
|
690
|
+
)
|
|
691
|
+
if private_key_content:
|
|
692
|
+
entry["private_key_content"] = private_key_content
|
|
693
|
+
|
|
694
|
+
exported_accounts[account_name] = entry
|
|
695
|
+
|
|
696
|
+
export_data = {
|
|
697
|
+
"version": 1,
|
|
698
|
+
"include_keys": include_keys,
|
|
699
|
+
"accounts": exported_accounts,
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
output_path = Path(output_path)
|
|
703
|
+
try:
|
|
704
|
+
with open(output_path, "w") as f:
|
|
705
|
+
json.dump(export_data, f, indent=2)
|
|
706
|
+
f.write("\n")
|
|
707
|
+
if include_keys:
|
|
708
|
+
output_path.chmod(0o600)
|
|
709
|
+
except OSError as e:
|
|
710
|
+
echo_color("r", f"Failed to write export file: {e}")
|
|
711
|
+
return False
|
|
712
|
+
|
|
713
|
+
echo_color(
|
|
714
|
+
"g",
|
|
715
|
+
f'Exported {len(exported_accounts)} account(s) to "{output_path}"',
|
|
716
|
+
)
|
|
717
|
+
if include_keys:
|
|
718
|
+
echo_color(
|
|
719
|
+
"y",
|
|
720
|
+
"This file contains private key material in plain text — "
|
|
721
|
+
"keep it secure (e.g. encrypt it or delete it once transferred).",
|
|
722
|
+
)
|
|
723
|
+
else:
|
|
724
|
+
echo_color(
|
|
725
|
+
"y",
|
|
726
|
+
"Metadata only: key files themselves were not copied. Pass "
|
|
727
|
+
"--include-keys to also embed key material.",
|
|
728
|
+
)
|
|
729
|
+
return True
|
|
730
|
+
|
|
731
|
+
def _write_imported_key_file(
|
|
732
|
+
self, path_str: str, content: str, mode: int, force: bool
|
|
733
|
+
) -> bool:
|
|
734
|
+
"""Write key material from an import file to disk, if safe to do so.
|
|
735
|
+
|
|
736
|
+
Args:
|
|
737
|
+
path_str: Destination path for the key file
|
|
738
|
+
content: Key file content to write
|
|
739
|
+
mode: File permission mode to set (e.g. 0o600 for a private key)
|
|
740
|
+
force: If True, overwrite an existing file at that path
|
|
741
|
+
|
|
742
|
+
Returns:
|
|
743
|
+
True if the file was written, False if skipped or on error
|
|
744
|
+
"""
|
|
745
|
+
if not path_str:
|
|
746
|
+
return False
|
|
747
|
+
path = Path(path_str)
|
|
748
|
+
if path.exists() and not force:
|
|
749
|
+
return False
|
|
750
|
+
try:
|
|
751
|
+
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
752
|
+
path.write_text(content)
|
|
753
|
+
path.chmod(mode)
|
|
754
|
+
return True
|
|
755
|
+
except OSError:
|
|
756
|
+
return False
|
|
757
|
+
|
|
758
|
+
def import_accounts(self, input_path: Path, force: bool = False) -> bool:
|
|
759
|
+
"""Import accounts previously written by `export_accounts`.
|
|
760
|
+
|
|
761
|
+
Args:
|
|
762
|
+
input_path: File previously created with `gitacc export`
|
|
763
|
+
force: If True, overwrite existing accounts, host aliases, and
|
|
764
|
+
key files with the same name/path. Without it, anything
|
|
765
|
+
that already exists is left untouched and skipped.
|
|
766
|
+
|
|
767
|
+
Returns:
|
|
768
|
+
True if the file was read and processed, False if the file
|
|
769
|
+
could not be read or contained no accounts
|
|
770
|
+
"""
|
|
771
|
+
input_path = Path(input_path)
|
|
772
|
+
if not input_path.exists():
|
|
773
|
+
echo_color("r", f'Import file not found: "{input_path}"')
|
|
774
|
+
return False
|
|
775
|
+
|
|
776
|
+
try:
|
|
777
|
+
with open(input_path, "r") as f:
|
|
778
|
+
data = json.load(f)
|
|
779
|
+
except (OSError, json.JSONDecodeError) as e:
|
|
780
|
+
echo_color("r", f"Failed to read import file: {e}")
|
|
781
|
+
return False
|
|
782
|
+
|
|
783
|
+
accounts = data.get("accounts", {})
|
|
784
|
+
if not accounts:
|
|
785
|
+
echo_color("y", "No accounts found in import file.")
|
|
786
|
+
return False
|
|
787
|
+
|
|
788
|
+
imported = 0
|
|
789
|
+
skipped = 0
|
|
790
|
+
|
|
791
|
+
for account_name, entry in accounts.items():
|
|
792
|
+
if self.account_exists(account_name) and not force:
|
|
793
|
+
echo_color(
|
|
794
|
+
"y",
|
|
795
|
+
f'Skipping "{account_name}": account already exists '
|
|
796
|
+
"(use --force to overwrite).",
|
|
797
|
+
)
|
|
798
|
+
skipped += 1
|
|
799
|
+
continue
|
|
800
|
+
|
|
801
|
+
private_key = entry.get("private_key", "")
|
|
802
|
+
public_key = entry.get("public_key", "")
|
|
803
|
+
|
|
804
|
+
private_key_content = entry.get("private_key_content")
|
|
805
|
+
if private_key_content:
|
|
806
|
+
self._write_imported_key_file(
|
|
807
|
+
private_key, private_key_content, 0o600, force
|
|
808
|
+
)
|
|
809
|
+
public_key_content = entry.get("public_key_content")
|
|
810
|
+
if public_key_content:
|
|
811
|
+
self._write_imported_key_file(
|
|
812
|
+
public_key, public_key_content, 0o644, force
|
|
813
|
+
)
|
|
814
|
+
|
|
815
|
+
if self.account_exists(account_name):
|
|
816
|
+
self.config_manager.remove_account(account_name)
|
|
817
|
+
|
|
818
|
+
success = self.config_manager.add_account(
|
|
819
|
+
account_name,
|
|
820
|
+
entry.get("name", account_name),
|
|
821
|
+
entry.get("email", ""),
|
|
822
|
+
private_key,
|
|
823
|
+
public_key,
|
|
824
|
+
sign=str(entry.get("sign", "false")).lower() == "true",
|
|
825
|
+
)
|
|
826
|
+
if not success:
|
|
827
|
+
echo_color("r", f'Failed to import account "{account_name}".')
|
|
828
|
+
continue
|
|
829
|
+
|
|
830
|
+
for alias, alias_info in entry.get("host_aliases", {}).items():
|
|
831
|
+
if self.ssh_manager.has_host_alias(alias) and not force:
|
|
832
|
+
continue
|
|
833
|
+
self.ssh_manager.add_host_alias(
|
|
834
|
+
alias,
|
|
835
|
+
alias_info.get("hostname", "github.com"),
|
|
836
|
+
private_key,
|
|
837
|
+
alias_info.get("user", "git"),
|
|
838
|
+
)
|
|
839
|
+
|
|
840
|
+
imported += 1
|
|
841
|
+
echo_color("g", f'Imported account "{account_name}"')
|
|
842
|
+
|
|
843
|
+
echo_color("g", f"Import complete: {imported} imported, {skipped} skipped.")
|
|
844
|
+
return True
|
|
@@ -54,6 +54,9 @@ class CLI:
|
|
|
54
54
|
self._add_update_command(subparsers)
|
|
55
55
|
self._add_autocomplete_command(subparsers)
|
|
56
56
|
self._add_rule_command(subparsers)
|
|
57
|
+
self._add_alias_command(subparsers)
|
|
58
|
+
self._add_export_command(subparsers)
|
|
59
|
+
self._add_import_command(subparsers)
|
|
57
60
|
|
|
58
61
|
# Include account names in top-level completion so `gitacc <TAB>` works for
|
|
59
62
|
# the `gitacc <account>` shorthand, not just for subcommand names.
|
|
@@ -86,6 +89,10 @@ Examples:
|
|
|
86
89
|
gitacc update myaccount Update Git name for an account
|
|
87
90
|
gitacc rule add work ~/work Auto-switch to "work" inside that directory
|
|
88
91
|
gitacc rule hook bash Print the shell hook (add to ~/.bashrc)
|
|
92
|
+
gitacc alias add work github-work Bind a Host alias to an account's key
|
|
93
|
+
gitacc alias list List managed SSH config host aliases
|
|
94
|
+
gitacc export accounts.json Export account metadata to a file
|
|
95
|
+
gitacc import accounts.json Import accounts from an export file
|
|
89
96
|
"""
|
|
90
97
|
|
|
91
98
|
def _add_add_command(self, subparsers: argparse._SubParsersAction) -> None:
|
|
@@ -346,6 +353,109 @@ Examples:
|
|
|
346
353
|
)
|
|
347
354
|
check_parser.set_defaults(func=self._handle_rule_check)
|
|
348
355
|
|
|
356
|
+
def _add_alias_command(self, subparsers: argparse._SubParsersAction) -> None:
|
|
357
|
+
"""Add the 'alias' command parser for SSH config Host aliases."""
|
|
358
|
+
parser = subparsers.add_parser(
|
|
359
|
+
"alias",
|
|
360
|
+
help="Manage per-account SSH config Host aliases",
|
|
361
|
+
description="Add, remove, and list managed Host alias blocks in "
|
|
362
|
+
"~/.ssh/config, so you can clone/push via e.g. "
|
|
363
|
+
"git@<alias>:org/repo using a specific account's key",
|
|
364
|
+
)
|
|
365
|
+
alias_subparsers = parser.add_subparsers(
|
|
366
|
+
dest="alias_command",
|
|
367
|
+
help="Alias commands",
|
|
368
|
+
)
|
|
369
|
+
|
|
370
|
+
add_parser = alias_subparsers.add_parser(
|
|
371
|
+
"add",
|
|
372
|
+
help="Add or update a Host alias for an account",
|
|
373
|
+
description="Bind an SSH config Host alias to an account's key",
|
|
374
|
+
)
|
|
375
|
+
account_arg = add_parser.add_argument(
|
|
376
|
+
"account_name",
|
|
377
|
+
help="Account whose key the alias should use",
|
|
378
|
+
)
|
|
379
|
+
add_parser.add_argument(
|
|
380
|
+
"host_alias",
|
|
381
|
+
help="Host alias to create/update (e.g. github-work)",
|
|
382
|
+
)
|
|
383
|
+
add_parser.add_argument(
|
|
384
|
+
"--hostname",
|
|
385
|
+
dest="hostname",
|
|
386
|
+
default="github.com",
|
|
387
|
+
help="Real host to connect to (default: github.com)",
|
|
388
|
+
)
|
|
389
|
+
add_parser.add_argument(
|
|
390
|
+
"--user",
|
|
391
|
+
dest="ssh_user",
|
|
392
|
+
default="git",
|
|
393
|
+
help="SSH user for the connection (default: git)",
|
|
394
|
+
)
|
|
395
|
+
if argcomplete:
|
|
396
|
+
account_arg.completer = lambda **kwargs: get_account_names()
|
|
397
|
+
add_parser.set_defaults(func=self._handle_alias_add)
|
|
398
|
+
|
|
399
|
+
remove_parser = alias_subparsers.add_parser(
|
|
400
|
+
"remove",
|
|
401
|
+
help="Remove a Host alias",
|
|
402
|
+
)
|
|
403
|
+
remove_parser.add_argument(
|
|
404
|
+
"host_alias",
|
|
405
|
+
help="Host alias to remove",
|
|
406
|
+
)
|
|
407
|
+
remove_parser.set_defaults(func=self._handle_alias_remove)
|
|
408
|
+
|
|
409
|
+
list_parser = alias_subparsers.add_parser(
|
|
410
|
+
"list",
|
|
411
|
+
help="List all managed Host aliases",
|
|
412
|
+
)
|
|
413
|
+
list_parser.set_defaults(func=self._handle_alias_list)
|
|
414
|
+
|
|
415
|
+
def _add_export_command(self, subparsers: argparse._SubParsersAction) -> None:
|
|
416
|
+
"""Add the 'export' command parser."""
|
|
417
|
+
parser = subparsers.add_parser(
|
|
418
|
+
"export",
|
|
419
|
+
help="Export account metadata to a file",
|
|
420
|
+
description="Export registered accounts (metadata only by "
|
|
421
|
+
"default) to a portable file for backup or migration to "
|
|
422
|
+
"another machine",
|
|
423
|
+
)
|
|
424
|
+
parser.add_argument(
|
|
425
|
+
"output_file",
|
|
426
|
+
help="File to write the export to",
|
|
427
|
+
)
|
|
428
|
+
parser.add_argument(
|
|
429
|
+
"--include-keys",
|
|
430
|
+
dest="include_keys",
|
|
431
|
+
action="store_true",
|
|
432
|
+
help="Also embed each account's private/public SSH key file "
|
|
433
|
+
"contents in the export. The output file will contain private "
|
|
434
|
+
"key material in plain text (written with 0600 permissions) — "
|
|
435
|
+
"keep it secure.",
|
|
436
|
+
)
|
|
437
|
+
parser.set_defaults(func=self._handle_export)
|
|
438
|
+
|
|
439
|
+
def _add_import_command(self, subparsers: argparse._SubParsersAction) -> None:
|
|
440
|
+
"""Add the 'import' command parser."""
|
|
441
|
+
parser = subparsers.add_parser(
|
|
442
|
+
"import",
|
|
443
|
+
help="Import accounts from an export file",
|
|
444
|
+
description="Import accounts previously created with 'gitacc export'",
|
|
445
|
+
)
|
|
446
|
+
parser.add_argument(
|
|
447
|
+
"input_file",
|
|
448
|
+
help="File previously created with 'gitacc export'",
|
|
449
|
+
)
|
|
450
|
+
parser.add_argument(
|
|
451
|
+
"--force",
|
|
452
|
+
dest="force",
|
|
453
|
+
action="store_true",
|
|
454
|
+
help="Overwrite existing accounts, host aliases, and key files "
|
|
455
|
+
"with the same name/path",
|
|
456
|
+
)
|
|
457
|
+
parser.set_defaults(func=self._handle_import)
|
|
458
|
+
|
|
349
459
|
def _handle_autocomplete_install(self, args: argparse.Namespace) -> int:
|
|
350
460
|
"""Handle the 'autocomplete install' command."""
|
|
351
461
|
if not argcomplete:
|
|
@@ -657,6 +767,47 @@ Examples:
|
|
|
657
767
|
success = self.account_manager.switch_account(account_name)
|
|
658
768
|
return 0 if success else 1
|
|
659
769
|
|
|
770
|
+
def _handle_alias_add(self, args: argparse.Namespace) -> int:
|
|
771
|
+
"""Handle the 'alias add' command."""
|
|
772
|
+
success = self.account_manager.add_host_alias(
|
|
773
|
+
args.account_name, args.host_alias, args.hostname, args.ssh_user
|
|
774
|
+
)
|
|
775
|
+
return 0 if success else 1
|
|
776
|
+
|
|
777
|
+
def _handle_alias_remove(self, args: argparse.Namespace) -> int:
|
|
778
|
+
"""Handle the 'alias remove' command."""
|
|
779
|
+
success = self.account_manager.remove_host_alias(args.host_alias)
|
|
780
|
+
return 0 if success else 1
|
|
781
|
+
|
|
782
|
+
def _handle_alias_list(self, args: argparse.Namespace) -> int:
|
|
783
|
+
"""Handle the 'alias list' command."""
|
|
784
|
+
aliases = self.account_manager.list_host_aliases()
|
|
785
|
+
if not aliases:
|
|
786
|
+
echo_color("y", "No managed host aliases yet.")
|
|
787
|
+
return 0
|
|
788
|
+
|
|
789
|
+
echo_color("g", "Managed SSH config host aliases:")
|
|
790
|
+
for host_alias, info in aliases.items():
|
|
791
|
+
print(
|
|
792
|
+
f" {host_alias} → {info['user']}@{info['hostname']} "
|
|
793
|
+
f"({info['identity_file']})"
|
|
794
|
+
)
|
|
795
|
+
return 0
|
|
796
|
+
|
|
797
|
+
def _handle_export(self, args: argparse.Namespace) -> int:
|
|
798
|
+
"""Handle the 'export' command."""
|
|
799
|
+
success = self.account_manager.export_accounts(
|
|
800
|
+
args.output_file, include_keys=getattr(args, "include_keys", False)
|
|
801
|
+
)
|
|
802
|
+
return 0 if success else 1
|
|
803
|
+
|
|
804
|
+
def _handle_import(self, args: argparse.Namespace) -> int:
|
|
805
|
+
"""Handle the 'import' command."""
|
|
806
|
+
success = self.account_manager.import_accounts(
|
|
807
|
+
args.input_file, force=getattr(args, "force", False)
|
|
808
|
+
)
|
|
809
|
+
return 0 if success else 1
|
|
810
|
+
|
|
660
811
|
def _handle_account_name_shortcut(self, account_name: str) -> int:
|
|
661
812
|
"""Handle account name as shortcut for switch command."""
|
|
662
813
|
success = self.account_manager.switch_account(account_name)
|