code: def ls(
      path: Optional[str] = None,
      print: bool = True,
      use_live: bool = True,
  ) -> List[str]:
      """List the contents of a folder in the Lightning Cloud Filesystem."""
      from lightning.app.cli.commands.cd import _CD_FILE
  
      if sys.platform == "win32":
          _error_and_exit(
              "`ls` isn't supported on windows. Open an issue on Github."
          )
  
      root = "/"
  
      context = (
          Live(
              Spinner("point", text=Text("pending...", style="white")),
              transient=True,
          )
          if use_live
          else nullcontext()
      )
  
      with context:
          if not os.path.exists(_LIGHTNING_CONNECTION_FOLDER):
              os.makedirs(_LIGHTNING_CONNECTION_FOLDER)
  
          if not os.path.exists(_CD_FILE):
              with open(_CD_FILE, "w") as f:
                  f.write(root + "\n")
          else:
              with open(_CD_FILE) as f:
                  lines = f.readlines()
                  root = lines[0].replace("\n", "")
  
          client = LightningClient(retry=False)
          projects = client.projects_service_list_memberships()
  
          if root == "/":
              project_names = [
                  project.name for project in projects.memberships
              ]
              if print:
                  _print_names_with_colors(
                      project_names,
                      [_FOLDER_COLOR] * len(project_names),
                  )
              return project_names
  
          # Note: Root format has the following structure:
          # /{PROJECT_NAME}/{APP_NAME}/{ARTIFACTS_PATHS}
          splits = root.split("/")[1:]
  
          project = [
              project
              for project in projects.memberships
              if project.name == splits[0]
          ]
  
          # This happens if the user changes cluster and the project doesn't exit.
          if len(project) == 0:
              return _error_and_exit(
                  f"There isn't any Lightning Project matching the name {splits[0]}."
                  " HINT: Use `lightning cd`."
              )
  
          project_id = project[0].project_id
  
          # Parallelise calls
          lit_apps = client.lightningapp_instance_service_list_lightningapp_instances(
              project_id=project_id, async_req=True
          )
          lit_cloud_spaces = (
              client.cloud_space_service_list_cloud_spaces(
                  project_id=project_id, async_req=True
              )
          )
  
          lit_apps = lit_apps.get().lightningapps
          lit_cloud_spaces = lit_cloud_spaces.get().cloudspaces
  
          if len(splits) == 1:
              apps = [lit_app.name for lit_app in lit_apps]
              cloud_spaces = [
                  lit_cloud_space.name
                  for lit_cloud_space in lit_cloud_spaces
              ]
              ressource_names = sorted(set(cloud_spaces + apps))
              if print:
                  _print_names_with_colors(
                      ressource_names,
                      [_FOLDER_COLOR] * len(ressource_names),
                  )
              return ressource_names
  
          lit_ressources = [
              lit_resource
              for lit_resource in lit_cloud_spaces
              if lit_resource.name == splits[1]
          ]
  
          if len(lit_ressources) == 0:
              lit_ressources = [
                  lit_resource
                  for lit_resource in lit_apps
                  if lit_resource.name == splits[1]
              ]
  
              if len(lit_ressources) == 0:
                  _error_and_exit(
                      f"There isn't any Lightning Ressource matching the name {splits[1]}."
                  )
  
          lit_resource = lit_ressources[0]
  
          app_paths = []
          app_colors = []
  
          cloud_spaces_paths = []
          cloud_spaces_colors = []
  
          depth = len(splits)
  
          prefix = "/".join(splits[2:])
          prefix = _get_prefix(prefix, lit_resource)
  
          for artifact in _collect_artifacts(
              client=client, project_id=project_id, prefix=prefix
          ):
              if str(artifact.filename).startswith("/"):
                  artifact.filename = artifact.filename[1:]
  
              path = os.path.join(
                  project_id, prefix[1:], artifact.filename
              )
  
              artifact_splits = path.split("/")
  
              if len(artifact_splits) <= depth + 1:
                  continue
  
              path = artifact_splits[depth + 1]
  
              paths = (
                  app_paths
                  if isinstance(
                      lit_resource, Externalv1LightningappInstance
                  )
                  else cloud_spaces_paths
              )
              colors = (
                  app_colors
                  if isinstance(
                      lit_resource, Externalv1LightningappInstance
                  )
                  else cloud_spaces_colors
              )
  
              if path not in paths:
                  paths.append(path)
  
                  # display files otherwise folders
                  colors.append(
                      _FILE_COLOR
                      if len(artifact_splits) == depth + 1
                      else _FOLDER_COLOR
                  )
  
      if print:
          if app_paths and cloud_spaces_paths:
              if app_paths:
                  rich.print("Lightning App")
                  _print_names_with_colors(app_paths, app_colors)
  
              if cloud_spaces_paths:
                  rich.print("Lightning CloudSpaces")
                  _print_names_with_colors(
                      cloud_spaces_paths, cloud_spaces_colors
                  )
          else:
              _print_names_with_colors(
                  app_paths + cloud_spaces_paths,
                  app_colors + cloud_spaces_colors,
              )
  
      return app_paths + cloud_spaces_paths
  
imports: import contextlib
  import os
  import sys
  from contextlib import nullcontext
  from typing import Generator, List, Optional
  import click
  import lightning_cloud
  import rich
  from lightning_cloud.openapi import Externalv1LightningappInstance
  from rich.console import Console
  from rich.live import Live
  from rich.spinner import Spinner
  from rich.text import Text
  from lightning.app.cli.connect.app import _LIGHTNING_CONNECTION_FOLDER
  from lightning.app.utilities.app_helpers import Logger
  from lightning.app.utilities.cli_helpers import _error_and_exit
  from lightning.app.utilities.network import LightningClient
label_desc: Use image node_id 1 for calling a global function with example usage: ls(path, print, use_live) and returns: unknown, project_names, _error_and_exit, ressource_names