Clean arch
This commit is contained in:
62
redmine_reporter/cli.py
Normal file
62
redmine_reporter/cli.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import os
|
||||
import sys
|
||||
import argparse
|
||||
import yaml
|
||||
from pathlib import Path
|
||||
from .client import RedmineClient
|
||||
from .core import fetch_issues_for_period
|
||||
from .formatter import SimpleFormatter, TabulateFormatter
|
||||
|
||||
|
||||
def load_config(config_path: str):
|
||||
with open(config_path, encoding='utf-8') as f:
|
||||
return yaml.safe_load(f)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Redmine Reporter")
|
||||
parser.add_argument("--config", default="config.yaml", help="Path to config file")
|
||||
parser.add_argument("--from", dest="from_date", help="Start date (YYYY-MM-DD)")
|
||||
parser.add_argument("--to", dest="to_date", help="End date (YYYY-MM-DD)")
|
||||
parser.add_argument("--format", choices=["simple", "tabulate"], help="Output format")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
config_path = Path(args.config)
|
||||
if not config_path.exists():
|
||||
print(f"Config file not found: {config_path}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
cfg = load_config(config_path)
|
||||
redmine_cfg = cfg["redmine"]
|
||||
report_cfg = cfg.get("report", {})
|
||||
|
||||
# Определяем параметры: CLI имеет приоритет над конфигом
|
||||
from_date = args.from_date or report_cfg.get("date_from")
|
||||
to_date = args.to_date or report_cfg.get("date_to")
|
||||
output_format = args.format or report_cfg.get("output_format", "simple")
|
||||
|
||||
if not from_date or not to_date:
|
||||
print("Error: both --from and --to dates must be specified (via CLI or config).", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
password_env_var = redmine_cfg.get("password_env", "REDMINE_PASSWORD")
|
||||
password = os.getenv(password_env_var)
|
||||
if not password:
|
||||
print(f"Error: environment variable '{password_env_var}' is not set", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
client = RedmineClient(
|
||||
url=redmine_cfg["url"],
|
||||
username=redmine_cfg["username"],
|
||||
password=password
|
||||
)
|
||||
|
||||
records = fetch_issues_for_period(client, from_date, to_date)
|
||||
|
||||
if not records:
|
||||
print("No issues found for the given period.")
|
||||
sys.exit(0)
|
||||
|
||||
formatter = TabulateFormatter() if output_format == "tabulate" else SimpleFormatter()
|
||||
print(formatter.format(records))
|
||||
Reference in New Issue
Block a user