Skip to content

Commit aff38b1

Browse files
authored
Feat/swanlab-logout (#597)
* cli-swanlab-logout * update confirm
1 parent e151da2 commit aff38b1

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

swanlab/cli/main.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
from .utils import is_valid_ip, is_valid_port, is_valid_root_dir, URL
1313
from swanlab.package import version_limit, get_package_version, is_login
1414
from swanlab.api.auth import terminal_login
15-
from ..env import get_server_host, get_server_port, get_swanlog_dir
15+
from ..env import get_server_host, get_server_port, get_swanlog_dir, get_swanlab_folder
1616
from ..db import connect
1717
from ..utils import FONT
1818
import time
19+
import shutil
1920

2021

2122
@click.group(invoke_without_command=True)
@@ -145,6 +146,28 @@ def login(api_key: str, relogin: bool, **kwargs):
145146
print(FONT.swanlab("Login successfully. Hi, " + FONT.bold(FONT.default(login_info.username))) + "!")
146147

147148

149+
# ---------------------------------- 退出登录命令 ----------------------------------
150+
@cli.command()
151+
def logout(**kwargs):
152+
"""Logout to the swanlab cloud."""
153+
command = FONT.bold("swanlab login")
154+
if is_login():
155+
# 如果已经是登录状态,那么则询问用户是否确认,如果确认则删除token文件夹
156+
confirm = input(FONT.swanlab("Are you sure you want to logout? (y/N): "))
157+
if confirm.lower() == "y":
158+
try:
159+
shutil.rmtree(get_swanlab_folder())
160+
return print(FONT.swanlab("Logout successfully. You can use `" + command + "` to login again."))
161+
except Exception as e:
162+
return print(FONT.swanlab("Logout failed. Please check if you have file operation permissions."))
163+
else:
164+
return print(FONT.swanlab("Logout canceled."))
165+
166+
# 如果还未登录,则不做任何处理,并告知用户如何登录
167+
tip = FONT.swanlab("You are not logged in. If you want to login in, please use `" + command + "` to login.")
168+
return print(tip)
169+
170+
148171
# ---------------------------------- 转换命令,用于转换其他实验跟踪工具 ----------------------------------
149172
@cli.command()
150173
@click.option(

0 commit comments

Comments
 (0)