Skip to content

Commit d90fd4b

Browse files
fix: 修正 Token 读取
详细修改内容: - deps - remove: 移除依赖 pywin32 - add: 添加依赖 keyring - Bump version to 1.7 - fix: 修正 Token 读取错误的问题 (即400状态码) - feat: clear 命令可以通过添加 --yes 参数忽略(直接确认)操作中的所有提示 - i18n
1 parent 9eacc66 commit d90fd4b

File tree

3 files changed

+59
-63
lines changed

3 files changed

+59
-63
lines changed

other-languages/en_US/glm.py

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import os
22
import sys
33
import json
4+
import keyring
45
import argparse
56
import requests
6-
import win32cred
77
import webbrowser
8-
import pywintypes
98
from tkinter import filedialog
109
from colorama import init, Fore
1110

1211
init(autoreset=True)
1312

14-
version = "1.6"
13+
version = "1.7"
1514
script_path = os.path.dirname(os.path.abspath(sys.argv[0]))
1615
config_path = os.path.join(script_path, "config.json")
1716

@@ -20,11 +19,12 @@
2019
def read_token():
2120
# 凭据 github-access-token.glm
2221
try:
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"
22+
token = keyring.get_password("github-access-token.glm", "github-access-token")
23+
if token == None:
24+
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")
25+
return "error"
26+
# else:
27+
return token
2828
except Exception as e:
2929
print(f"{Fore.RED}{Fore.RESET} Error reading Token:\n{Fore.RED}{e}{Fore.RESET}")
3030
return "error"
@@ -36,7 +36,7 @@ def set_token(token):
3636
print(f"{Fore.YELLOW}{Fore.RESET} Are you sure you want to remove the set Token?")
3737
try:
3838
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)
39+
keyring.delete_password("github-access-token.glm", "github-access-token")
4040
print(f"{Fore.GREEN}{Fore.RESET} The Token was successfully removed.")
4141
return "successful"
4242
except KeyboardInterrupt:
@@ -57,16 +57,8 @@ def set_token(token):
5757
return "error"
5858
# -----------------
5959

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-
6860
try:
69-
win32cred.CredWrite(cred, 0)
61+
keyring.set_password("github-access-token.glm", "github-access-token", token)
7062
print(f"{Fore.GREEN}{Fore.RESET} Successfully update Token.")
7163
return "successful"
7264
except Exception as e:
@@ -164,19 +156,23 @@ def get_labels(url, save):
164156
return "successful"
165157

166158
# ---------------------------------------------------------------------------
167-
def clear_labels(url, token):
159+
def clear_labels(url, token, yes=False):
168160
# 本函数有以下行为
169161
# 正常操作清空指定仓库标签,并返回successful,错误时输出错误原因并返回具体错误信息
170162
# 可能返回如下错误
171163
# cancel 操作取消 | get error 获取时出错
172164

165+
# v1.7
166+
# 在调用时如果传入 yes=True则直接确认所有提示
167+
173168
flag = 0
174169

175-
# 确认
176-
print(f"{Fore.BLUE}?{Fore.RESET} Confirm deletion?\n{Fore.YELLOW}{Fore.RESET} This operation will empty {Fore.YELLOW}all{Fore.RESET} labels, irrevocable!")
177-
if not (input("[Y] Confirm [N] Cancel:").lower() in ["y", "yes", "confirm"]):
178-
print(f"{Fore.BLUE}[!]{Fore.RESET} Cancelled operation.")
179-
return "cancel"
170+
if not yes:
171+
# 确认
172+
print(f"{Fore.BLUE}?{Fore.RESET} Confirm deletion?\n{Fore.YELLOW}{Fore.RESET} This operation will empty {Fore.YELLOW}all{Fore.RESET} labels, irrevocable!")
173+
if not (input("[Y] Confirm [N] Cancel:").lower() in ["y", "yes", "confirm"]):
174+
print(f"{Fore.BLUE}[!]{Fore.RESET} Cancelled operation.")
175+
return "cancel"
180176

