Skip to content

Commit

Permalink
add debug mode for start and stop
Browse files Browse the repository at this point in the history
  • Loading branch information
hubertdeng123 committed Nov 11, 2024
1 parent f2cad41 commit b92d660
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 0 deletions.
12 changes: 12 additions & 0 deletions devservices/commands/start.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations

import logging
from argparse import _SubParsersAction
from argparse import ArgumentParser
from argparse import Namespace

from devservices.constants import LOGGER_NAME
from devservices.exceptions import DependencyError
from devservices.exceptions import DockerComposeError
from devservices.utils.console import Console
Expand All @@ -18,6 +20,12 @@ def add_parser(subparsers: _SubParsersAction[ArgumentParser]) -> None:
parser.add_argument(
"service_name", help="Name of the service to start", nargs="?", default=None
)
parser.add_argument(
"--debug",
help="Enable debug mode",
action="store_true",
default=False,
)
parser.set_defaults(func=start)


Expand All @@ -36,6 +44,10 @@ def start(args: Namespace) -> None:
mode_to_start = "default"
mode_dependencies = modes[mode_to_start]

if args.debug:
logger = logging.getLogger(LOGGER_NAME)
logger.setLevel(logging.DEBUG)

with Status(
lambda: console.warning(f"Starting {service.name}"),
lambda: console.success(f"{service.name} started"),
Expand Down
13 changes: 13 additions & 0 deletions devservices/commands/stop.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from __future__ import annotations

import logging
from argparse import _SubParsersAction
from argparse import ArgumentParser
from argparse import Namespace

from devservices.constants import LOGGER_NAME
from devservices.exceptions import DependencyError
from devservices.exceptions import DockerComposeError
from devservices.utils.console import Console
Expand All @@ -18,6 +20,12 @@ def add_parser(subparsers: _SubParsersAction[ArgumentParser]) -> None:
parser.add_argument(
"service_name", help="Name of the service to stop", nargs="?", default=None
)
parser.add_argument(
"--debug",
help="Enable debug mode",
action="store_true",
default=False,
)
parser.set_defaults(func=stop)


Expand All @@ -35,6 +43,11 @@ def stop(args: Namespace) -> None:
# TODO: allow custom modes to be used
mode_to_stop = "default"
mode_dependencies = modes[mode_to_stop]

if args.debug:
logger = logging.getLogger(LOGGER_NAME)
logger.setLevel(logging.DEBUG)

state = State()
started_services = state.get_started_services()
if service.name not in started_services:
Expand Down
1 change: 1 addition & 0 deletions devservices/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
DEVSERVICES_DOWNLOAD_URL = "https://github.com/getsentry/devservices/releases/download"
BINARY_PERMISSIONS = 0o755
MAX_LOG_LINES = "100"
LOGGER_NAME = "devservices"
2 changes: 2 additions & 0 deletions devservices/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import argparse
import atexit
import logging
import os
from importlib import metadata

Expand All @@ -27,6 +28,7 @@
)

disable_sentry = os.environ.get("DISABLE_SENTRY", default=False)
logging.basicConfig(level=logging.INFO)

if not disable_sentry:
sentry_sdk.init(
Expand Down
4 changes: 4 additions & 0 deletions devservices/utils/dependencies.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import logging
import os
import shutil
import subprocess
Expand All @@ -18,6 +19,7 @@
from devservices.constants import DEPENDENCY_GIT_PARTIAL_CLONE_CONFIG_OPTIONS
from devservices.constants import DEVSERVICES_DEPENDENCIES_CACHE_DIR
from devservices.constants import DEVSERVICES_DIR_NAME
from devservices.constants import LOGGER_NAME
from devservices.exceptions import ConfigNotFoundError
from devservices.exceptions import ConfigParseError
from devservices.exceptions import ConfigValidationError
Expand Down Expand Up @@ -436,4 +438,6 @@ def _has_remote_config(remote_config: RemoteConfig | None) -> TypeGuard[RemoteCo
def _run_command(
cmd: list[str], cwd: str, stdout: int | TextIO | None = subprocess.DEVNULL
) -> None:
logger = logging.getLogger(LOGGER_NAME)
logger.debug(f"Running command: {' '.join(cmd)}")
subprocess.run(cmd, cwd=cwd, check=True, stdout=stdout, stderr=subprocess.DEVNULL)
4 changes: 4 additions & 0 deletions devservices/utils/docker_compose.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import logging
import os
import platform
import re
Expand All @@ -17,6 +18,7 @@
from devservices.constants import DEVSERVICES_DIR_NAME
from devservices.constants import DOCKER_COMPOSE_DOWNLOAD_URL
from devservices.constants import DOCKER_USER_PLUGIN_DIR
from devservices.constants import LOGGER_NAME
from devservices.constants import MINIMUM_DOCKER_COMPOSE_VERSION
from devservices.exceptions import BinaryInstallError
from devservices.exceptions import DockerComposeError
Expand Down Expand Up @@ -272,6 +274,8 @@ def run_docker_compose_command(
cmd_outputs = []
for cmd in docker_compose_commands:
try:
logger = logging.getLogger(LOGGER_NAME)
logger.debug(f"Running command: {' '.join(cmd)}")
cmd_outputs.append(
subprocess.run(
cmd, check=True, capture_output=True, text=True, env=current_env
Expand Down

0 comments on commit b92d660

Please sign in to comment.