import os
from klon_core.ping import ping as do_ping, PingRunner
from klon_core.data import NodeDatabase
from .connect import require_init


def ping(db_path=os.getcwd()):
    require_init(db_path)
    do_ping(node_db=NodeDatabase.open(db_path), runner=CliPingRunner())


class CliPingRunner(PingRunner):
    def on_request_error(self, resp):
        print("there was an error communicating with the hub")  # TODO

    def on_exit(self, task_result):
        if task_result:
            print("Hub is up.")
        else:
            task_result.print_stack_trace()
