-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdline.py
More file actions
92 lines (73 loc) · 2.68 KB
/
cmdline.py
File metadata and controls
92 lines (73 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#! /usr/bin/env python
"""
A Command Line Tool for managing iCloud keyring credentials.
"""
import argparse
import getpass
import sys
from typing import NoReturn, Sequence
from foundation import version_info_formatted
from foundation.predicates import in_pred
from foundation.string_utils import strip_and_lower
from . import utils
def main(args: Sequence[str] | None = None) -> NoReturn:
"""Main commandline entrypoint for iCloud keyring management."""
if args is None:
args = sys.argv[1:]
parser = argparse.ArgumentParser(description="iCloud Keyring Management Tool")
parser.add_argument(
"--username",
action="store",
dest="username",
help="Apple ID username",
)
parser.add_argument(
"--delete-from-keyring",
action="store_true",
dest="delete_from_keyring",
default=False,
help="Delete stored password from system keyring for this username",
)
parser.add_argument(
"--version",
action="store_true",
dest="version",
help="Show the version, commit hash and timestamp",
)
command_line = parser.parse_args(args)
if command_line.version:
print(version_info_formatted())
sys.exit(0)
username: str | None = strip_and_lower(command_line.username) if command_line.username else None
if username is None:
parser.error("--username is required")
if command_line.delete_from_keyring:
# Delete password from keyring
if utils.password_exists_in_keyring(username):
user_response = strip_and_lower(
input(f"Delete password for {username} from keyring? [y/N] ")
)
is_affirmative = in_pred(["y", "yes"])
if is_affirmative(user_response):
utils.delete_password_in_keyring(username)
print(f"Password for {username} deleted from keyring")
else:
print("Operation cancelled")
else:
print(f"No password found in keyring for {username}")
else:
# Store password in keyring
password = getpass.getpass(f"Enter iCloud password for {username}: ")
if not password:
print("No password provided", file=sys.stderr)
sys.exit(1)
user_response = strip_and_lower(input(f"Save password for {username} to keyring? [y/N] "))
is_affirmative = in_pred(["y", "yes"])
if is_affirmative(user_response):
utils.store_password_in_keyring(username, password)
print(f"Password for {username} saved to keyring")
else:
print("Password not saved")
sys.exit(0)
if __name__ == "__main__":
main()