Skip to content

Commit fa577fd

Browse files
Added index to the cfengine add command & fixed a faulty call to it
Ticket: None Changelog: Title Signed-off-by: Simon Halvorsen <simon.halvorsen@northern.tech>
1 parent 4ee9538 commit fa577fd

3 files changed

Lines changed: 52 additions & 13 deletions

File tree

‎src/cfengine_cli/cfengine_wrapper/arg_parse.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
)
1212

1313

14+
def _add_index_arg(parser: argparse.ArgumentParser):
15+
parser.add_argument(
16+
"--index",
17+
help="Specify alternate index (HTTPS URL or relative path to JSON file)",
18+
type=str,
19+
default=None,
20+
)
21+
22+
1423
def parse_wrapper_args(subp: argparse._SubParsersAction):
1524
update_parser = subp.add_parser(
1625
"update",
@@ -20,7 +29,7 @@ def parse_wrapper_args(subp: argparse._SubParsersAction):
2029
update_parser.add_argument(
2130
"to_update",
2231
nargs="*",
23-
help="Directory of cfbs-project to update",
32+
help="Module(s) to update (all modules if omitted)",
2433
)
2534
remove_parser = subp.add_parser(
2635
"remove",
@@ -43,6 +52,7 @@ def parse_wrapper_args(subp: argparse._SubParsersAction):
4352
nargs="+",
4453
help="Module(s) for which to add",
4554
)
55+
_add_index_arg(add_parser)
4656
search_parser = subp.add_parser(
4757
"search",
4858
help="Searches the build-index for specified module(s)",
@@ -53,6 +63,7 @@ def parse_wrapper_args(subp: argparse._SubParsersAction):
5363
nargs="+",
5464
help="Module(s) for which to lookup",
5565
)
66+
_add_index_arg(search_parser)
5667

5768
input_parser = subp.add_parser(
5869
"input",
@@ -84,6 +95,7 @@ def parse_wrapper_args(subp: argparse._SubParsersAction):
8495
nargs="*",
8596
help="Module(s) for which you would like more info, utilizes cfbs `info` function",
8697
)
98+
_add_index_arg(moduleinfo_parser)
8799

88100
show_parser = subp.add_parser(
89101
"show", help="Shows your saved host-groups or info about a specified host"

‎src/cfengine_cli/cfengine_wrapper/cfengine_commands.py‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import os
22

3+
from cfbs.cfbs_config import CFBSConfig
34
from cfbs.utils import is_cfbs_repo
5+
from cfbs.validate import validate_index_string
46
from cfbs.commands import (
57
build_command,
68
info_command,
@@ -301,7 +303,14 @@ def show(target: list[str] | None = None) -> int:
301303
return info(target)
302304

303305

304-
def moduleinfo(modules: list[str]) -> int:
306+
def _init_cfbs_config(index: str | None = None) -> None:
307+
if index is not None:
308+
validate_index_string(index)
309+
CFBSConfig.get_instance(index=index)
310+
311+
312+
def moduleinfo(modules: list[str], index: str | None = None) -> int:
313+
_init_cfbs_config(index)
305314
if modules != []:
306315
return info_command(modules)
307316
if not is_cfbs_repo():
@@ -318,17 +327,19 @@ def cfbs_input(modules: list[str]) -> int:
318327
return input_command(modules, "cfengine input")
319328

320329

321-
def cfbs_add(modules: list[str]) -> int:
322-
return add_command(modules, "cfengine input")
330+
def cfbs_add(modules: list[str], index: str | None = None) -> int:
331+
_init_cfbs_config(index)
332+
return add_command(modules, "cfengine add")
323333

324334

325-
def cfbs_remove(modules: list[str] | None = None) -> int:
326-
return remove_command(modules, "cfengine input")
335+
def cfbs_remove(modules: list[str]) -> int:
336+
return remove_command(modules)
327337

328338

329-
def cfbs_update(to_update) -> int:
339+
def cfbs_update(to_update: list[str]) -> int:
330340
return update_command(to_update)
331341

332342

333-
def cfbs_search(modules: list[str]) -> int:
343+
def cfbs_search(modules: list[str], index: str | None = None) -> int:
344+
_init_cfbs_config(index)
334345
return search_command(modules)

‎src/cfengine_cli/main.py‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@
3030
validate_deploy_args,
3131
validate_destroy_args,
3232
)
33-
from cfbs.utils import CFBSProgrammerError
33+
from cfbs.utils import (
34+
CFBSProgrammerError,
35+
CFBSExitError,
36+
CFBSUserError,
37+
CFBSNetworkError,
38+
CFBSValidationError,
39+
)
40+
from cfbs.git import CFBSGitError
3441

3542

3643
def _get_arg_parser():
@@ -248,11 +255,11 @@ def run_command_with_args(args) -> int:
248255
if args.command == "input":
249256
return cfengine_commands.cfbs_input(args.module)
250257
if args.command == "add":
251-
return cfengine_commands.cfbs_add(args.module)
258+
return cfengine_commands.cfbs_add(args.module, args.index)
252259
if args.command == "remove":
253260
return cfengine_commands.cfbs_remove(args.module)
254261
if args.command == "search":
255-
return cfengine_commands.cfbs_search(args.module)
262+
return cfengine_commands.cfbs_search(args.module, args.index)
256263
if args.command == "update":
257264
return cfengine_commands.cfbs_update(args.to_update)
258265
if args.command == "format":
@@ -363,7 +370,7 @@ def run_command_with_args(args) -> int:
363370
if args.command == "show":
364371
return cfengine_commands.show(args.hosts)
365372
if args.command == "moduleinfo":
366-
return cfengine_commands.moduleinfo(args.modules)
373+
return cfengine_commands.moduleinfo(args.modules, args.index)
367374
if args.command == "connect":
368375
return cfengine_commands.connect(args.hosts)
369376
raise UserError(f"Unknown command: '{args.command}'")
@@ -429,7 +436,16 @@ def main():
429436
exit_code = _main()
430437
assert type(exit_code) is int
431438
sys.exit(exit_code)
432-
except (UserError, CFRUserError, CFRExitError) as e:
439+
except (
440+
UserError,
441+
CFRUserError,
442+
CFRExitError,
443+
CFBSExitError,
444+
CFBSUserError,
445+
CFBSNetworkError,
446+
CFBSValidationError,
447+
CFBSGitError,
448+
) as e:
433449
print(str(e))
434450
sys.exit(-1)
435451
# Exceptions below are not expected, print extra info:

0 commit comments

Comments
 (0)