get: implement --show-url to display only url/path to remote

This commit is contained in:
Saugat Pachhai
2020-01-16 19:28:14 +05:45
parent 1f1fb03138
commit 4509f82bc3
3 changed files with 53 additions and 0 deletions
+26
View File
@@ -10,7 +10,28 @@ logger = logging.getLogger(__name__)
class CmdGet(CmdBaseNoRepo):
def _run_get_url(self):
from dvc.api import get_url
try:
url = get_url(
self.args.path, repo=self.args.url, rev=self.args.rev
)
logger.info("{} {}".format(self.args.path, url))
except DvcException:
logger.exception(
"failed to show url for '{}' from '{}'".format(
self.args.path, self.args.url
)
)
return 1
return 0
def run(self):
if self.args.show_url:
return self._run_get_url()
from dvc.repo import Repo
try:
@@ -55,4 +76,9 @@ def add_parser(subparsers, parent_parser):
get_parser.add_argument(
"--rev", nargs="?", help="Git revision (e.g. branch, tag, SHA)"
)
get_parser.add_argument(
"--show-url",
action="store_true",
help="Returns path/url to the location in remote for specified path",
)
get_parser.set_defaults(func=CmdGet)
+13
View File
@@ -5,6 +5,7 @@ import pytest
from dvc.cache import Cache
from dvc.config import Config
from dvc.main import main
from dvc.repo.get import GetDVCFileError, PathMissingError
from dvc.repo import Repo
from dvc.system import System
@@ -184,3 +185,15 @@ def test_get_from_non_dvc_master(tmp_dir, erepo_dir, caplog):
assert caplog.text == ""
assert (tmp_dir / dst).read_text() == "some_contents"
def test_get_url(tmp_dir, erepo_dir):
with erepo_dir.chdir():
erepo_dir.dvc_gen("foo", "foo")
assert main(["get", ".", "foo", "--show-url"]) == 0
def test_get_url_not_existing(tmp_dir, erepo_dir):
with erepo_dir.chdir():
erepo_dir.dvc_gen("foo", "foo")
assert main(["get", ".", "food", "--show-url"]) == 1
+14
View File
@@ -14,3 +14,17 @@ def test_get(mocker):
assert cmd.run() == 0
m.assert_called_once_with("repo_url", path="src", out="out", rev="version")
def test_get_url(mocker):
cli_args = parse_args(
["get", "repo_url", "src", "--rev", "version", "--show-url"]
)
assert cli_args.func == CmdGet
cmd = cli_args.func(cli_args)
m = mocker.patch("dvc.api.get_url")
assert cmd.run() == 0
m.assert_called_once_with("src", repo="repo_url", rev="version")