Skip to content

Commit 3a49ce2

Browse files
phlogistonjohnmergify[bot]
authored andcommitted
commands: add new generic --skip-if command line options
Previously, sambacc had the `--skip-if-file=` command line option that would skip running a command if a named file exists. I have a need for doing something similar for environment variable contents. In order to avoid needing to add a plethora of new --skip-if-x type options over time, add a more generic `--skip-if=` option and have the value contain an initial prefix like `file:` or `env:` that determines how the rest of the value will be parsed and how the skip will be determined (or not). Deprecate `--skip-if-file`. Maybe it's a case of over engineering, but at least it's easy to unit test. Examples: * `--skip-if=env:CHAT==yes`, if the environment variable `CHAT` contains a value equal to "yes" the command will be skipped * `--skip-if=file:/foo/bar` if the file `/foo/bar` exists the command will be skipped * `--skip-if=file:!/foo/bar` if the file `/foo/bar` does not exist the command will be skipped * `--skip-if=env:MODE!=777`, if the environment variable `MODE` contains a value not equal to "777" the command will be skipped * `--skip-if=always:`, always skip this command (mainly to be used for testing/hacking on things) Signed-off-by: John Mulligan <[email protected]>
1 parent 29f3807 commit 3a49ce2

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

sambacc/commands/dcmain.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
import typing
2020

2121
from . import addc
22+
from . import skips
2223
from .cli import Fail
2324
from .main import (
2425
CommandContext,
25-
action_filter,
2626
enable_logging,
2727
env_to_cli,
2828
global_args,
@@ -41,9 +41,10 @@ def main(args: typing.Optional[typing.Sequence[str]] = None) -> None:
4141
raise Fail("missing container identity")
4242

4343
pre_action(cli)
44-
skip = action_filter(cli)
44+
ctx = CommandContext(cli)
45+
skip = skips.test(ctx)
4546
if skip:
46-
print(f"Action skipped: {skip}")
47+
print(f"Command Skipped: {skip}")
4748
return
4849
cfunc = getattr(cli, "cfunc", default_cfunc)
4950
cfunc(CommandContext(cli))

sambacc/commands/main.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from . import initialize # noqa: F401
3737
from . import join # noqa: F401
3838
from . import run # noqa: F401
39+
from . import skips
3940
from . import users # noqa: F401
4041
from .cli import commands, Fail, Parser
4142

@@ -98,10 +99,23 @@ def global_args(parser: Parser) -> None:
9899
"--samba-command-prefix",
99100
help="Wrap samba commands within a supplied command prefix",
100101
)
102+
parser.add_argument(
103+
"--skip-if",
104+
dest="skip_conditions",
105+
action="append",
106+
type=skips.parse,
107+
help=(
108+
"Skip execution based on a condition. Conditions include"
109+
" 'file:<path>', 'env:<var>(==|!=)<value>', and 'always:'."
110+
" (Pass `?` for more details)"
111+
),
112+
)
101113
parser.add_argument(
102114
"--skip-if-file",
103115
action="append",
104-
help="Perform no action if the specified path exists.",
116+
dest="skip_conditions",
117+
type=skips.SkipFile.parse,
118+
help="(DEPRECATED) Perform no action if the specified path exists.",
105119
)
106120
parser.add_argument(
107121
"--validate-config",
@@ -309,13 +323,6 @@ def enable_logging(cli: typing.Any) -> None:
309323
logger.addHandler(handler)
310324

311325

312-
def action_filter(cli: typing.Any) -> typing.Optional[str]:
313-
for path in cli.skip_if_file or []:
314-
if os.path.exists(path):
315-
return f"skip-if-file: {path} exists"
316-
return None
317-
318-
319326
def main(args: typing.Optional[typing.Sequence[str]] = None) -> None:
320327
cli = commands.assemble(arg_func=global_args).parse_args(args)
321328
env_to_cli(cli)
@@ -324,12 +331,13 @@ def main(args: typing.Optional[typing.Sequence[str]] = None) -> None:
324331
raise Fail("missing container identity")
325332

326333
pre_action(cli)
327-
skip = action_filter(cli)
334+
ctx = CommandContext(cli)
335+
skip = skips.test(ctx)
328336
if skip:
329-
print(f"Action skipped: {skip}")
337+
print(f"Command Skipped: {skip}")
330338
return
331339
cfunc = getattr(cli, "cfunc", default_cfunc)
332-
cfunc(CommandContext(cli))
340+
cfunc(ctx)
333341
return
334342

335343

0 commit comments

Comments
 (0)