diff options
| author | tv <tv@krebsco.de> | 2026-09-30 05:02:08 +0200 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-09-30 05:08:54 +0200 |
| commit | f460d30844358ef65477457a106681dc420e6452 (patch) | |
| tree | 2a15607d18c84e48fc964824d828017b142d2541 /src/MdBlocks/CLI.hs | |
| parent | 80efc38f395b9ee70545d586ef02c68cf952be8d (diff) | |
drop support for traversing directories
Diffstat (limited to 'src/MdBlocks/CLI.hs')
| -rw-r--r-- | src/MdBlocks/CLI.hs | 59 |
1 files changed, 24 insertions, 35 deletions
diff --git a/src/MdBlocks/CLI.hs b/src/MdBlocks/CLI.hs index 94dbae6..5c2bc29 100644 --- a/src/MdBlocks/CLI.hs +++ b/src/MdBlocks/CLI.hs @@ -5,11 +5,13 @@ module MdBlocks.CLI where import qualified Data.Aeson.Encode.Pretty as JSON import qualified Data.ByteString.Lazy.Char8 as BL import qualified Data.Text.IO as T +import System.Directory (doesFileExist) +import System.Exit (die) import Options.Applicative import MdBlocks.Address -import MdBlocks.Repository +import MdBlocks.Parser import MdBlocks.Resolver import MdBlocks.Types @@ -64,45 +66,32 @@ run (List fp) = do BL.putStrLn $ JSON.encodePretty bs -run (Extract rawAddr) = - case parseAddress rawAddr of +run (Extract rawAddress) = + case parseAddress rawAddress of Left err -> - failWith err + die ("mdblocks: " ++ err) - Right addr -> - extractBlock addr + Right address -> do + isFile <- doesFileExist (addressFile address) -run (ShowMeta addr) = - putStrLn ("show not implemented yet: " ++ addr) - -extractBlock :: Address -> IO () -extractBlock addr = do - let fp = - case addr of - FileAddress file _ -> - file - - GlobalId _ -> - error "global block addresses are not supported by --extract" + if not isFile + then + die ("mdblocks: not a file: " ++ addressFile address) + else do + blocks <- loadBlocks (addressFile address) - blocks <- loadBlocks fp + case resolveBlock address blocks of + Nothing -> + die ("mdblocks: block not found: " ++ rawAddress) - case resolveBlock addr blocks of - Nothing -> - failWith - ("block not found: " ++ showAddress addr) + Just block -> + T.putStr (blockContent block) - Just block -> - T.putStr (blockContent block) - -failWith :: String -> IO () -failWith msg = do - putStrLn ("mdblocks: " ++ msg) - fail msg +run (ShowMeta addr) = + putStrLn ("show not implemented yet: " ++ addr) -showAddress :: Address -> String -showAddress (GlobalId ident) = - "#" ++ show ident -showAddress (FileAddress fp ident) = - fp ++ "#" ++ show ident +loadBlocks :: FilePath -> IO [Block] +loadBlocks fp = do + txt <- T.readFile fp + pure (parseFile fp txt) |
