Skip to content

Commit

Permalink
feat(core): add template command (#2590)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-alisafaee committed Feb 21, 2022
1 parent 1a544ca commit 4ff9c4f
Show file tree
Hide file tree
Showing 40 changed files with 3,224 additions and 1,497 deletions.
7 changes: 7 additions & 0 deletions docs/reference/commands.rst
Expand Up @@ -27,6 +27,13 @@ Renku Command Line

.. automodule:: renku.cli.init

.. _cli-template:

``renku template``
------------------

.. automodule:: renku.cli.template

.. _cli-clone:

``renku clone``
Expand Down
34 changes: 27 additions & 7 deletions renku/cli/__init__.py
Expand Up @@ -64,6 +64,7 @@
import sys
import uuid
from pathlib import Path
from typing import List

import click
import yaml
Expand Down Expand Up @@ -91,6 +92,7 @@
from renku.cli.service import service
from renku.cli.status import status
from renku.cli.storage import storage
from renku.cli.template import template
from renku.cli.update import update
from renku.cli.workflow import workflow
from renku.core.commands.echo import WARNING
Expand All @@ -116,7 +118,7 @@ def get_entry_points(name: str):
return all_entry_points.get(name, [])


WARNING_UNPROTECTED_COMMANDS = ["clone", "init", "help", "login", "logout", "service", "credentials", "env"]
WARNING_UNPROTECTED_COMMANDS = ["clone", "credentials", "env", "help", "init", "login", "logout", "service"]


def _uuid_representer(dumper, data):
Expand All @@ -139,7 +141,22 @@ def print_global_config_path(ctx, param, value):

def is_allowed_command(ctx):
"""Check if invoked command contains help command."""
return ctx.invoked_subcommand in WARNING_UNPROTECTED_COMMANDS or "-h" in sys.argv or "--help" in sys.argv

def is_allowed_subcommands(command: str, subcommands: List[str]):
args = sys.argv

if ctx.invoked_subcommand != command or command not in args:
return False

subcommand_index = args.index(command) + 1
return subcommand_index < len(args) and args[subcommand_index] in subcommands

return (
ctx.invoked_subcommand in WARNING_UNPROTECTED_COMMANDS
or is_allowed_subcommands("template", ["ls", "show"])
or "-h" in sys.argv
or "--help" in sys.argv
)


@with_plugins(get_entry_points("renku.cli_plugins"))
Expand Down Expand Up @@ -182,8 +199,10 @@ def cli(ctx, path, external_storage_requested):
renku_path = Path(path) / RENKU_HOME
old_metadata = renku_path / OLD_METADATA_PATH
new_metadata = renku_path / RepositoryApiMixin.DATABASE_PATH / Database.ROOT_OID
is_command_allowed = is_allowed_command(ctx)
is_renku_project = old_metadata.exists() or new_metadata.exists()

if not old_metadata.exists() and not new_metadata.exists() and not is_allowed_command(ctx):
if not is_renku_project and not is_command_allowed:
raise UsageError(
(
"`{0}` is not a renku repository.\n"
Expand All @@ -194,7 +213,7 @@ def cli(ctx, path, external_storage_requested):

ctx.obj = LocalClient(path=path, external_storage_requested=external_storage_requested)

if path != os.getcwd() and ctx.invoked_subcommand not in WARNING_UNPROTECTED_COMMANDS:
if is_renku_project and path != os.getcwd() and not is_command_allowed:
click.secho(WARNING + "Run CLI commands only from project's root directory.\n", err=True)


Expand All @@ -206,8 +225,10 @@ def help(ctx):


# Register subcommands:
cli.add_command(check_immutable_template_files)
cli.add_command(clone)
cli.add_command(config)
cli.add_command(credentials)
cli.add_command(dataset)
cli.add_command(doctor)
cli.add_command(env)
Expand All @@ -219,17 +240,16 @@ def help(ctx):
cli.add_command(logout)
cli.add_command(migrate)
cli.add_command(migrationscheck)
cli.add_command(check_immutable_template_files)
cli.add_command(move)
cli.add_command(project)
cli.add_command(remove)
cli.add_command(rerun)
cli.add_command(rollback)
cli.add_command(run)
cli.add_command(save)
cli.add_command(service)
cli.add_command(status)
cli.add_command(storage)
cli.add_command(credentials)
cli.add_command(template)
cli.add_command(update)
cli.add_command(workflow)
cli.add_command(service)
37 changes: 17 additions & 20 deletions renku/cli/init.py
Expand Up @@ -52,15 +52,15 @@
.. code-block:: console
$ renku init --list-templates
$ renku template ls
INDEX ID DESCRIPTION PARAMETERS
----- ------ ------------------------------- -----------------------------
1 python The simplest Python-based [...] description: project des[...]
2 R R-based renku project with[...] description: project des[...]
If you know which template you are going to use, you can provide either the id
``--template-id`` or the template index number ``--template-index``.
If you know which template you are going to use, you can provide its id using
``--template-id``.
You can use a newer version of the templates or even create your own one and
provide it to the ``init`` command by specifying the target template repository
Expand Down Expand Up @@ -91,7 +91,7 @@
~~~~~~~~~~~~~~~~~-
Some templates require parameters to properly initialize a new project. You
can check them by listing the templates ``--list-templates``.
can check them by listing the templates ``renku template ls --verbose``.
To provide parameters, use the ``--parameter`` option and provide each
parameter using ``--parameter "param1"="value1"``.
Expand Down Expand Up @@ -197,11 +197,6 @@
from renku.core import errors
from renku.core.commands.options import option_external_storage_requested

_GITLAB_CI = ".gitlab-ci.yml"
_DOCKERFILE = "Dockerfile"
_REQUIREMENTS = "requirements.txt"
CI_TEMPLATES = [_GITLAB_CI, _DOCKERFILE, _REQUIREMENTS]

INVALID_DATA_DIRS = [".", ".renku", ".git"]
"""Paths that cannot be used as data directory name."""

Expand Down Expand Up @@ -256,7 +251,7 @@ def resolve_data_directory(data_dir, path):
help="Data directory within the project",
)
@click.option("-t", "--template-id", help="Provide the id of the template to use.")
@click.option("-i", "--template-index", help="Provide the index number of the template to use.", type=int)
@click.option("-i", "--template-index", help="Deprecated", type=int)
@click.option("-s", "--template-source", help="Provide the templates repository url or path.")
@click.option(
"-r", "--template-ref", default=None, help="Specify the reference to checkout on remote template repository."
Expand Down Expand Up @@ -311,13 +306,21 @@ def init(
from renku.core.commands.init import init_command
from renku.core.utils.git import check_global_git_user_is_configured

if template_ref and not template_source:
raise errors.ParameterError("Can't use '--template-ref' without specifying '--template-source'")
if describe:
raise errors.ParameterError("'-d/--describe' is deprecated: Use 'renku template show' instead.")
if list_templates:
raise errors.ParameterError("'-l/--list-templates' is deprecated: Use 'renku template ls' instead.")
if template_index:
raise errors.ParameterError(
"'-i/--template-index' is deprecated: Use '-t/--template-id' to pass a template id."
)

data_dir = resolve_data_directory(data_dir, path)

check_global_git_user_is_configured()

if template_ref and not template_source:
raise errors.ParameterError("Can't use '--template-ref' without specifying '--template-source'")

custom_metadata = None
if metadata:
custom_metadata = json.loads(Path(metadata).read_text())
Expand All @@ -331,21 +334,15 @@ def init(
description=description,
keywords=keyword,
template_id=template_id,
template_index=template_index,
template_source=template_source,
template_ref=template_ref,
metadata=parameters,
input_parameters=parameters,
custom_metadata=custom_metadata,
list_templates=list_templates,
force=force,
describe=describe,
data_dir=data_dir,
initial_branch=initial_branch,
)

if list_templates:
return

# Install git hooks
from .githooks import install

Expand Down

0 comments on commit 4ff9c4f

Please sign in to comment.