code: def main() -> None:
      # Check environment and versions if not in the cloud and not testing
      is_testing = bool(int(os.getenv("LIGHTING_TESTING", "0")))
      if not is_testing and "LIGHTNING_APP_STATE_URL" not in os.environ:
          try:
              # Enforce running in PATH Python
              _check_environment_and_redirect()
  
              # Check for newer versions and upgrade
              _check_version_and_upgrade()
          except SystemExit:
              raise
          except Exception:
              # Note: We intentionally ignore all exceptions here so that we never panic if one of the above calls fails.
              # If they fail for some reason users should still be able to continue with their command.
              click.echo(
                  "We encountered an unexpected problem while checking your environment."
                  "We will still proceed with the command, however, there is a chance that errors may occur."
              )
  
      # 1: Handle connection to a Lightning App.
      if len(sys.argv) > 1 and sys.argv[1] in (
          "connect",
          "disconnect",
          "logout",
      ):
          _main()
      else:
          # 2: Collect the connection a Lightning App.
          app_name, app_id = _retrieve_connection_to_an_app()
          if app_name:
              # 3: Handle development use case.
              is_local_app = app_name == "localhost"
              if sys.argv[1:3] == ["run", "app"] or (
                  sys.argv[1:3] == ["show", "logs"]
                  and "show logs" not in _list_app_commands(False)
              ):
                  _main()
              else:
                  if is_local_app:
                      message = "You are connected to the local Lightning App."
                  else:
                      message = f"You are connected to the cloud Lightning App: {app_name}."
  
                  if (
                      len(sys.argv) > 1
                      and sys.argv[1] in ["-h", "--help"]
                  ) or len(sys.argv) == 1:
                      _list_app_commands()
                  else:
                      _run_app_command(app_name, app_id)
  
                  click.echo()
                  click.echo(
                      message
                      + " Return to the primary CLI with `lightning disconnect`."
                  )
          else:
              _main()
  
imports: import os
  import sys
  from pathlib import Path
  from typing import Tuple, Union
  import click
  from lightning_utilities.core.imports import RequirementCache
  from requests.exceptions import ConnectionError
  import lightning.app.core.constants
  from lightning.app import __version__
  from lightning.app.cli import cmd_init, cmd_install, cmd_pl_init, cmd_react_ui_init
  from lightning.app.cli.commands.app_commands import _run_app_command
  from lightning.app.cli.commands.cd import cd
  from lightning.app.cli.commands.cp import cp
  from lightning.app.cli.commands.logs import logs
  from lightning.app.cli.commands.ls import ls
  from lightning.app.cli.commands.pwd import pwd
  from lightning.app.cli.commands.rm import rm
  from lightning.app.cli.connect.app import _list_app_commands, _retrieve_connection_to_an_app, connect_app, disconnect_app
  from lightning.app.cli.connect.data import connect_data
  from lightning.app.cli.lightning_cli_delete import delete
  from lightning.app.cli.lightning_cli_launch import launch
  from lightning.app.cli.lightning_cli_list import get_list
  from lightning.app.core.constants import ENABLE_APP_COMMENT_COMMAND_EXECUTION, get_lightning_cloud_url
  from lightning.app.runners.cloud import CloudRuntime
  from lightning.app.runners.runtime import dispatch
  from lightning.app.runners.runtime_type import RuntimeType
  from lightning.app.utilities.app_commands import run_app_commands
  from lightning.app.utilities.app_helpers import Logger
  from lightning.app.utilities.cli_helpers import _check_environment_and_redirect, _check_version_and_upgrade, _format_input_env_variables
  from lightning.app.utilities.exceptions import _ApiExceptionHandler
  from lightning.app.utilities.login import Auth
  from lightning.app.utilities.port import _find_lit_app_port
label_desc: Use image node_id 1 for calling a global function with example usage: main() without return types