181177
# 请求头
182178
headers = {
@@ -198,12 +194,13 @@ def clear_labels(url, token):
198194
else:
199195
print(f"{Fore.RED}{Fore.RESET} Failed to delete label {Fore.BLUE}{label['name']}{Fore.RESET}: {Fore.YELLOW}{response.status_code}{Fore.RESET}\n{Fore.RED}{response.text}{Fore.RESET}")
200196
flag += 1
201-
print(f"{Fore.BLUE}?{Fore.RESET} Continue?")
202-
try:
203-
input(f"Press {Fore.BLUE}Enter{Fore.RESET} to confirm, press {Fore.BLUE}Ctrl + C{Fore.RESET} to cancel...")
204-
except KeyboardInterrupt:
205-
print(f"{Fore.BLUE}[!]{Fore.RESET} Cancelled operation.")
206-
return "cancel"
197+
if not yes:
198+
print(f"{Fore.BLUE}?{Fore.RESET} Continue?")
199+
try:
200+
input(f"Press {Fore.BLUE}Enter{Fore.RESET} to confirm, press {Fore.BLUE}Ctrl + C{Fore.RESET} to cancel...")
201+
except KeyboardInterrupt:
202+
print(f"{Fore.BLUE}[!]{Fore.RESET} Cancelled operation.")
203+
return "cancel"
207204

208205
if flag:
209206
print(f"{Fore.YELLOW}{Fore.RESET} The operation is complete with {Fore.YELLOW}{flag}{Fore.RESET} failed items.")
@@ -322,6 +319,7 @@ def main():
322319
parser_clear = subparsers.add_parser('clear', help='Clear labels')
323320
parser_clear.add_argument('repo_url', type=str, help='GitHub repo URL')
324321
parser_clear.add_argument('--token', type=str, help='GitHub Token')
322+
parser_clear.add_argument('--yes', help='Ignore (confirm directly) all prompts in the operation', action='store_true')
325323

326324
args = parser.parse_args()
327325

@@ -419,7 +417,7 @@ def main():
419417
token = read_token()
420418
if token in ["error", "token error"]:
421419
return 1, running_result
422-
running_result = clear_labels(running_result, token)
420+
running_result = clear_labels(running_result, token, args.yes)
423421
if running_result in ["cancel"]:
424422
return 1, running_result
425423
else:

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
colorama==0.4.6
22
requests==2.32.3
3-
pywin32==306
3+
keyring==25.5.0

程序脚本/glm.py

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import os
22
import sys
33
import json
4+
import keyring
45
import argparse
56
import requests
6-
import win32cred
77
import webbrowser
8-
import pywintypes
98
from tkinter import filedialog
109
from colorama import init, Fore
1110

1211
init(autoreset=True)
1312

14-
version = "1.6"
13+
version = "1.7"
1514
script_path = os.path.dirname(os.path.abspath(sys.argv[0]))
1615
config_path = os.path.join(script_path, "config.json")
1716

@@ -20,11 +19,12 @@
2019
def read_token():
2120
# 凭据 github-access-token.glm
2221
try:
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} 你可能还没设置Token, 请尝试使用以下命令设置Token:\n glm config --token <YOUR-TOKEN>\n")
27-
return "error"
22+
token = keyring.get_password("github-access-token.glm", "github-access-token")
23+
if token == None:
24+
print(f"{Fore.YELLOW}{Fore.RESET} 你可能还没设置Token, 请尝试使用以下命令设置Token:\n glm config --token <YOUR-TOKEN>\n")
25+
return "error"
26+
# else:
27+
return token
2828
except Exception as e:
2929
print(f"{Fore.RED}{Fore.RESET} 读取Token时出错:\n{Fore.RED}{e}{Fore.RESET}")
3030
return "error"
@@ -36,7 +36,7 @@ def set_token(token):
3636
print(f"{Fore.YELLOW}{Fore.RESET} 确定要移除设置的Token?")
3737
try:
3838
input(f"按{Fore.BLUE}Enter{Fore.RESET}键确认,按{Fore.BLUE}Ctrl + C{Fore.RESET}键取消...")
39-
win32cred.CredDelete("github-access-token.glm", win32cred.CRED_TYPE_GENERIC)
39+
keyring.delete_password("github-access-token.glm", "github-access-token")
4040
print(f"{Fore.GREEN}{Fore.RESET} 成功移除设置的Token")
4141
return "successful"
4242
except KeyboardInterrupt:
@@ -57,16 +57,8 @@ def set_token(token):
5757
return "error"
5858
# -----------------
5959

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-
6860
try:
69-
win32cred.CredWrite(cred, 0)
61+
keyring.set_password("github-access-token.glm", "github-access-token", token)
7062
print(f"{Fore.GREEN}{Fore.RESET} 成功更新Token")
7163
return "successful"
7264
except Exception as e:
@@ -164,19 +156,23 @@ def get_labels(url, save):
164156
return "successful"
165157

