diff --git a/dotdrop/config.py b/dotdrop/config.py index 560d453..ebd6a70 100644 --- a/dotdrop/config.py +++ b/dotdrop/config.py @@ -87,6 +87,8 @@ class Cfg: @profile: chosen profile @debug: enable debug """ + if not cfgpath: + raise ValueError('config file path undefined') if not os.path.exists(cfgpath): raise ValueError('config file does not exist: {}'.format(cfgpath)) # make sure to have an absolute path to config file diff --git a/dotdrop/options.py b/dotdrop/options.py index 15148a6..4c9c2c7 100644 --- a/dotdrop/options.py +++ b/dotdrop/options.py @@ -20,6 +20,7 @@ ENV_CONFIG = 'DOTDROP_CONFIG' ENV_NOBANNER = 'DOTDROP_NOBANNER' ENV_DEBUG = 'DOTDROP_DEBUG' ENV_NODEBUG = 'DOTDROP_FORCE_NODEBUG' +ENV_XDG = 'XDG_CONFIG_HOME' BACKUP_SUFFIX = '.dotdropbak' PROFILE = socket.gethostname() @@ -27,8 +28,7 @@ if ENV_PROFILE in os.environ: PROFILE = os.environ[ENV_PROFILE] CONFIG = 'config.yaml' -if ENV_CONFIG in os.environ: - CONFIG = os.environ[ENV_CONFIG] +HOMECFG = '~/.config/dotdrop' BANNER = """ _ _ _ __| | ___ | |_ __| |_ __ ___ _ __ @@ -54,7 +54,7 @@ Usage: Options: -p --profile= Specify the profile to use [default: {}]. - -c --cfg= Path to the config [default: {}]. + -c --cfg= Path to the config. -C --file= Path of dotfile to compare. -i --ignore= Pattern to ignore. -o --dopts= Diff options [default: ]. @@ -72,7 +72,7 @@ Options: -v --version Show version. -h --help Show this screen. -""".format(BANNER, PROFILE, CONFIG) +""".format(BANNER, PROFILE) class AttrMonitor: @@ -105,7 +105,7 @@ class Options(AttrMonitor): if ENV_NODEBUG in os.environ: self.debug = False self.profile = self.args['--profile'] - self.confpath = os.path.expanduser(self.args['--cfg']) + self.confpath = self._get_config_path() if self.debug: self.log.dbg('config file: {}'.format(self.confpath)) @@ -120,6 +120,35 @@ class Options(AttrMonitor): # start monitoring for bad attribute 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): """print the header""" self.log.log(BANNER)