Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions docs/usage/version.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ borg version

.. class:: borg-options-table

+-------------------------------------------------------+
| .. class:: borg-common-opt-ref |
| |
| :ref:`common_options` |
+-------------------------------------------------------+
+-------------------------------------------------------+------------+-----------------------+
| **options** |
+-------------------------------------------------------+------------+-----------------------+
| | ``--json`` | format output as JSON |
+-------------------------------------------------------+------------+-----------------------+
| .. class:: borg-common-opt-ref |
| |
| :ref:`common_options` |
+-------------------------------------------------------+------------+-----------------------+

.. raw:: html

Expand All @@ -30,6 +34,10 @@ borg version



options
--json format output as JSON


:ref:`common_options`
|

Expand Down
11 changes: 10 additions & 1 deletion src/borg/archiver/version_cmd.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .. import __version__
from ..constants import * # NOQA
from ..helpers import json_print
from ..helpers.argparsing import ArgumentParser

from ..logger import create_logger
Expand All @@ -20,7 +21,14 @@ def do_version(self, args):
server_version = repository.server_version
else:
server_version = client_version
print(f"{format_version(client_version)} / {format_version(server_version)}")

formatted_client_version = format_version(client_version)
formatted_server_version = format_version(server_version)
if args.json:
version_info = {"client": formatted_client_version, "server": formatted_server_version}
json_print(version_info)
else:
print(f"{formatted_client_version} / {formatted_server_version}")

def build_parser_version(self, subparsers, common_parser, mid_common_parser):
from ._common import process_epilog
Expand Down Expand Up @@ -53,3 +61,4 @@ def build_parser_version(self, subparsers, common_parser, mid_common_parser):
)
subparser = ArgumentParser(parents=[common_parser], description=self.do_version.__doc__, epilog=version_epilog)
subparsers.add_subcommand("version", subparser, help="display the Borg client and server versions")
subparser.add_argument("--json", action="store_true", help="format output as JSON")
21 changes: 21 additions & 0 deletions src/borg/testsuite/archiver/version_cmd_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import json

from ... import __version__
from ...version import format_version, parse_version
from . import cmd


EXPECTED_VERSION = format_version(parse_version(__version__))


def test_version(archiver):
output = cmd(archiver, "version")

assert output == f"{EXPECTED_VERSION} / {EXPECTED_VERSION}\n"


def test_version_json(archiver):
output = cmd(archiver, "version", "--json")
version_info = json.loads(output)

assert version_info == {"client": EXPECTED_VERSION, "server": EXPECTED_VERSION}
Loading