|
3 | 3 | import json
|
4 | 4 | import argparse
|
5 | 5 | import requests
|
| 6 | +import win32cred |
6 | 7 | import webbrowser
|
| 8 | +import pywintypes |
7 | 9 | from tkinter import filedialog
|
8 | 10 | from colorama import init, Fore
|
9 | 11 |
|
10 | 12 | init(autoreset=True)
|
11 | 13 |
|
12 |
| -version = "1.5" |
| 14 | +version = "1.6" |
13 | 15 | script_path = os.path.dirname(os.path.abspath(sys.argv[0]))
|
14 | 16 | config_path = os.path.join(script_path, "config.json")
|
15 | 17 |
|
16 | 18 | # -----------------------------------------------------------------------------------------------------
|
17 | 19 |
|
18 | 20 | def read_token():
|
| 21 | + # 凭据 github-access-token.glm |
19 | 22 | try:
|
20 |
| - with open(config_path, 'r') as file: |
21 |
| - data = json.load(file) |
22 |
| - token = data.get('token') |
23 |
| - if not token.startswith('ghp_'): |
24 |
| - print(f"{Fore.YELLOW}⚠{Fore.RESET} Please check whether the Token is correct.") |
25 |
| - try: |
26 |
| - input(f"Press {Fore.BLUE}Enter{Fore.RESET} to confirm, press {Fore.BLUE}Ctrl + C{Fore.RESET} to cancel...") |
27 |
| - except KeyboardInterrupt: |
28 |
| - print(f"{Fore.BLUE}[!]{Fore.RESET} Cancelled operation.") |
29 |
| - return "token error" |
30 |
| - return token |
| 23 | + token = win32cred.CredRead("github-access-token.glm", win32cred.CRED_TYPE_GENERIC) |
| 24 | + return token['CredentialBlob'].decode() |
| 25 | + except pywintypes.error as e: |
| 26 | + print(f"{Fore.YELLOW}⚠{Fore.RESET} You may not have set the Token yet, please try using the following command to set the Token:\n glm config --token <YOUR-TOKEN>\n") |
| 27 | + return "error" |
31 | 28 | except Exception as e:
|
32 |
| - print(f"{Fore.RED}✕{Fore.RESET} Error reading configuration file:\n{Fore.RED}{e}{Fore.RESET}") |
| 29 | + print(f"{Fore.RED}✕{Fore.RESET} Error reading Token:\n{Fore.RED}{e}{Fore.RESET}") |
33 | 30 | return "error"
|
34 | 31 |
|
35 | 32 | def set_token(token):
|
36 |
| - try: |
37 |
| - # --- Token 检查 --- |
38 |
| - if not token.startswith('ghp_'): |
39 |
| - print(f"{Fore.YELLOW}⚠{Fore.RESET} Please check whether the Token is correct.") |
40 |
| - try: |
41 |
| - input(f"Press {Fore.BLUE}Enter{Fore.RESET} to confirm, press {Fore.BLUE}Ctrl + C{Fore.RESET} to cancel...") |
42 |
| - except KeyboardInterrupt: |
43 |
| - print(f"{Fore.BLUE}[!]{Fore.RESET} Cancelled operation.") |
44 |
| - return "error" |
45 |
| - # ----------------- |
| 33 | + # 凭据 github-access-token.glm |
| 34 | + # == 移除 == |
| 35 | + if token == "remove": |
| 36 | + print(f"{Fore.YELLOW}⚠{Fore.RESET} Are you sure you want to remove the set Token?") |
| 37 | + try: |
| 38 | + input(f"Press {Fore.BLUE}Enter{Fore.RESET} to confirm, press {Fore.BLUE}Ctrl + C{Fore.RESET} to cancel...") |
| 39 | + win32cred.CredDelete("github-access-token.glm", win32cred.CRED_TYPE_GENERIC) |
| 40 | + print(f"{Fore.GREEN}✓{Fore.RESET} The Token was successfully removed.") |
| 41 | + return "successful" |
| 42 | + except KeyboardInterrupt: |
| 43 | + print(f"{Fore.BLUE}[!]{Fore.RESET} Cancelled operation.") |
| 44 | + return "error" |
| 45 | + except Exception as e: |
| 46 | + print(f"{Fore.RED}✕{Fore.RESET} An error occurred removing the set Token:\n{Fore.RED}{e}{Fore.RESET}\n") |
| 47 | + return "error" |
46 | 48 |
|
47 |
| - # 读取现有的 JSON 文件 |
48 |
| - with open(config_path, 'r') as file: |
49 |
| - data = json.load(file) |
50 |
| - |
51 |
| - # 更新 token 字段 |
52 |
| - data['token'] = token |
53 |
| - |
54 |
| - # 将更新后的 JSON 写回文件 |
55 |
| - with open(config_path, 'w') as file: |
56 |
| - json.dump(data, file, indent=4) |
| 49 | + # == 添加 == |
| 50 | + # --- Token 检查 --- |
| 51 | + if not token.startswith('ghp_'): |
| 52 | + print(f"{Fore.YELLOW}⚠{Fore.RESET} Please check whether the Token is correct.") |
| 53 | + try: |
| 54 | + input(f"Press {Fore.BLUE}Enter{Fore.RESET} to confirm, press {Fore.BLUE}Ctrl + C{Fore.RESET} to cancel...") |
| 55 | + except KeyboardInterrupt: |
| 56 | + print(f"{Fore.BLUE}[!]{Fore.RESET} Cancelled operation.") |
| 57 | + return "error" |
| 58 | + # ----------------- |
| 59 | + |
| 60 | + cred = { |
| 61 | + 'Type': win32cred.CRED_TYPE_GENERIC, |
| 62 | + 'TargetName': "github-access-token.glm", |
| 63 | + 'UserName': "github-access-token", |
| 64 | + 'CredentialBlob': token, |
| 65 | + 'Persist': win32cred.CRED_PERSIST_ENTERPRISE |
| 66 | + } |
| 67 | + |
| 68 | + try: |
| 69 | + win32cred.CredWrite(cred, 0) |
57 | 70 | print(f"{Fore.GREEN}✓{Fore.RESET} Successfully update Token.")
|
58 | 71 | return "successful"
|
59 | 72 | except Exception as e:
|
60 |
| - print(f"{Fore.RED}✕{Fore.RESET} An error occurred processing the configuration file:\n{Fore.RED}{e}{Fore.RESET}\n") |
| 73 | + print(f"{Fore.RED}✕{Fore.RESET} An error occurred setting Token:\n{Fore.RED}{e}{Fore.RESET}\n") |
61 | 74 | return "error"
|
62 | 75 |
|
63 | 76 | def formatting_url(url):
|
@@ -373,8 +386,12 @@ def main():
|
373 | 386 | with open(config_path, 'r') as file:
|
374 | 387 | data = json.load(file)
|
375 | 388 |
|
376 |
| - token = data.get('token') |
377 |
| - print(f"{Fore.GREEN}✓{Fore.RESET} Current configuration information:\n Account configuration:\n Token: {Fore.BLUE}{token}{Fore.RESET}\n Program configuration:\n Version: {Fore.BLUE}GitHub Labels Manager v{version} by 鸭鸭「カモ」{Fore.RESET}\n Installed in: {Fore.BLUE}{script_path}{Fore.RESET}") |
| 389 | + if read_token() != 'error': |
| 390 | + token = f"{Fore.GREEN}Set{Fore.RESET}" |
| 391 | + else: |
| 392 | + token = f"{Fore.YELLOW}Not set or read error{Fore.RESET}" |
| 393 | + |
| 394 | + print(f"{Fore.GREEN}✓{Fore.RESET} Current configuration information:\n Account configuration:\n Token: {token}\n Program configuration:\n Version: {Fore.BLUE}GitHub Labels Manager v{version} by 鸭鸭「カモ」{Fore.RESET}\n Installed in: {Fore.BLUE}{script_path}{Fore.RESET}") |
378 | 395 | elif args.token:
|
379 | 396 | running_result = set_token(args.token)
|
380 | 397 | if running_result == "error":
|
|
0 commit comments