|
| 1 | +# Databricks CLI |
| 2 | +# Copyright 2017 Databricks, Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"), except |
| 5 | +# that the use of services to which certain application programming |
| 6 | +# interfaces (each, an "API") connect requires that the user first obtain |
| 7 | +# a license for the use of the APIs from Databricks, Inc. ("Databricks"), |
| 8 | +# by creating an account at www.databricks.com and agreeing to either (a) |
| 9 | +# the Community Edition Terms of Service, (b) the Databricks Terms of |
| 10 | +# Service, or (c) another written agreement between Licensee and Databricks |
| 11 | +# for the use of the APIs. |
| 12 | +# |
| 13 | +# You may not use this file except in compliance with the License. |
| 14 | +# You may obtain a copy of the License at |
| 15 | +# |
| 16 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | +# |
| 18 | +# Unless required by applicable law or agreed to in writing, software |
| 19 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 20 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | +# See the License for the specific language governing permissions and |
| 22 | +# limitations under the License. |
| 23 | + |
| 24 | +import click |
| 25 | + |
| 26 | +from databricks_cli.click_types import OneOfOption |
| 27 | +from databricks_cli.configure.config import provide_api_client, profile_option, debug_option |
| 28 | +from databricks_cli.repos.api import ReposApi |
| 29 | +from databricks_cli.utils import CONTEXT_SETTINGS, eat_exceptions, pretty_format |
| 30 | +from databricks_cli.version import print_version_callback, version |
| 31 | + |
| 32 | +UPDATE_OPTIONS = ['branch', 'tag'] |
| 33 | +ID_OPTIONS = ['repo-id', 'path'] |
| 34 | + |
| 35 | + |
| 36 | +@click.command(context_settings=CONTEXT_SETTINGS, |
| 37 | + short_help='List repos that user has Manage permissions on') |
| 38 | +@click.option('--path-prefix', help="Path prefix to filter results by") |
| 39 | +@click.option('--next-page-token', help="Token used to fetch the next page of results") |
| 40 | +@debug_option |
| 41 | +@profile_option |
| 42 | +@eat_exceptions # noqa |
| 43 | +@provide_api_client |
| 44 | +def list_repos_cli(api_client, path_prefix, next_page_token): |
| 45 | + """ |
| 46 | + List repos that the user has Manage permissions on. |
| 47 | + """ |
| 48 | + content = ReposApi(api_client).list(path_prefix, next_page_token) |
| 49 | + click.echo(pretty_format(content)) |
| 50 | + |
| 51 | + |
| 52 | +@click.command(context_settings=CONTEXT_SETTINGS, |
| 53 | + short_help='Create a repo and link it to the given remote Git repo') |
| 54 | +@click.option('--url', required=True, help="URL of the remote Git repo") |
| 55 | +@click.option('--provider', required=True, help="Git provider (case insensitive)") |
| 56 | +@click.option('--path', help="Desired workspace path of the repo object") |
| 57 | +@debug_option |
| 58 | +@profile_option |
| 59 | +@eat_exceptions # noqa |
| 60 | +@provide_api_client |
| 61 | +def create_repo_cli(api_client, url, provider, path): |
| 62 | + """ |
| 63 | + Creates a repo object and links it to the remote Git repo specified. |
| 64 | + """ |
| 65 | + content = ReposApi(api_client).create(url, provider, path) |
| 66 | + click.echo(pretty_format(content)) |
| 67 | + |
| 68 | + |
| 69 | +@click.command(context_settings=CONTEXT_SETTINGS, |
| 70 | + short_help='Get repo based on ID or path') |
| 71 | +@click.option('--repo-id', cls=OneOfOption, default=None, one_of=ID_OPTIONS, help="Repo ID") |
| 72 | +@click.option('--path', cls=OneOfOption, default=None, one_of=ID_OPTIONS, |
| 73 | + help="Workspace path of the repo object") |
| 74 | +@debug_option |
| 75 | +@profile_option |
| 76 | +@eat_exceptions # noqa |
| 77 | +@provide_api_client |
| 78 | +def get_repo_cli(api_client, repo_id, path): |
| 79 | + """ |
| 80 | + Gets the repo. |
| 81 | + """ |
| 82 | + id_from_param_or_path = (repo_id if repo_id is not None |
| 83 | + else ReposApi(api_client).get_repo_id(path)) |
| 84 | + content = ReposApi(api_client).get(id_from_param_or_path) |
| 85 | + click.echo(pretty_format(content)) |
| 86 | + |
| 87 | + |
| 88 | +@click.command(context_settings=CONTEXT_SETTINGS, |
| 89 | + short_help='Checkout the repo to the given branch or tag') |
| 90 | +@click.option('--repo-id', cls=OneOfOption, default=None, one_of=ID_OPTIONS, help="Repo ID") |
| 91 | +@click.option('--path', cls=OneOfOption, default=None, one_of=ID_OPTIONS, |
| 92 | + help="Workspace path of the repo object") |
| 93 | +@click.option('--branch', cls=OneOfOption, default=None, one_of=UPDATE_OPTIONS, help="Branch name") |
| 94 | +@click.option('--tag', cls=OneOfOption, default=None, one_of=UPDATE_OPTIONS, help="Tag name") |
| 95 | +@debug_option |
| 96 | +@profile_option |
| 97 | +@eat_exceptions # noqa |
| 98 | +@provide_api_client |
| 99 | +def update_repo_cli(api_client, repo_id, branch, tag, path): |
| 100 | + """ |
| 101 | + Checks out the repo to the given branch or tag. This call returns an error if the branch |
| 102 | + or tag doesn't exist. |
| 103 | + """ |
| 104 | + id_from_param_or_path = (repo_id if repo_id is not None |
| 105 | + else ReposApi(api_client).get_repo_id(path)) |
| 106 | + content = ReposApi(api_client).update(id_from_param_or_path, branch, tag) |
| 107 | + click.echo(pretty_format(content)) |
| 108 | + |
| 109 | + |
| 110 | +@click.command(context_settings=CONTEXT_SETTINGS, |
| 111 | + short_help='Delete the repo based on ID or path') |
| 112 | +@click.option('--repo-id', cls=OneOfOption, default=None, one_of=ID_OPTIONS, help="Repo ID") |
| 113 | +@click.option('--path', cls=OneOfOption, default=None, one_of=ID_OPTIONS, |
| 114 | + help="Workspace path of the repo object") |
| 115 | +@debug_option |
| 116 | +@profile_option |
| 117 | +@eat_exceptions # noqa |
| 118 | +@provide_api_client |
| 119 | +def delete_repo_cli(api_client, repo_id, path): |
| 120 | + """ |
| 121 | + Deletes the repo. |
| 122 | + """ |
| 123 | + id_from_param_or_path = (repo_id if repo_id is not None |
| 124 | + else ReposApi(api_client).get_repo_id(path)) |
| 125 | + content = ReposApi(api_client).delete(id_from_param_or_path) |
| 126 | + click.echo(pretty_format(content)) |
| 127 | + |
| 128 | + |
| 129 | +@click.group(context_settings=CONTEXT_SETTINGS, |
| 130 | + short_help="Utility to interact with Repos.") |
| 131 | +@click.option("--version", "-v", is_flag=True, callback=print_version_callback, |
| 132 | + expose_value=False, is_eager=True, help=version) |
| 133 | +@debug_option |
| 134 | +@profile_option |
| 135 | +@eat_exceptions |
| 136 | +def repos_group(): # pragma: no cover |
| 137 | + """Utility to interact with Repos.""" |
| 138 | + pass |
| 139 | + |
| 140 | + |
| 141 | +repos_group.add_command(list_repos_cli, name='list') |
| 142 | +repos_group.add_command(get_repo_cli, name='get') |
| 143 | +repos_group.add_command(create_repo_cli, name='create') |
| 144 | +repos_group.add_command(update_repo_cli, name='update') |
| 145 | +repos_group.add_command(delete_repo_cli, name='delete') |
0 commit comments