diff --git a/aiida/cmdline/commands/cmd_daemon.py b/aiida/cmdline/commands/cmd_daemon.py index 857c697e2a..1a68efe7e4 100644 --- a/aiida/cmdline/commands/cmd_daemon.py +++ b/aiida/cmdline/commands/cmd_daemon.py @@ -75,6 +75,7 @@ def start(foreground, number): @verdi_daemon.command() @click.option('--all', 'all_profiles', is_flag=True, help='Show status of all daemons.') +@decorators.requires_loaded_profile() def status(all_profiles): """Print the status of the current daemon or all daemons. diff --git a/aiida/cmdline/utils/decorators.py b/aiida/cmdline/utils/decorators.py index 3146bf7b2e..5e0cc159e4 100644 --- a/aiida/cmdline/utils/decorators.py +++ b/aiida/cmdline/utils/decorators.py @@ -236,3 +236,29 @@ def wrapper(wrapped, _, args, kwargs): return wrapped(*args, **kwargs) return wrapper + + +def requires_loaded_profile(): + """Function decorator for CLI command that requires a profile to be loaded. + + Example:: + + @requires_loaded_profile() + def create_node(): + pass + + If no profile has been loaded, the command will exit with a critical error. Most ``verdi`` commands will + automatically load the default profile. So if this error is hit, it is most likely that either no profile have been + defined at all or the default is unspecified. + """ + + @decorator + def wrapper(wrapped, _, args, kwargs): + from aiida.manage.configuration import get_profile + + if get_profile() is None: + echo.echo_critical('No profile loaded: make sure at least one profile is configured and a default is set.') + + return wrapped(*args, **kwargs) + + return wrapper diff --git a/tests/cmdline/commands/test_daemon.py b/tests/cmdline/commands/test_daemon.py index ea007faf99..9ef7968e2f 100644 --- a/tests/cmdline/commands/test_daemon.py +++ b/tests/cmdline/commands/test_daemon.py @@ -90,3 +90,10 @@ def test_daemon_status(run_cli_command): assert f'Profile: {get_profile().name}' in result.output assert last_line == 'Use verdi daemon [incr | decr] [num] to increase / decrease the amount of workers' + + +@pytest.mark.usefixtures('empty_config') +def test_daemon_status_no_profile(run_cli_command): + """Test ``verdi daemon status`` when no profiles are defined.""" + result = run_cli_command(cmd_daemon.status, raises=True) + assert 'No profile loaded: make sure at least one profile is configured and a default is set.' in result.output