1
1
import os
2
2
import sys
3
3
import json
4
+ import keyring
4
5
import argparse
5
6
import requests
6
- import win32cred
7
7
import webbrowser
8
- import pywintypes
9
8
from tkinter import filedialog
10
9
from colorama import init , Fore
11
10
12
11
init (autoreset = True )
13
12
14
- version = "1.6 "
13
+ version = "1.7 "
15
14
script_path = os .path .dirname (os .path .abspath (sys .argv [0 ]))
16
15
config_path = os .path .join (script_path , "config.json" )
17
16
20
19
def read_token ():
21
20
# 凭据 github-access-token.glm
22
21
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
28
28
except Exception as e :
29
29
print (f"{ Fore .RED } ✕{ Fore .RESET } Error reading Token:\n { Fore .RED } { e } { Fore .RESET } " )
30
30
return "error"
@@ -36,7 +36,7 @@ def set_token(token):
36
36
print (f"{ Fore .YELLOW } ⚠{ Fore .RESET } Are you sure you want to remove the set Token?" )
37
37
try :
38
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 )
39
+ keyring . delete_password ("github-access-token.glm" , "github-access-token" )
40
40
print (f"{ Fore .GREEN } ✓{ Fore .RESET } The Token was successfully removed." )
41
41
return "successful"
42
42
except KeyboardInterrupt :
@@ -57,16 +57,8 @@ def set_token(token):
57
57
return "error"
58
58
# -----------------
59
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
60
try :
69
- win32cred . CredWrite ( cred , 0 )
61
+ keyring . set_password ( "github-access-token.glm" , "github-access-token" , token )
70
62
print (f"{ Fore .GREEN } ✓{ Fore .RESET } Successfully update Token." )
71
63
return "successful"
72
64
except Exception as e :
@@ -164,19 +156,23 @@ def get_labels(url, save):
164
156
return "successful"
165
157
166
158
# ---------------------------------------------------------------------------
167
- def clear_labels (url , token ):
159
+ def clear_labels (url , token , yes = False ):
168
160
# 本函数有以下行为
169
161
# 正常操作清空指定仓库标签,并返回successful,错误时输出错误原因并返回具体错误信息
170
162
# 可能返回如下错误
171
163
# cancel 操作取消 | get error 获取时出错
172
164
165
+ # v1.7
166
+ # 在调用时如果传入 yes=True则直接确认所有提示
167
+
173
168
flag = 0
174
169
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"
180
176
181
177
# 请求头
182
178
headers = {
@@ -198,12 +194,13 @@ def clear_labels(url, token):
198
194
else :
199
195
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 } " )
200
196
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"
207
204
208
205
if flag :
209
206
print (f"{ Fore .YELLOW } ⚠{ Fore .RESET } The operation is complete with { Fore .YELLOW } { flag } { Fore .RESET } failed items." )
@@ -322,6 +319,7 @@ def main():
322
319
parser_clear = subparsers .add_parser ('clear' , help = 'Clear labels' )
323
320
parser_clear .add_argument ('repo_url' , type = str , help = 'GitHub repo URL' )
324
321
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' )
325
323
326
324
args = parser .parse_args ()
327
325
@@ -419,7 +417,7 @@ def main():
419
417
token = read_token ()
420
418
if token in ["error" , "token error" ]:
421
419
return 1 , running_result
422
- running_result = clear_labels (running_result , token )
420
+ running_result = clear_labels (running_result , token , args . yes )
423
421
if running_result in ["cancel" ]:
424
422
return 1 , running_result
425
423
else :
0 commit comments