def test_execute_command(fx_get_sub_parser, fx_mock_res_client, caplog):
    with patch("resilient_sdk.cmds.clone.get_resilient_client") as mock_client:

        mock_client.return_value = fx_mock_res_client
        cmd_clone = CmdClone(fx_get_sub_parser)
        args = cmd_clone.parser.parse_known_args()[0]

        cmd_clone.execute_command(args)
        mock_client.assert_called_once_with(path_config_file=None)
        assert "'clone' command finished in" in caplog.text
def test_execute_command_with_custom_config_file(fx_get_sub_parser,
                                                 fx_mock_res_client, caplog):

    mock_app_config_path = "mock_path"
    sys.argv.extend(["-c", mock_app_config_path])

    with patch("resilient_sdk.cmds.clone.get_resilient_client") as mock_client:

        mock_client.return_value = fx_mock_res_client
        cmd_clone = CmdClone(fx_get_sub_parser)
        args = cmd_clone.parser.parse_known_args()[0]

        cmd_clone.execute_command(args)
        mock_client.assert_called_once_with(
            path_config_file=mock_app_config_path)
        assert "'clone' command finished in" in caplog.text
Example #3
0
def main():
    """
    Main entry point for resilient-sdk
    """

    # See if RES_SDK_DEV environment var is set
    sdk_dev = sdk_helpers.str_to_bool(os.getenv("RES_SDK_DEV"))

    # Get main parser object
    parser = get_main_app_parser()

    # Get sub_parser object, its dest is cmd
    sub_parser = get_main_app_sub_parser(parser)

    # Add any subcommands to main app parser here
    cmd_codegen = CmdCodegen(sub_parser)
    cmd_clone = CmdClone(sub_parser)
    cmd_docgen = CmdDocgen(sub_parser)
    cmd_extract = CmdExtract(sub_parser)
    cmd_ext_package = CmdExtPackage(sub_parser)

    if sdk_dev:
        # Add 'dev' command if environment var set
        cmd_dev = CmdDev(sub_parser)

    try:
        # Parse the arguments
        args = parser.parse_args()

        if args.cmd is None:
            parser.print_help()
            sys.exit()

    except SDKException as err:
        # Get main_cmd (codegen, docgen etc.)
        main_cmd = sdk_helpers.get_main_cmd()

        LOG.error(err)
        LOG.info("\n-----------------\n")

        # Print specifc usage for that cmd for these errors
        if "too few arguments" in err.message or "no subcommand provided" in err.message:
            if main_cmd == cmd_codegen.CMD_NAME:
                cmd_codegen.parser.print_usage()

            elif main_cmd == cmd_clone.CMD_NAME:
                cmd_clone.parser.print_usage()

            elif main_cmd == cmd_docgen.CMD_NAME:
                cmd_docgen.parser.print_usage()

            elif main_cmd == cmd_extract.CMD_NAME:
                cmd_extract.parser.print_usage()

            elif main_cmd == cmd_ext_package.CMD_NAME:
                cmd_ext_package.parser.print_usage()

            elif sdk_dev and main_cmd == cmd_dev.CMD_NAME:
                cmd_dev.parser.print_usage()

            else:
                parser.print_help()

        # Exit
        sys.exit()

    # If -v was specified, set the log level to DEBUG
    if args.verbose:
        LOG.setLevel(logging.DEBUG)
        LOG.debug("Logging set to DEBUG mode")

    # Handle what subcommand was called
    if args.cmd == cmd_docgen.CMD_NAME:
        cmd_docgen.execute_command(args)

    elif args.cmd == cmd_codegen.CMD_NAME:
        cmd_codegen.execute_command(args)

    elif args.cmd == cmd_clone.CMD_NAME:
        cmd_clone.execute_command(args)

    elif args.cmd == cmd_extract.CMD_NAME:
        cmd_extract.execute_command(args)

    elif args.cmd == cmd_ext_package.CMD_NAME:
        cmd_ext_package.execute_command(args)

    elif sdk_dev and args.cmd == cmd_dev.CMD_NAME:
        cmd_dev.execute_command(args)
Example #4
0
def main():
    """
    Main entry point for resilient-sdk
    """

    # add color support for WINDOWS
    os.system("")

    # See if RES_SDK_DEV environment var is set
    sdk_dev = sdk_helpers.is_env_var_set(constants.ENV_VAR_DEV)

    # Get main parser object
    parser = get_main_app_parser()

    # Get sub_parser object, its dest is cmd
    sub_parser = get_main_app_sub_parser(parser)

    if sdk_dev:
        # Add 'dev' command if environment var set
        cmd_dev = CmdDev(sub_parser)
        LOG.info("{0}Running SDK in Developer Mode{0}".format(constants.LOG_DIVIDER))

    # Add any subcommands to main app parser here
    cmd_validate = CmdValidate(sub_parser)
    cmd_codegen = CmdCodegen(sub_parser)
    cmd_clone = CmdClone(sub_parser)
    cmd_docgen = CmdDocgen(sub_parser)
    cmd_extract = CmdExtract(sub_parser)
    cmd_ext_package = CmdExtPackage(sub_parser, cmd_validate=cmd_validate)

    try:
        # Parse the arguments
        args = parser.parse_args()

        if args.cmd is None:
            parser.print_help()
            sys.exit()

    except SDKException as err:
        # Get main_cmd (codegen, docgen etc.)
        main_cmd = sdk_helpers.get_main_cmd()

        LOG.error(err)
        LOG.info("{0}".format(constants.LOG_DIVIDER))

        # Print specifc usage for that cmd for these errors
        if "too few arguments" in err.message or "no subcommand provided" in err.message:
            if main_cmd == cmd_codegen.CMD_NAME:
                cmd_codegen.parser.print_usage()

            elif main_cmd == cmd_clone.CMD_NAME:
                cmd_clone.parser.print_usage()

            elif main_cmd == cmd_docgen.CMD_NAME:
                cmd_docgen.parser.print_usage()

            elif main_cmd == cmd_extract.CMD_NAME:
                cmd_extract.parser.print_usage()

            elif main_cmd == cmd_ext_package.CMD_NAME:
                cmd_ext_package.parser.print_usage()

            elif main_cmd == cmd_validate.CMD_NAME:
                cmd_validate.parser.print_usage()

            elif sdk_dev and main_cmd == cmd_dev.CMD_NAME:
                cmd_dev.parser.print_usage()

            else:
                parser.print_help()

        # Exit
        sys.exit()

    # If -v was specified, set the log level to DEBUG
    if args.verbose:
        LOG.setLevel(logging.DEBUG)
        LOG.debug("Logging set to DEBUG mode")

    # Handle what subcommand was called
    if args.cmd == cmd_docgen.CMD_NAME:
        cmd_docgen.execute_command(args)

    elif args.cmd == cmd_codegen.CMD_NAME:
        cmd_codegen.execute_command(args)

    elif args.cmd == cmd_clone.CMD_NAME:
        cmd_clone.execute_command(args)

    elif args.cmd == cmd_extract.CMD_NAME:
        cmd_extract.execute_command(args)

    elif args.cmd == cmd_ext_package.CMD_NAME:
        cmd_ext_package.execute_command(args)

    elif args.cmd == cmd_validate.CMD_NAME:
        cmd_validate.execute_command(args)
    elif sdk_dev and args.cmd == cmd_dev.CMD_NAME:
        cmd_dev.execute_command(args)