1{-# LANGUAGE LambdaCase #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Obelisk.ExecutableConfig.Lookup where
5import Control.Exception (bracket)
6import Control.Monad (forM)
7import Data.ByteString (ByteString)
8import qualified Data.ByteString as BS
10import qualified Data.Map as Map
11import Data.Text (Text)
12import qualified Data.Text.Encoding as T
13import Foreign.C.String (withCString)
14import Foreign.Ptr (nullPtr)
16import Obelisk.ExecutableConfig.Internal.AssetManager
18getFromMgr :: AAssetManager -> BS.ByteString -> IO (Maybe ByteString)
19getFromMgr mgr name = do
21 a <- BS.useAsCString name $ \fn ->
22 assetManager_open mgr fn 3
23 return $ if unAAsset a == nullPtr
26 close = mapM_ asset_close
27 bracket open close $ mapM $ \asset -> do
28 b <- asset_getBuffer asset
29 l <- asset_getLength asset
30 BS.packCStringLen (b, fromIntegral l)
32getConfigs :: IO (Map Text ByteString)
33getConfigs = fmap (Map.mapKeys T.decodeUtf8) $ bracket getAssets freeAssetManager $ \mgrObj -> do
34 mgr <- assetManagerFromJava mgrObj
36 d <- withCString "config.files" $ \fn ->
37 assetManager_open mgr fn 3
38 return $ if unAAsset d == nullPtr
41 closeDir = mapM_ asset_close
42 configPaths <- bracket openDir closeDir $ \case
44 b <- asset_getBuffer asset
45 l <- asset_getLength asset
46 lines0 <$> BS.packCStringLen (b, fromIntegral l)
47 Nothing -> error "could not open configuration manifest 'config.files'"
48 result <- fmap Map.fromList $ forM configPaths $ \fp ->
49 getFromMgr mgr ("config/" <> fp) >>= \case
50 Just v -> return (fp, v)
51 Nothing -> error $ "Config present in config.files but not in assets: " <> show fp
52 putStrLn $ "getConfigs: found " <> show result
55lines0 :: BS.ByteString -> [BS.ByteString]
58 | otherwise = case BS.elemIndex 0 ps of
60 Just n -> BS.take n ps : lines0 (BS.drop (n+1) ps)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list