From 996b03794669b6a96ce4c60522ab856e6d65a713 Mon Sep 17 00:00:00 2001 From: Gautam Aggarwal Date: Wed, 26 Mar 2025 17:39:10 +0000 Subject: [PATCH 1/5] 1st --- borgmatic/commands/borgmatic.py | 19 +++++++++++++++++++ borgmatic/config/schema.yaml | 6 ++++++ 2 files changed, 25 insertions(+) diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index bd1ec1e6f..d8c270347 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -943,6 +943,22 @@ def exit_with_help_link(): # pragma: no cover sys.exit(1) +def check_and_show_help_on_no_args(): + """ + Check if the 'borgmatic' command is run without any arguments. If the configuration option + 'show_help_on_no_args' is set to True, show the help message. Otherwise, trigger the + default backup behavior. + """ + if len(sys.argv) == 1: # No arguments provided + show_help_on_no_args = any( + config.get('show_help_on_no_args', False) + for config in load_configurations(tuple(collect.collect_config_filenames(None)))[0].values() + ) + if show_help_on_no_args: + print(parse_arguments('--help')) + sys.exit(0) + + def main(extra_summary_logs=[]): # pragma: no cover configure_signals() configure_delayed_logging() @@ -971,6 +987,9 @@ def main(extra_summary_logs=[]): # pragma: no cover print(borgmatic.commands.completion.fish.fish_completion()) sys.exit(0) + # Use the helper function to check and show help on no arguments + check_and_show_help_on_no_args() + validate = bool('validate' in arguments) config_filenames = tuple(collect.collect_config_filenames(global_arguments.config_paths)) configs, config_paths, parse_logs = load_configurations( diff --git a/borgmatic/config/schema.yaml b/borgmatic/config/schema.yaml index ffbde0534..96bba3b91 100644 --- a/borgmatic/config/schema.yaml +++ b/borgmatic/config/schema.yaml @@ -2665,3 +2665,9 @@ properties: example: /usr/local/bin/keepassxc-cli description: | Configuration for integration with the KeePassXC password manager. + show_help_on_no_args: + type: boolean + description: | + If true, running borgmatic without any arguments will display the + help message instead of triggering a backup. Defaults to false. + example: true From 7a766c717e7a6237514d1016d4dd3706766d8e1f Mon Sep 17 00:00:00 2001 From: Gautam Aggarwal Date: Thu, 27 Mar 2025 02:55:16 +0000 Subject: [PATCH 2/5] 2nd Draft --- borgmatic/commands/borgmatic.py | 20 ++++++++++---------- borgmatic/config/schema.yaml | 8 +++++--- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/borgmatic/commands/borgmatic.py b/borgmatic/commands/borgmatic.py index d8c270347..0a6a775c2 100644 --- a/borgmatic/commands/borgmatic.py +++ b/borgmatic/commands/borgmatic.py @@ -943,19 +943,18 @@ def exit_with_help_link(): # pragma: no cover sys.exit(1) -def check_and_show_help_on_no_args(): +def check_and_show_help_on_no_args(configs): """ Check if the 'borgmatic' command is run without any arguments. If the configuration option - 'show_help_on_no_args' is set to True, show the help message. Otherwise, trigger the + 'default_actions' is set to False, show the help message. Otherwise, trigger the default backup behavior. """ if len(sys.argv) == 1: # No arguments provided - show_help_on_no_args = any( - config.get('show_help_on_no_args', False) - for config in load_configurations(tuple(collect.collect_config_filenames(None)))[0].values() + default_actions = any( + config.get('default_actions', True) for config in configs.values() ) - if show_help_on_no_args: - print(parse_arguments('--help')) + if not default_actions: + parse_arguments('--help') sys.exit(0) @@ -987,9 +986,6 @@ def main(extra_summary_logs=[]): # pragma: no cover print(borgmatic.commands.completion.fish.fish_completion()) sys.exit(0) - # Use the helper function to check and show help on no arguments - check_and_show_help_on_no_args() - validate = bool('validate' in arguments) config_filenames = tuple(collect.collect_config_filenames(global_arguments.config_paths)) configs, config_paths, parse_logs = load_configurations( @@ -997,6 +993,10 @@ def main(extra_summary_logs=[]): # pragma: no cover global_arguments.overrides, resolve_env=global_arguments.resolve_env and not validate, ) + + # Use the helper function to check and show help on no arguments, passing the preloaded configs + check_and_show_help_on_no_args(configs) + configuration_parse_errors = ( (max(log.levelno for log in parse_logs) >= logging.CRITICAL) if parse_logs else False ) diff --git a/borgmatic/config/schema.yaml b/borgmatic/config/schema.yaml index 96bba3b91..65ba9f773 100644 --- a/borgmatic/config/schema.yaml +++ b/borgmatic/config/schema.yaml @@ -2665,9 +2665,11 @@ properties: example: /usr/local/bin/keepassxc-cli description: | Configuration for integration with the KeePassXC password manager. - show_help_on_no_args: + default_actions: type: boolean description: | - If true, running borgmatic without any arguments will display the - help message instead of triggering a backup. Defaults to false. + Whether to apply default actions (e.g., backup) when no arguments + are supplied to the borgmatic command. If set to true, borgmatic + will trigger the default actions. If set to false, borgmatic will + display the help message instead. example: true From 088da190129786a0a092be11086ee43326ff4601 Mon Sep 17 00:00:00 2001 From: Gautam Aggarwal Date: Thu, 27 Mar 2025 11:26:56 +0000 Subject: [PATCH 3/5] Added Unit Tests --- borgmatic/config/schema.yaml | 8 ++++++-- tests/unit/commands/test_borgmatic.py | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/borgmatic/config/schema.yaml b/borgmatic/config/schema.yaml index 65ba9f773..7a4c4a28f 100644 --- a/borgmatic/config/schema.yaml +++ b/borgmatic/config/schema.yaml @@ -2670,6 +2670,10 @@ properties: description: | Whether to apply default actions (e.g., backup) when no arguments are supplied to the borgmatic command. If set to true, borgmatic - will trigger the default actions. If set to false, borgmatic will - display the help message instead. + will trigger the default actions,which include : + -Create backups based on your configured schedules and paths. + - Prune old backups based on your retention policies. + - Verify the integrity of your backups. + + If set to false, borgmatic will display the help message instead. example: true diff --git a/tests/unit/commands/test_borgmatic.py b/tests/unit/commands/test_borgmatic.py index 160f44543..15512a678 100644 --- a/tests/unit/commands/test_borgmatic.py +++ b/tests/unit/commands/test_borgmatic.py @@ -2079,3 +2079,23 @@ def test_collect_configuration_run_summary_logs_outputs_merged_json_results(): arguments=arguments, ) ) +def test_check_and_show_help_on_no_args_shows_help_when_no_args_and_default_actions_false(): + flexmock(module.sys).should_receive('argv').and_return(['borgmatic']) + flexmock(module).should_receive('parse_arguments').with_args('--help').once() + flexmock(module.sys).should_receive('exit').with_args(0).once() + module.check_and_show_help_on_no_args({'test.yaml': {'default_actions': False}}) + + +def test_check_and_show_help_on_no_args_does_not_show_help_when_no_args_and_default_actions_true(): + flexmock(module.sys).should_receive('argv').and_return(['borgmatic']) + flexmock(module).should_receive('parse_arguments').never() + flexmock(module.sys).should_receive('exit').never() + module.check_and_show_help_on_no_args({'test.yaml': {'default_actions': True}}) + + +def test_check_and_show_help_on_no_args_does_not_show_help_when_args_provided(): + flexmock(module.sys).should_receive('argv').and_return(['borgmatic', '--create']) + flexmock(module).should_receive('parse_arguments').never() + flexmock(module.sys).should_receive('exit').never() + module.check_and_show_help_on_no_args({'test.yaml': {'default_actions': False}}) + From 0182dbd914edce4b577230929677803621af5ee6 Mon Sep 17 00:00:00 2001 From: Gautam Aggarwal Date: Sat, 29 Mar 2025 03:43:58 +0000 Subject: [PATCH 4/5] Added 2 new unit tests and updated docs --- docs/how-to/set-up-backups.md | 10 +++++++++ tests/unit/commands/test_borgmatic.py | 30 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/docs/how-to/set-up-backups.md b/docs/how-to/set-up-backups.md index c90b9136e..d63368f37 100644 --- a/docs/how-to/set-up-backups.md +++ b/docs/how-to/set-up-backups.md @@ -296,6 +296,16 @@ skip_actions: - compact ``` +## Disabling default actions + +By default, running `borgmatic` without any arguments will perform the default backup actions (create, prune, and compact). If you want to disable this behavior and require explicit actions to be specified, add the following to your configuration: + +```yaml +default_actions: false +``` + +With this setting, running `borgmatic` without arguments will show the help message instead of performing any actions. + ## Autopilot diff --git a/tests/unit/commands/test_borgmatic.py b/tests/unit/commands/test_borgmatic.py index 15512a678..27e91cf5b 100644 --- a/tests/unit/commands/test_borgmatic.py +++ b/tests/unit/commands/test_borgmatic.py @@ -2099,3 +2099,33 @@ def test_check_and_show_help_on_no_args_does_not_show_help_when_args_provided(): flexmock(module.sys).should_receive('exit').never() module.check_and_show_help_on_no_args({'test.yaml': {'default_actions': False}}) +def test_check_and_show_help_on_no_args_with_no_default_actions_in_all_configs(): + flexmock(module.sys).should_receive('argv').and_return(['borgmatic']) + + # Both configs have default_actions set to False, so help should be shown + configs = { + 'config1.yaml': {'default_actions': False}, + 'config2.yaml': {'default_actions': False} + } + + # Expect help to be shown + flexmock(module).should_receive('parse_arguments').with_args('--help').once() + flexmock(module.sys).should_receive('exit').with_args(0).once() + + module.check_and_show_help_on_no_args(configs) + +def test_check_and_show_help_on_no_args_with_conflicting_configs(): + flexmock(module.sys).should_receive('argv').and_return(['borgmatic']) + + # Simulate two config files with conflicting 'default_actions' values + configs = { + 'config1.yaml': {'default_actions': True}, + 'config2.yaml': {'default_actions': False} + } + + # Expect help not to be shown because at least one config enables default actions + flexmock(module).should_receive('parse_arguments').never() + flexmock(module.sys).should_receive('exit').never() + + module.check_and_show_help_on_no_args(configs) + From 36265eea7dab22dd6e9fb081d49d4f7b3f78bf3b Mon Sep 17 00:00:00 2001 From: Gautam Aggarwal Date: Sun, 30 Mar 2025 01:34:30 +0000 Subject: [PATCH 5/5] Docs update --- borgmatic/config/schema.yaml | 6 +----- docs/how-to/set-up-backups.md | 10 +++++++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/borgmatic/config/schema.yaml b/borgmatic/config/schema.yaml index 7a4c4a28f..c3f8e1a98 100644 --- a/borgmatic/config/schema.yaml +++ b/borgmatic/config/schema.yaml @@ -2670,10 +2670,6 @@ properties: description: | Whether to apply default actions (e.g., backup) when no arguments are supplied to the borgmatic command. If set to true, borgmatic - will trigger the default actions,which include : - -Create backups based on your configured schedules and paths. - - Prune old backups based on your retention policies. - - Verify the integrity of your backups. - + will trigger the default actions(create, prune, compact and check). If set to false, borgmatic will display the help message instead. example: true diff --git a/docs/how-to/set-up-backups.md b/docs/how-to/set-up-backups.md index d63368f37..5871bd6bd 100644 --- a/docs/how-to/set-up-backups.md +++ b/docs/how-to/set-up-backups.md @@ -296,15 +296,19 @@ skip_actions: - compact ``` -## Disabling default actions +### Disabling default actions -By default, running `borgmatic` without any arguments will perform the default backup actions (create, prune, and compact). If you want to disable this behavior and require explicit actions to be specified, add the following to your configuration: +By default, running `borgmatic` without any arguments will perform the default +backup actions (create, prune, compact and check). If you want to disable this +behavior and require explicit actions to be specified, add the following to +your configuration: ```yaml default_actions: false ``` -With this setting, running `borgmatic` without arguments will show the help message instead of performing any actions. +With this setting, running `borgmatic` without arguments will show the help +message instead of performing any actions. ## Autopilot