From ed10e83bff7731ca0e661970051fbce3b44d40c5 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Mon, 23 Dec 2013 19:41:28 -0500 Subject: [PATCH] Use a global configuration file (again). --- src/OptionalConfiguration.hs | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/OptionalConfiguration.hs b/src/OptionalConfiguration.hs index 69ea041..3b663d0 100644 --- a/src/OptionalConfiguration.hs +++ b/src/OptionalConfiguration.hs @@ -28,12 +28,14 @@ import Data.Data ( Data ) import Data.Maybe ( fromMaybe ) import Data.Monoid ( Monoid(..) ) import Data.Typeable ( Typeable ) +import Paths_htsn ( getSysconfDir ) import System.Directory ( getHomeDirectory ) import System.FilePath ( () ) import System.IO.Error ( catchIOError ) import System.Log ( Priority(..) ) -import Logging ( log_error ) +import Logging ( log_error ) -- Can't import report_error from Main +import Terminal ( display_error ) -- 'cause of circular imports. import TSN.FeedHosts ( FeedHosts(..) ) @@ -131,26 +133,29 @@ instance DCT.Configured Priority where convert _ = Nothing --- | Obtain an OptionalConfiguration from the file ".htsnrc" in the --- user's home directory. +-- | Obtain an OptionalConfiguration from htsnrc in either the global +-- configuration directory or the user's home directory. The one in +-- $HOME is prefixed by a dot so that it is hidden. -- -- We make an attempt at cross-platform compatibility; we will try --- to find the correct directory even on Windows. But if the call --- to getHomeDirectory fails for whatever reason, we fall back to --- using the environment variable $HOME. +-- to find the correct directory even on Windows. But if the calls +-- to getHomeDirectory/getSysconfDir fail for whatever reason, we +-- fall back to using the Unix-specific /etc and $HOME. -- from_rc :: IO OptionalConfiguration from_rc = do - -- After some thought, the "global" /etc/htsnrc configuration file - -- was left out. Since each config file needs a password, and this - -- should be run by a dedicated user anyway, the global file does - -- not serve much purpose. It could also be a security risk (visible - -- password) if the admin screws up. + etc <- catchIOError getSysconfDir (\e -> do + display_error (show e) + log_error (show e) + return "/etc") home <- catchIOError getHomeDirectory (\e -> do + display_error (show e) log_error (show e) return "$(HOME)") + let global_config_path = etc "htsnrc" let user_config_path = home ".htsnrc" - cfg <- DC.load [ DC.Optional user_config_path ] + cfg <- DC.load [ DC.Optional global_config_path, + DC.Optional user_config_path ] cfg_daemonize <- DC.lookup cfg "daemonize" cfg_feed_hosts <- DC.lookup cfg "feed_hosts" cfg_log_file <- DC.lookup cfg "log_file" -- 2.43.2