feat: replace credential options with --server config option
- Replace --url, --username, --password CLI options with single --server option - Server credentials are now read from config file (~/.config/zzpyjenkins/config.toml) - Remove os and typing imports (no longer needed) - Update type hints to use modern str | None syntax (Python 3.11+) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,55 +1,34 @@
|
|||||||
"""Command-line interface for zzpyjenkins."""
|
"""Command-line interface for zzpyjenkins."""
|
||||||
|
|
||||||
import os
|
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
import click
|
import click
|
||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
|
|
||||||
|
from .config import ConfigError, get_default_config_path, get_server_config, load_config
|
||||||
from .jenkins_client import JenkinsClient
|
from .jenkins_client import JenkinsClient
|
||||||
|
|
||||||
console = Console()
|
console = Console()
|
||||||
|
|
||||||
|
|
||||||
@click.group()
|
@click.group()
|
||||||
@click.option("--url", "-u", help="Jenkins server URL (default: JENKINS_URL env)")
|
@click.option("--server", "-s", required=True, help="Server name from config file")
|
||||||
@click.option(
|
|
||||||
"--username", "-U", help="Jenkins username (default: JENKINS_USERNAME env)"
|
|
||||||
)
|
|
||||||
@click.option(
|
|
||||||
"--password", "-P", help="Jenkins password/token (default: JENKINS_PASSWORD env)"
|
|
||||||
)
|
|
||||||
@click.version_option()
|
@click.version_option()
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
def main(
|
def main(ctx: click.Context, server: str):
|
||||||
ctx: click.Context,
|
|
||||||
url: Optional[str],
|
|
||||||
username: Optional[str],
|
|
||||||
password: Optional[str],
|
|
||||||
):
|
|
||||||
"""A CLI tool to interact with Jenkins server for building jobs."""
|
"""A CLI tool to interact with Jenkins server for building jobs."""
|
||||||
ctx.ensure_object(dict)
|
ctx.ensure_object(dict)
|
||||||
ctx.obj["url"] = url
|
ctx.obj["server"] = server
|
||||||
ctx.obj["username"] = username
|
|
||||||
ctx.obj["password"] = password
|
|
||||||
|
|
||||||
|
|
||||||
def get_client(ctx: click.Context) -> JenkinsClient:
|
def get_client(ctx: click.Context) -> JenkinsClient:
|
||||||
"""Create Jenkins client from options or environment variables."""
|
"""Create Jenkins client from config file."""
|
||||||
url = ctx.obj.get("url") or os.environ.get("JENKINS_URL")
|
server_name = ctx.obj["server"]
|
||||||
username = ctx.obj.get("username") or os.environ.get("JENKINS_USERNAME")
|
config_path = get_default_config_path()
|
||||||
password = (
|
|
||||||
ctx.obj.get("password")
|
|
||||||
or os.environ.get("JENKINS_PASSWORD")
|
|
||||||
or os.environ.get("JENKINS_TOKEN")
|
|
||||||
)
|
|
||||||
|
|
||||||
if not all([url, username, password]):
|
try:
|
||||||
console.print("[red]Error: Missing Jenkins configuration.[/red]")
|
config = load_config(config_path)
|
||||||
console.print(
|
url, username, password = get_server_config(config, server_name)
|
||||||
"Use --url/--username/--password options or set environment variables:"
|
except ConfigError as e:
|
||||||
)
|
console.print(f"[red]Error: {e}[/red]")
|
||||||
console.print(" JENKINS_URL, JENKINS_USERNAME, JENKINS_PASSWORD")
|
|
||||||
raise SystemExit(1)
|
raise SystemExit(1)
|
||||||
|
|
||||||
return JenkinsClient(url, username, password)
|
return JenkinsClient(url, username, password)
|
||||||
@@ -66,7 +45,7 @@ def info(ctx: click.Context):
|
|||||||
@main.command("list")
|
@main.command("list")
|
||||||
@click.option("--pattern", "-p", default=None, help="Filter jobs by name pattern")
|
@click.option("--pattern", "-p", default=None, help="Filter jobs by name pattern")
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
def list_jobs(ctx: click.Context, pattern: Optional[str]):
|
def list_jobs(ctx: click.Context, pattern: str | None):
|
||||||
"""List all jobs on Jenkins server."""
|
"""List all jobs on Jenkins server."""
|
||||||
client = get_client(ctx)
|
client = get_client(ctx)
|
||||||
client.list_jobs(pattern)
|
client.list_jobs(pattern)
|
||||||
@@ -96,7 +75,7 @@ def build(ctx: click.Context, job_name: str, params: tuple[str, ...]):
|
|||||||
@click.argument("job_name")
|
@click.argument("job_name")
|
||||||
@click.argument("build_number", type=int, required=False)
|
@click.argument("build_number", type=int, required=False)
|
||||||
@click.pass_context
|
@click.pass_context
|
||||||
def status(ctx: click.Context, job_name: str, build_number: Optional[int]):
|
def status(ctx: click.Context, job_name: str, build_number: int | None):
|
||||||
"""Get build status for a job."""
|
"""Get build status for a job."""
|
||||||
client = get_client(ctx)
|
client = get_client(ctx)
|
||||||
client.get_build_status(job_name, build_number)
|
client.get_build_status(job_name, build_number)
|
||||||
|
|||||||
Reference in New Issue
Block a user