Improve user directory handling
This commit is contained in:
33
src/Main.hs
33
src/Main.hs
@@ -78,8 +78,8 @@ userLicenseDir = userHomeDir <&> (++ defaultUserLicenseRelDir)
|
|||||||
|
|
||||||
writeLicense :: String -> SearchMode -> String -> IO ()
|
writeLicense :: String -> SearchMode -> String -> IO ()
|
||||||
writeLicense outfile sm name = do
|
writeLicense outfile sm name = do
|
||||||
paths <- searchPathList sm
|
content <-
|
||||||
content <- try (readLicense paths) :: IO (Either IOError String)
|
searchPathList sm >>= (try . readLicense) :: IO (Either IOError String)
|
||||||
case content of
|
case content of
|
||||||
Left e -> printReadError e
|
Left e -> printReadError e
|
||||||
Right text -> catch (writeFile outfile text) printWriteError
|
Right text -> catch (writeFile outfile text) printWriteError
|
||||||
@@ -142,24 +142,33 @@ printHelp = do
|
|||||||
|
|
||||||
listLicenses :: IO ()
|
listLicenses :: IO ()
|
||||||
listLicenses = do
|
listLicenses = do
|
||||||
hadSystem <- printLicenseDir "System" defaultSystemLicenseDir
|
|
||||||
homeDir <- userLicenseDir
|
homeDir <- userLicenseDir
|
||||||
hadUser <- printLicenseDir "User" homeDir
|
(userFound, userFiles) <- printLicenseDir "User" homeDir []
|
||||||
unless (hadSystem || hadUser) $
|
unless (null userFiles) $ putStrLn ""
|
||||||
|
(systemFound, _) <- printLicenseDir "System" defaultSystemLicenseDir userFiles
|
||||||
|
unless (userFound || systemFound) $
|
||||||
do
|
do
|
||||||
hPutStrLn stderr "Unable to read both system and user license dir"
|
hPutStrLn stderr "Unable to read both system and user license directory."
|
||||||
hPutStrLn stderr $ "System: " ++ show defaultSystemLicenseDir
|
hPutStrLn stderr $ "System: " ++ show defaultSystemLicenseDir
|
||||||
hPutStrLn stderr $ "User: " ++ show homeDir
|
hPutStrLn stderr $ "User: " ++ show homeDir
|
||||||
where
|
where
|
||||||
printLicenseDir :: String -> String -> IO Bool
|
-- Return the list of licenses found
|
||||||
printLicenseDir name dir = do
|
printLicenseDir :: String -> String -> [String] -> IO (Bool, [String])
|
||||||
|
printLicenseDir name dir overriden = do
|
||||||
res <- try (listDirectory dir) :: IO (Either IOError [String])
|
res <- try (listDirectory dir) :: IO (Either IOError [String])
|
||||||
case res of
|
case res of
|
||||||
Right files -> do
|
Right files -> do
|
||||||
putStrLn $ name ++ " license dir:"
|
unless (null files) $ putStrLn $ name ++ " licenses:"
|
||||||
mapM_ (putStrLn . (" - " ++)) files
|
mapM_
|
||||||
return True
|
( \f ->
|
||||||
Left _ -> return False
|
putStrLn $
|
||||||
|
if f `elem` overriden
|
||||||
|
then " - " ++ f ++ " [overriden]"
|
||||||
|
else " - " ++ f
|
||||||
|
)
|
||||||
|
files
|
||||||
|
return (True, files)
|
||||||
|
Left _ -> return (False, [])
|
||||||
|
|
||||||
handleArgError :: String -> IO ()
|
handleArgError :: String -> IO ()
|
||||||
handleArgError msg = hPutStrLn stderr $ "error: " ++ msg
|
handleArgError msg = hPutStrLn stderr $ "error: " ++ msg
|
||||||
|
|||||||
Reference in New Issue
Block a user