Add project status command

This commit is contained in:
Jake Howard 2018-12-21 16:46:30 +00:00
parent af45867376
commit ef5ce9f442
Signed by: jake
GPG key ID: 57AFB45680EDD477
4 changed files with 41 additions and 1 deletions

View file

@ -4,6 +4,7 @@ from dataclasses import dataclass, field
from pathlib import Path from pathlib import Path
from typing import Dict, List from typing import Dict, List
from click.termui import _ansi_colors as ansi_colors # type: ignore
from dotenv import dotenv_values from dotenv import dotenv_values
from catfish.utils.processes import get_running_process_for from catfish.utils.processes import get_running_process_for
@ -100,3 +101,20 @@ class Process:
@property @property
def is_running(self): def is_running(self):
return self.get_running_process() is not None 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)]

View file

@ -3,6 +3,7 @@ import shlex
import subprocess import subprocess
import click import click
from prettytable import PrettyTable
from catfish.utils.sockets import socket_has_data from catfish.utils.sockets import socket_has_data
@ -32,3 +33,23 @@ def run(ctx, command):
break break
click.echo(line, nl=False) click.echo(line, nl=False)
return ctx.exit(proc.returncode) 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"))

View file

@ -11,7 +11,7 @@ def bool_to_str(i: bool) -> str:
def escape_value(value: str): def escape_value(value: str):
value = value.replace('"', '\\"') 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) value = '"{}"'.format(value)
return value return value

View file

@ -20,6 +20,7 @@ setup(
"pyzmq", "pyzmq",
"aiofiles", "aiofiles",
"python-dotenv", "python-dotenv",
"prettytable",
], ],
entry_points=""" entry_points="""
[console_scripts] [console_scripts]