Improve user directory handling

This commit is contained in:
2026-04-24 23:58:42 -07:00
parent 04554d48dc
commit dd8bbf2277

View File

@@ -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