166158
# ---------------------------------------------------------------------------
167-
def clear_labels(url, token):
159+
def clear_labels(url, token, yes=False):
168160
# 本函数有以下行为
169161
# 正常操作清空指定仓库标签,并返回successful,错误时输出错误原因并返回具体错误信息
170162
# 可能返回如下错误
171163
# cancel 操作取消 | get error 获取时出错
172164

165+
# v1.7
166+
# 在调用时如果传入 yes=True则直接确认所有提示
167+
173168
flag = 0
174169

175-
# 确认
176-
print(f"{Fore.BLUE}?{Fore.RESET} 确认删除?\n{Fore.YELLOW}{Fore.RESET} 此操作将清空指定仓库的{Fore.YELLOW}所有{Fore.RESET}标签,不可撤销!")
177-
if not (input("[Y]确认 [N]取消 : ").lower() in ["y", "yes", "确认"]):
178-
print(f"{Fore.BLUE}[!]{Fore.RESET} 已取消操作")
179-
return "cancel"
170+
if not yes:
171+
# 确认
172+
print(f"{Fore.BLUE}?{Fore.RESET} 确认删除?\n{Fore.YELLOW}{Fore.RESET} 此操作将清空指定仓库的{Fore.YELLOW}所有{Fore.RESET}标签,不可撤销!")
173+
if not (input("[Y]确认 [N]取消 : ").lower() in ["y", "yes", "确认"]):
174+
print(f"{Fore.BLUE}[!]{Fore.RESET} 已取消操作")
175+
return "cancel"
180176

181177
# 请求头
182178
headers = {
@@ -198,12 +194,13 @@ def clear_labels(url, token):
198194
else:
199195
print(f"{Fore.RED}{Fore.RESET} 删除标签 {Fore.BLUE}{label['name']}{Fore.RESET} 时失败: {Fore.YELLOW}{response.status_code}{Fore.RESET}\n{Fore.RED}{response.text}{Fore.RESET}")
200196
flag += 1
201-
print(f"{Fore.BLUE}?{Fore.RESET} 是否继续?")
202-
try:
203-
input(f"按{Fore.BLUE}Enter{Fore.RESET}键确认,按{Fore.BLUE}Ctrl + C{Fore.RESET}键取消...")
204-
except KeyboardInterrupt:
205-
print(f"{Fore.BLUE}[!]{Fore.RESET} 已取消操作")
206-
return "cancel"
197+
if not yes:
198+
print(f"{Fore.BLUE}?{Fore.RESET} 是否继续?")
199+
try:
200+
input(f"按{Fore.BLUE}Enter{Fore.RESET}键确认,按{Fore.BLUE}Ctrl + C{Fore.RESET}键取消...")
201+
except KeyboardInterrupt:
202+
print(f"{Fore.BLUE}[!]{Fore.RESET} 已取消操作")
203+
return "cancel"
207204

208205
if flag:
209206
print(f"{Fore.YELLOW}{Fore.RESET} 操作完成,共出现 {Fore.YELLOW}{flag}{Fore.RESET} 个失败项。")
@@ -322,6 +319,7 @@ def main():
322319
parser_clear = subparsers.add_parser('clear', help='清空标签')
323320
parser_clear.add_argument('repo_url', type=str, help='GitHub仓库URL')
324321
parser_clear.add_argument('--token', type=str, help='GitHub访问令牌')
322+
parser_clear.add_argument('--yes', help='忽略(直接确认)操作中的所有提示', action='store_true')
325323

326324
args = parser.parse_args()
327325

@@ -419,7 +417,7 @@ def main():
419417
token = read_token()
420418
if token in ["error", "token error"]:
421419
return 1, running_result
422-
running_result = clear_labels(running_result, token)
420+
running_result = clear_labels(running_result, token, args.yes)
423421
if running_result in ["cancel"]:
424422
return 1, running_result
425423
else:

0 commit comments

Comments
 (0)