From ef5ce9f442a4922ec6f6724328c24d1f6ebfb701 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 21 Dec 2018 16:46:30 +0000 Subject: [PATCH] Add project status command --- catfish/project/__init__.py | 18 ++++++++++++++++++ catfish/project/cli.py | 21 +++++++++++++++++++++ catfish/utils/logfmt.py | 2 +- setup.py | 1 + 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/catfish/project/__init__.py b/catfish/project/__init__.py index 5e310e5..27582cb 100644 --- a/catfish/project/__init__.py +++ b/catfish/project/__init__.py @@ -4,6 +4,7 @@ from dataclasses import dataclass, field from pathlib import Path from typing import Dict, List +from click.termui import _ansi_colors as ansi_colors # type: ignore from dotenv import dotenv_values from catfish.utils.processes import get_running_process_for @@ -100,3 +101,20 @@ class Process: @property def is_running(self): return self.get_running_process() is not None + + @property + def pid(self): + proc = self.get_running_process() + if proc: + return proc.pid + + @property + def port(self): + proc = self.get_running_process() + if proc: + return proc.pid + + @property + def color(self): + proc_index = self.project.processes.index(self) + return list(ansi_colors.keys())[1:][proc_index % len(ansi_colors)] diff --git a/catfish/project/cli.py b/catfish/project/cli.py index f065268..2749035 100644 --- a/catfish/project/cli.py +++ b/catfish/project/cli.py @@ -3,6 +3,7 @@ import shlex import subprocess import click +from prettytable import PrettyTable from catfish.utils.sockets import socket_has_data @@ -32,3 +33,23 @@ def run(ctx, command): break click.echo(line, nl=False) return ctx.exit(proc.returncode) + + +@project.command() +@click.option("--running-only", is_flag=True, default=False) +def status(running_only): + project = Project(os.getcwd()) + table = PrettyTable(["ident", "pid", "port"]) + for process in project.processes: + if running_only and not process.is_running: + continue + port = process.port + pid = process.pid + table.add_row( + [ + click.style(process.ident, fg=process.color), + pid if pid else "", + port if port else "", + ] + ) + click.echo(table.get_string(sortby="ident")) diff --git a/catfish/utils/logfmt.py b/catfish/utils/logfmt.py index b5d7488..de353b8 100644 --- a/catfish/utils/logfmt.py +++ b/catfish/utils/logfmt.py @@ -11,7 +11,7 @@ def bool_to_str(i: bool) -> str: def escape_value(value: str): value = value.replace('"', '\\"') - if any([char in value for char in CHARS_TO_ESCAPE]): + if any(char in value for char in CHARS_TO_ESCAPE): value = '"{}"'.format(value) return value diff --git a/setup.py b/setup.py index d7b6838..81ebf59 100644 --- a/setup.py +++ b/setup.py @@ -20,6 +20,7 @@ setup( "pyzmq", "aiofiles", "python-dotenv", + "prettytable", ], entry_points=""" [console_scripts]