From 5bb1fec0ba93e56691f043323dddf3bb303b5636 Mon Sep 17 00:00:00 2001 From: jao Date: Mon, 26 Nov 2018 03:31:53 +0000 Subject: Utilities for dealing with config and data directories --- app/Configuration.hs | 53 ---------------------------------------------------- app/Main.hs | 27 ++++++++++++++++++++++---- 2 files changed, 23 insertions(+), 57 deletions(-) delete mode 100644 app/Configuration.hs (limited to 'app') diff --git a/app/Configuration.hs b/app/Configuration.hs deleted file mode 100644 index 3e30730..0000000 --- a/app/Configuration.hs +++ /dev/null @@ -1,53 +0,0 @@ -{-# LANGUAGE FlexibleContexts, CPP #-} - ------------------------------------------------------------------------------- --- | --- Module: Configuration --- Copyright: (c) 2018 Jose Antonio Ortega Ruiz --- License: BSD3-style (see LICENSE) --- --- Maintainer: jao@gnu.org --- Stability: unstable --- Portability: portable --- Created: Wed Nov 21, 2018 23:13 --- --- --- Parsing configuration files --- ------------------------------------------------------------------------------- - - -module Configuration (readConfig, readDefaultConfig) where - -import Control.Monad.IO.Class (liftIO) - -import System.Environment -import System.Posix.Files (fileExist) - -import qualified Xmobar as X - --- | Reads the configuration files or quits with an error -readConfig :: FilePath -> String -> IO (X.Config,[String]) -readConfig f usage = do - let err m = error $ f ++ ": " ++ m - file <- liftIO $ fileExist f - r <- if file - then X.readConfig X.defaultConfig f - else err $ "file not found" ++ "\n" ++ usage - case r of - Left e -> err (show e) - Right res -> return res - --- | Read default configuration file or load the default config -readDefaultConfig :: String -> IO (X.Config,[String]) -readDefaultConfig usage = do - xdgConfigFile <- X.getXdgConfigFile - xdgConfigFileExists <- liftIO $ fileExist xdgConfigFile - home <- liftIO $ getEnv "HOME" - let defaultConfigFile = home ++ "/.xmobarrc" - defaultConfigFileExists <- liftIO $ fileExist defaultConfigFile - if xdgConfigFileExists - then readConfig xdgConfigFile usage - else if defaultConfigFileExists - then readConfig defaultConfigFile usage - else return (X.defaultConfig,[]) diff --git a/app/Main.hs b/app/Main.hs index b5d3993..51bfe8b 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -23,11 +23,10 @@ import System.Exit import System.Environment (getArgs) import Control.Monad (unless) import Text.Read (readMaybe) +import System.Posix.Files (fileExist) import Xmobar - import Paths_xmobar (version) -import Configuration as C (readConfig, readDefaultConfig) -- $main @@ -36,12 +35,32 @@ main :: IO () main = do (o,file) <- getArgs >>= getOpts (c,defaultings) <- case file of - [cfgfile] -> C.readConfig cfgfile usage - _ -> C.readDefaultConfig usage + [cfgfile] -> config cfgfile usage + _ -> defConfig usage unless (null defaultings || (not $ any (== Debug) o)) $ putStrLn $ "Fields missing from config defaulted: " ++ intercalate "," defaultings doOpts c o >>= xmobar +config :: FilePath -> String -> IO (Config,[String]) +config f msg = do + let err m = error $ f ++ ": " ++ m + file <- fileExist f + r <- if file + then readConfig defaultConfig f + else err $ "file not found" ++ "\n" ++ msg + case r of + Left e -> err (show e) + Right res -> return res + +-- | Read default configuration file or load the default config +defConfig :: String -> IO (Config,[String]) +defConfig msg = do + xdgConfigFile <- xmobarConfigFile + xdgConfigFileExists <- fileExist xdgConfigFile + if xdgConfigFileExists + then config xdgConfigFile msg + else return (defaultConfig,[]) + data Opts = Help | Debug | Version -- cgit v1.2.3