Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion swanlab/cli/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@Description:
暴露子命令
"""
from .auth import login, logout
from .auth import login, logout, verify
from .converter import convert
from .dashboard import watch
from .sync import sync
1 change: 1 addition & 0 deletions swanlab/cli/commands/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
"""
from .login import login
from .logout import logout
from .verify import verify
25 changes: 25 additions & 0 deletions swanlab/cli/commands/auth/verify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""
@author: cunyue
@file: verify.py
@time: 2025/9/23 11:06
@description: 验证用户当前登录状态
"""

import click
from rich.text import Text

from swanlab.core_python import auth
from swanlab.error import KeyFileError
from swanlab.log import swanlog
from swanlab.package import get_key


@click.command()
def verify():
"""Verify the current login status."""
try:
key = get_key()
except KeyFileError:
raise click.ClickException("You are not verified. Please use `swanlab login` to login.")
login_info = auth.code_login(key, save_key=False)
swanlog.info('You are logged into', login_info.web_host, 'as', Text(login_info.username, "bold"))
2 changes: 2 additions & 0 deletions swanlab/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def cli():
cli.add_command(C.login) # 登录
# noinspection PyTypeChecker
cli.add_command(C.logout) # 登出
# noinspection PyTypeChecker
cli.add_command(C.verify) # 验证当前登录状态

# noinspection PyTypeChecker
cli.add_command(C.watch) # 启动服务
Expand Down
33 changes: 33 additions & 0 deletions test/unit/cli/test_cli_verify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
@author: cunyue
@file: test_cli_verify.py
@time: 2025/9/23 11:22
@description: 测试cli verify命令
"""

import os

import pytest
from click.testing import CliRunner

import tutils as T
from swanlab.cli.main import cli


# noinspection PyTypeChecker
@pytest.mark.skipif(T.is_skip_cloud_test, reason="skip cloud test")
def test_verify_ok():
runner = CliRunner()
result = runner.invoke(cli, ["verify"])
assert result.exit_code == 0
assert ("You are logged into " + T.WEB_HOST) in result.output


# noinspection PyTypeChecker
def test_verify_error():
runner = CliRunner()
if T.SwanLabEnv.API_KEY.value in os.environ:
del os.environ[T.SwanLabEnv.API_KEY.value]
result = runner.invoke(cli, ["verify"])
assert result.exit_code == 1
assert 'You are not verified. Please use `swanlab login` to login.' in result.output