mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-19 15:28:28 +01:00
cli: fix overwriting remotes in dvc remote add
Now dvc remote add requires user confirmation in case of overwriting of an already existing remote. Fixes #1760
This commit is contained in:
+21
-2
@@ -5,12 +5,17 @@ import os
|
||||
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.
|
||||
@@ -39,6 +44,13 @@ class CmdRemoteAdd(CmdConfig):
|
||||
)
|
||||
|
||||
section = Config.SECTION_REMOTE_FMT.format(self.args.name)
|
||||
|
||||
try:
|
||||
self._prevent_config_overwriting(section)
|
||||
except DvcException as exc:
|
||||
logger.error(exc)
|
||||
return 1
|
||||
|
||||
ret = self._set(section, Config.SECTION_REMOTE_URL, self.args.url)
|
||||
if ret != 0:
|
||||
return ret
|
||||
@@ -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."
|
||||
|
||||
+2
-2
@@ -179,8 +179,8 @@ class CyclicGraphError(DvcException):
|
||||
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 "
|
||||
"from the user. Use '-f' to force.".format(path)
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -57,6 +57,15 @@ class TestRemote(TestDvc):
|
||||
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])
|
||||
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):
|
||||
|
||||
Reference in New Issue
Block a user