mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-10 15:04:16 +00:00
dynamically find config file path for #108
This commit is contained in:
@@ -87,6 +87,8 @@ class Cfg:
|
|||||||
@profile: chosen profile
|
@profile: chosen profile
|
||||||
@debug: enable debug
|
@debug: enable debug
|
||||||
"""
|
"""
|
||||||
|
if not cfgpath:
|
||||||
|
raise ValueError('config file path undefined')
|
||||||
if not os.path.exists(cfgpath):
|
if not os.path.exists(cfgpath):
|
||||||
raise ValueError('config file does not exist: {}'.format(cfgpath))
|
raise ValueError('config file does not exist: {}'.format(cfgpath))
|
||||||
# make sure to have an absolute path to config file
|
# make sure to have an absolute path to config file
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ ENV_CONFIG = 'DOTDROP_CONFIG'
|
|||||||
ENV_NOBANNER = 'DOTDROP_NOBANNER'
|
ENV_NOBANNER = 'DOTDROP_NOBANNER'
|
||||||
ENV_DEBUG = 'DOTDROP_DEBUG'
|
ENV_DEBUG = 'DOTDROP_DEBUG'
|
||||||
ENV_NODEBUG = 'DOTDROP_FORCE_NODEBUG'
|
ENV_NODEBUG = 'DOTDROP_FORCE_NODEBUG'
|
||||||
|
ENV_XDG = 'XDG_CONFIG_HOME'
|
||||||
BACKUP_SUFFIX = '.dotdropbak'
|
BACKUP_SUFFIX = '.dotdropbak'
|
||||||
|
|
||||||
PROFILE = socket.gethostname()
|
PROFILE = socket.gethostname()
|
||||||
@@ -27,8 +28,7 @@ if ENV_PROFILE in os.environ:
|
|||||||
PROFILE = os.environ[ENV_PROFILE]
|
PROFILE = os.environ[ENV_PROFILE]
|
||||||
|
|
||||||
CONFIG = 'config.yaml'
|
CONFIG = 'config.yaml'
|
||||||
if ENV_CONFIG in os.environ:
|
HOMECFG = '~/.config/dotdrop'
|
||||||
CONFIG = os.environ[ENV_CONFIG]
|
|
||||||
|
|
||||||
BANNER = """ _ _ _
|
BANNER = """ _ _ _
|
||||||
__| | ___ | |_ __| |_ __ ___ _ __
|
__| | ___ | |_ __| |_ __ ___ _ __
|
||||||
@@ -54,7 +54,7 @@ Usage:
|
|||||||
|
|
||||||
Options:
|
Options:
|
||||||
-p --profile=<profile> Specify the profile to use [default: {}].
|
-p --profile=<profile> Specify the profile to use [default: {}].
|
||||||
-c --cfg=<path> Path to the config [default: {}].
|
-c --cfg=<path> Path to the config.
|
||||||
-C --file=<path> Path of dotfile to compare.
|
-C --file=<path> Path of dotfile to compare.
|
||||||
-i --ignore=<pattern> Pattern to ignore.
|
-i --ignore=<pattern> Pattern to ignore.
|
||||||
-o --dopts=<opts> Diff options [default: ].
|
-o --dopts=<opts> Diff options [default: ].
|
||||||
@@ -72,7 +72,7 @@ Options:
|
|||||||
-v --version Show version.
|
-v --version Show version.
|
||||||
-h --help Show this screen.
|
-h --help Show this screen.
|
||||||
|
|
||||||
""".format(BANNER, PROFILE, CONFIG)
|
""".format(BANNER, PROFILE)
|
||||||
|
|
||||||
|
|
||||||
class AttrMonitor:
|
class AttrMonitor:
|
||||||
@@ -105,7 +105,7 @@ class Options(AttrMonitor):
|
|||||||
if ENV_NODEBUG in os.environ:
|
if ENV_NODEBUG in os.environ:
|
||||||
self.debug = False
|
self.debug = False
|
||||||
self.profile = self.args['--profile']
|
self.profile = self.args['--profile']
|
||||||
self.confpath = os.path.expanduser(self.args['--cfg'])
|
self.confpath = self._get_config_path()
|
||||||
if self.debug:
|
if self.debug:
|
||||||
self.log.dbg('config file: {}'.format(self.confpath))
|
self.log.dbg('config file: {}'.format(self.confpath))
|
||||||
|
|
||||||
@@ -120,6 +120,35 @@ class Options(AttrMonitor):
|
|||||||
# start monitoring for bad attribute
|
# start monitoring for bad attribute
|
||||||
self._set_attr_err = True
|
self._set_attr_err = True
|
||||||
|
|
||||||
|
def _get_config_path(self):
|
||||||
|
"""get the config path"""
|
||||||
|
# cli provided
|
||||||
|
if self.args['--cfg']:
|
||||||
|
return os.path.expanduser(self.args['--cfg'])
|
||||||
|
|
||||||
|
# environment variable provided
|
||||||
|
if ENV_CONFIG in os.environ:
|
||||||
|
return os.path.expanduser(os.environ[ENV_CONFIG])
|
||||||
|
|
||||||
|
# look in current directory
|
||||||
|
if os.path.exists(CONFIG):
|
||||||
|
return CONFIG
|
||||||
|
|
||||||
|
# look in XDG_CONFIG_HOME
|
||||||
|
if ENV_XDG in os.environ:
|
||||||
|
cfg = os.path.expanduser(os.environ[ENV_XDG])
|
||||||
|
path = os.path.join(cfg, CONFIG)
|
||||||
|
if os.path.exists(path):
|
||||||
|
return path
|
||||||
|
|
||||||
|
# look in home .config/dotdrop directory
|
||||||
|
cfg = os.path.expanduser(HOMECFG)
|
||||||
|
path = os.path.join(cfg, CONFIG)
|
||||||
|
if os.path.exists(path):
|
||||||
|
return path
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
def _header(self):
|
def _header(self):
|
||||||
"""print the header"""
|
"""print the header"""
|
||||||
self.log.log(BANNER)
|
self.log.log(BANNER)
|
||||||
|
|||||||
Reference in New Issue
Block a user