def do_cli( ctx, no_interactive, location, runtime, dependency_manager, output_dir, name, app_template, no_input, extra_context, auto_clone=True, ): from samcli.commands.init.init_generator import do_generate from samcli.commands.init.init_templates import InitTemplates from samcli.commands.init.interactive_init_flow import do_interactive # check for mutually exclusive parameters if location and app_template: msg = """ You must not provide both the --location and --app-template parameters. You can run 'sam init' without any options for an interactive initialization flow, or you can provide one of the following required parameter combinations: --name and --runtime and --app-template and --dependency-manager --location """ raise UserException(msg) # check for required parameters if location or (name and runtime and dependency_manager and app_template): # need to turn app_template into a location before we generate if app_template: templates = InitTemplates(no_interactive, auto_clone) location = templates.location_from_app_template( runtime, dependency_manager, app_template) no_input = True default_context = {"project_name": name, "runtime": runtime} if extra_context is None: extra_context = default_context else: extra_context = _merge_extra_context(default_context, extra_context) if not output_dir: output_dir = "." do_generate(location, runtime, dependency_manager, output_dir, name, no_input, extra_context) elif no_interactive: error_msg = """ ERROR: Missing required parameters, with --no-interactive set. Must provide one of the following required parameter combinations: --name and --runtime and --dependency-manager and --app-template --location You can also re-run without the --no-interactive flag to be prompted for required values. """ raise UserException(error_msg) else: # proceed to interactive state machine, which will call do_generate do_interactive(location, runtime, dependency_manager, output_dir, name, app_template, no_input)
def test_location_from_app_template_image(self, subprocess_mock, git_exec_mock, sd_mock, copy_mock): it = InitTemplates(True) manifest = { "ruby2.5-image": [{ "directory": "mock-ruby-image-template", "displayName": "Hello World Lambda Image Example", "dependencyManager": "bundler", "appTemplate": "hello-world-lambda-image", "packageType": IMAGE, }] } manifest_json = json.dumps(manifest) m = mock_open(read_data=manifest_json) with patch("samcli.cli.global_config.GlobalConfig.config_dir", new_callable=PropertyMock) as mock_cfg: mock_cfg.return_value = "/tmp/test-sam" with patch("samcli.commands.init.init_templates.open", m): location = it.location_from_app_template( IMAGE, None, "ruby2.5-image", "bundler", "hello-world-lambda-image") self.assertTrue(search("mock-ruby-image-template", location))
def _generate_from_app_template(location, runtime, dependency_manager, output_dir, name, app_template): extra_context = None if not name: name = click.prompt("Project Name", type=str) if not runtime: runtime = click.prompt("Runtime", type=click.Choice(RUNTIMES)) if not dependency_manager: valid_dep_managers = RUNTIME_TO_DEPENDENCY_MANAGERS.get(runtime) if valid_dep_managers is None: dependency_manager = None else: dependency_manager = click.prompt( "Dependency Manager", type=click.Choice(valid_dep_managers), default=valid_dep_managers[0]) templates = InitTemplates() if app_template is not None: location = templates.location_from_app_template( runtime, dependency_manager, app_template) extra_context = {"project_name": name, "runtime": runtime} else: location = templates.prompt_for_location(runtime, dependency_manager) extra_context = {"project_name": name, "runtime": runtime} no_input = True if not output_dir: output_dir = click.prompt("Output Directory", type=click.Path(), default=".") do_generate(location, runtime, dependency_manager, output_dir, name, no_input, extra_context)
def _generate_from_app_template(location, runtime, dependency_manager, output_dir, name, app_template): extra_context = None runtime = _get_runtime(runtime) dependency_manager = _get_dependency_manager(dependency_manager, runtime) if not name: name = click.prompt("\nProject name", type=str, default="sam-app") templates = InitTemplates() if app_template is not None: location = templates.location_from_app_template( runtime, dependency_manager, app_template) extra_context = {"project_name": name, "runtime": runtime} else: location, app_template = templates.prompt_for_location( runtime, dependency_manager) extra_context = {"project_name": name, "runtime": runtime} # executing event_bridge logic if call is for Schema dynamic template is_dynamic_schemas_template = templates.is_dynamic_schemas_template( app_template, runtime, dependency_manager) if is_dynamic_schemas_template: schemas_api_caller = get_schemas_api_caller() schema_template_details = _get_schema_template_details( schemas_api_caller) schemas_template_parameter = get_schemas_template_parameter( schema_template_details) extra_context = {**schemas_template_parameter, **extra_context} no_input = True summary_msg = """ ----------------------- Generating application: ----------------------- Name: {name} Runtime: {runtime} Dependency Manager: {dependency_manager} Application Template: {app_template} Output Directory: {output_dir} Next steps can be found in the README file at {output_dir}/{name}/README.md """.format( name=name, runtime=runtime, dependency_manager=dependency_manager, app_template=app_template, output_dir=output_dir, ) click.echo(summary_msg) do_generate(location, runtime, dependency_manager, output_dir, name, no_input, extra_context) # executing event_bridge logic if call is for Schema dynamic template if is_dynamic_schemas_template: _package_schemas_code(runtime, schemas_api_caller, schema_template_details, output_dir, name, location)
def do_cli( ctx, no_interactive, location, pt_explicit, package_type, runtime, base_image, dependency_manager, output_dir, name, app_template, no_input, extra_context, auto_clone=True, ): """ Implementation of the ``cli`` method """ from samcli.commands.init.init_generator import do_generate from samcli.commands.init.interactive_init_flow import do_interactive from samcli.commands.init.init_templates import InitTemplates from samcli.commands.exceptions import LambdaImagesTemplateException _deprecate_notification(runtime) # check for required parameters zip_bool = name and runtime and dependency_manager and app_template image_bool = name and pt_explicit and base_image if location or zip_bool or image_bool: # need to turn app_template into a location before we generate templates = InitTemplates(no_interactive, auto_clone) if package_type == IMAGE and image_bool: base_image, runtime = _get_runtime_from_image(base_image) options = templates.init_options(package_type, runtime, base_image, dependency_manager) if len(options) == 1: app_template = options[0].get("appTemplate") elif len(options) > 1: raise LambdaImagesTemplateException( "Multiple lambda image application templates found. " "This should not be possible, please raise an issue.") if app_template and not location: location = templates.location_from_app_template( package_type, runtime, base_image, dependency_manager, app_template) no_input = True extra_context = _get_cookiecutter_template_context( name, runtime, extra_context) if not output_dir: output_dir = "." do_generate(location, package_type, runtime, dependency_manager, output_dir, name, no_input, extra_context) else: # proceed to interactive state machine, which will call do_generate do_interactive( location, pt_explicit, package_type, runtime, base_image, dependency_manager, output_dir, name, app_template, no_input, )
def _generate_from_app_template(location, runtime, dependency_manager, output_dir, name, app_template): extra_context = None if not runtime: choices = list(map(str, range(1, len(INIT_RUNTIMES) + 1))) choice_num = 1 click.echo("\nWhich runtime would you like to use?") for r in INIT_RUNTIMES: msg = "\t" + str(choice_num) + " - " + r click.echo(msg) choice_num = choice_num + 1 choice = click.prompt("Runtime", type=click.Choice(choices), show_choices=False) runtime = INIT_RUNTIMES[int(choice) - 1] # zero index if not dependency_manager: valid_dep_managers = RUNTIME_TO_DEPENDENCY_MANAGERS.get(runtime) if valid_dep_managers is None: dependency_manager = None elif len(valid_dep_managers) == 1: dependency_manager = valid_dep_managers[0] else: choices = list(map(str, range(1, len(valid_dep_managers) + 1))) choice_num = 1 click.echo("\nWhich dependency manager would you like to use?") for dm in valid_dep_managers: msg = "\t" + str(choice_num) + " - " + dm click.echo(msg) choice_num = choice_num + 1 choice = click.prompt("Dependency manager", type=click.Choice(choices), show_choices=False) dependency_manager = valid_dep_managers[int(choice) - 1] # zero index if not name: name = click.prompt("\nProject name", type=str, default="sam-app") templates = InitTemplates() if app_template is not None: location = templates.location_from_app_template( runtime, dependency_manager, app_template) extra_context = {"project_name": name, "runtime": runtime} else: location, app_template = templates.prompt_for_location( runtime, dependency_manager) extra_context = {"project_name": name, "runtime": runtime} no_input = True summary_msg = """ ----------------------- Generating application: ----------------------- Name: {name} Runtime: {runtime} Dependency Manager: {dependency_manager} Application Template: {app_template} Output Directory: {output_dir} Next steps can be found in the README file at {output_dir}/{name}/README.md """.format( name=name, runtime=runtime, dependency_manager=dependency_manager, app_template=app_template, output_dir=output_dir, ) click.echo(summary_msg) do_generate(location, runtime, dependency_manager, output_dir, name, no_input, extra_context)