mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-07 07:18:27 +01:00
config: add ability to add and remove options
Signed-off-by: Ruslan Kuprieiev <[email protected]>
This commit is contained in:
@@ -381,6 +381,11 @@ def parse_args(argv=None):
|
||||
'config',
|
||||
parents=[parent_parser],
|
||||
help='Get or set repository options')
|
||||
config_parser.add_argument('-u',
|
||||
'--unset',
|
||||
default=False,
|
||||
action='store_true',
|
||||
help='Unset option')
|
||||
config_parser.add_argument('name',
|
||||
help='Option name')
|
||||
config_parser.add_argument('value',
|
||||
|
||||
+62
-25
@@ -1,7 +1,7 @@
|
||||
import os
|
||||
import configobj
|
||||
|
||||
from dvc.command.base import CmdBase
|
||||
from dvc.command.base import CmdBase, DvcLock
|
||||
from dvc.logger import Logger
|
||||
from dvc.config import Config
|
||||
|
||||
@@ -9,43 +9,80 @@ class CmdConfig(CmdBase):
|
||||
def __init__(self, settings):
|
||||
super(CmdConfig, self).__init__(settings)
|
||||
|
||||
# Using configobj because it doesn't
|
||||
# drop comments like configparser does.
|
||||
self.configobj = configobj.ConfigObj(self._config_path)
|
||||
|
||||
@property
|
||||
def _config_path(self):
|
||||
return os.path.join(self.git.git_dir, Config.CONFIG)
|
||||
|
||||
def _find_key(self, l, name):
|
||||
for k in l:
|
||||
def _get_key(self, d, name, add=False):
|
||||
for k in d.keys():
|
||||
if k.lower() == name.lower():
|
||||
return k
|
||||
|
||||
if add:
|
||||
d[name] = {}
|
||||
return name
|
||||
|
||||
return None
|
||||
|
||||
def run(self):
|
||||
_section, _opt = self.parsed_args.name.strip().split('.')
|
||||
|
||||
# Using configobj because it doesn't
|
||||
# drop comments like configparser does.
|
||||
config = configobj.ConfigObj(self._config_path)
|
||||
|
||||
section = self._find_key(config.keys(), _section)
|
||||
if not section:
|
||||
return 1
|
||||
|
||||
opt = self._find_key(config[section].keys(), _opt)
|
||||
if not opt:
|
||||
return 1
|
||||
|
||||
if self.parsed_args.value == None:
|
||||
Logger.info(config[section][opt])
|
||||
return 0
|
||||
|
||||
def unset(self):
|
||||
try:
|
||||
config[section][opt] = self.parsed_args.value
|
||||
config.write()
|
||||
del self.configobj[self.section][self.opt]
|
||||
self.configobj.write()
|
||||
except Exception as exc:
|
||||
Logger.error('Failed to unset \'{}\': {}'.format(self.parsed_args.name, exc))
|
||||
return 1
|
||||
|
||||
return 0
|
||||
|
||||
def show(self):
|
||||
Logger.info(self.configobj[self.section][self.opt])
|
||||
return 0
|
||||
|
||||
def set(self):
|
||||
try:
|
||||
self.configobj[self.section][self.opt] = self.parsed_args.value
|
||||
self.configobj.write()
|
||||
except Exception as exc:
|
||||
Logger.error('Failed to set \'{}\' to \'{}\': {}'.format(self.parsed_args.name,
|
||||
self.parsed_args.value,
|
||||
value,
|
||||
exc))
|
||||
return 1
|
||||
|
||||
return 0
|
||||
|
||||
def check_opt(self):
|
||||
_section, _opt = self.parsed_args.name.strip().split('.')
|
||||
add = (self.parsed_args.value != None and self.parsed_args.unset == False)
|
||||
|
||||
section = self._get_key(self.configobj, _section, add)
|
||||
if not section:
|
||||
return 1
|
||||
|
||||
opt = self._get_key(self.configobj[section], _opt, add)
|
||||
if not opt:
|
||||
return 1
|
||||
|
||||
self.section = section
|
||||
self.opt = opt
|
||||
|
||||
return 0
|
||||
|
||||
def _run(self):
|
||||
if self.check_opt() != 0:
|
||||
return 1
|
||||
|
||||
if self.parsed_args.unset:
|
||||
return self.unset()
|
||||
|
||||
if self.parsed_args.value == None:
|
||||
return self.show()
|
||||
|
||||
return self.set()
|
||||
|
||||
def run(self):
|
||||
with DvcLock(self.is_locker, self.git):
|
||||
return self._run()
|
||||
|
||||
Reference in New Issue
Block a user