Skip to content

Commit

Permalink
feat(cli): --version option for server and client (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
ketiltrout authored Nov 1, 2024
1 parent 32d16b6 commit dbb47da
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
3 changes: 2 additions & 1 deletion alpenhorn/client/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import peewee as pw

from .. import db
from ..common.util import start_alpenhorn
from ..common.util import start_alpenhorn, version_option
from ..db import (
ArchiveAcq,
ArchiveFile,
Expand Down Expand Up @@ -46,6 +46,7 @@ def _verbosity_from_cli(verbose: int, debug: int, quiet: int) -> int:


@click.group(context_settings={"help_option_names": ["-h", "--help"]})
@version_option
@click.option(
"--conf",
"-c",
Expand Down
47 changes: 47 additions & 0 deletions alpenhorn/common/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import click
import socket
import hashlib
import logging
Expand All @@ -11,6 +12,52 @@
log = logging.getLogger(__name__)


def version_option(func):
"""Click --version option"""
return click.option(
"--version",
is_flag=True,
callback=print_version,
expose_value=False,
is_eager=True,
help="Show version information and exit.",
)(func)


def print_version(ctx, param, value):
"""Click callback for the --version eager option."""

from .. import __version__
import sys

if not value or ctx.resilient_parsing:
return

click.echo(f"alpenhorn {__version__} (Python {sys.version})")
click.echo(
"""
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
)
ctx.exit(0)


def start_alpenhorn(
cli_conf: str | None, client: bool, verbosity: int | None = None
) -> None:
Expand Down
3 changes: 2 additions & 1 deletion alpenhorn/server/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from .. import db
from ..common import config
from ..common.util import start_alpenhorn
from ..common.util import start_alpenhorn, version_option
from ..scheduler import FairMultiFIFOQueue, pool
from . import auto_import, update

Expand All @@ -32,6 +32,7 @@ def log_exception(*args):
default=None,
metavar="FILE",
)
@version_option
def cli(conf):
"""Alpenhorn data management service."""

Expand Down

0 comments on commit dbb47da

Please sign in to comment.