Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: fix overwriting remotes in dvc remote add #1808

Merged
merged 3 commits into from
Mar 31, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions dvc/command/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
import re

import dvc.logger as logger
from dvc.command.base import fix_subparsers, append_doc_link
from dvc.config import Config
from dvc.command.base import append_doc_link, fix_subparsers
from dvc.command.config import CmdConfig
from dvc.config import Config
from dvc.exceptions import ConfirmRemoveError, DvcException


class CmdRemoteAdd(CmdConfig):
def _prevent_config_overwriting(self, section):
if (section in self.configobj.keys()) and not self.args.force:
raise ConfirmRemoveError(section)

@staticmethod
def resolve_path(path, config_file):
"""Resolve path relative to config file location.
Expand Down Expand Up @@ -39,6 +44,13 @@ def run(self):
)

section = Config.SECTION_REMOTE_FMT.format(self.args.name)

try:
self._prevent_config_overwriting(section)
except DvcException as exc:
puhoshville marked this conversation as resolved.
Show resolved Hide resolved
logger.error(exc)
return 1

ret = self._set(section, Config.SECTION_REMOTE_URL, self.args.url)
if ret != 0:
return ret
Expand Down Expand Up @@ -156,6 +168,13 @@ def add_parser(subparsers, parent_parser):
default=False,
help="Set as default remote.",
)
remote_add_parser.add_argument(
"-f",
"--force",
action="store_true",
default=False,
help="Force overwriting existing configs",
)
remote_add_parser.set_defaults(func=CmdRemoteAdd)

REMOTE_DEFAULT_HELP = "Set/unset default remote."
Expand Down
4 changes: 2 additions & 2 deletions dvc/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ def __init__(self, stages):
class ConfirmRemoveError(DvcException):
def __init__(self, path):
super(ConfirmRemoveError, self).__init__(
"unable to remove '{}' without a confirmation from the user. Use "
"'-f' to force.".format(path)
"unable to remove or overwrite '{}' without a confirmation "
puhoshville marked this conversation as resolved.
Show resolved Hide resolved
"from the user. Use '-f' to force.".format(path)
)


Expand Down
9 changes: 9 additions & 0 deletions tests/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ def test_relative_path(self):
config = configobj.ConfigObj(self.dvc.config.config_file)
self.assertEqual(config['remote "mylocal"']["url"], rel)

def test_overwrite(self):
remote_name = "a"
remote_url = "s3://bucket/name"
main(["remote", "add", remote_name, remote_url])
puhoshville marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(main(["remote", "add", remote_name, remote_url]), 1)
self.assertEqual(
main(["remote", "add", "-f", remote_name, remote_url]), 0
)


class TestRemoteRemoveDefault(TestDvc):
def test(self):
Expand Down