From 12062b333c14d7c217a66b08d07f557e9748fdca Mon Sep 17 00:00:00 2001
From: Jean-Paul Calderone <exarkun@twistedmatrix.com>
Date: Wed, 7 Jun 2023 11:00:26 -0400
Subject: [PATCH] Download and list entries in an SDMF directory

---
 gbs-downloader.cabal  |  4 ++--
 list-dircap/Main.hs   | 26 +++++++++++---------------
 src/Tahoe/Download.hs |  3 +++
 3 files changed, 16 insertions(+), 17 deletions(-)

diff --git a/gbs-downloader.cabal b/gbs-downloader.cabal
index 974cad6..51ec08e 100644
--- a/gbs-downloader.cabal
+++ b/gbs-downloader.cabal
@@ -111,6 +111,7 @@ library
     , servant-client
     , servant-client-core
     , tahoe-chk
+    , tahoe-directory
     , tahoe-great-black-swamp  >=0.3 && <0.4
     , tahoe-ssk                >=0.2 && <0.3
     , text
@@ -189,14 +190,13 @@ executable list-dircap
     , gbs-downloader
     , megaparsec
     , tahoe-directory
-    , tahoe-ssk       >=0.2 && <0.3
+    , tahoe-ssk        >=0.2 && <0.3
     , text
     , yaml
 
   hs-source-dirs:   list-dircap
   default-language: Haskell2010
 
-
 test-suite gbs-downloader-test
   -- Import common warning flags.
   import:
diff --git a/list-dircap/Main.hs b/list-dircap/Main.hs
index 60d8924..a075398 100644
--- a/list-dircap/Main.hs
+++ b/list-dircap/Main.hs
@@ -3,9 +3,6 @@
 module Main where
 
 import qualified Data.ByteString as B
-
--- import qualified Data.ByteString.Char8 as C8
--- import qualified Data.ByteString.Lazy as BL
 import qualified Data.Text as T
 import Data.Yaml (decodeEither')
 import System.Environment (getArgs)
@@ -13,25 +10,24 @@ import Tahoe.Announcement (Announcements (..))
 import qualified Tahoe.Directory as TD
 import Text.Megaparsec (parse)
 
--- import Tahoe.Download (announcementToMutableStorageServer, download)
+import Tahoe.Download (announcementToMutableStorageServer, downloadDirectory)
 
 main :: IO ()
 main = do
     [announcementPath, dirReadCap] <- getArgs
     -- Load server announcements
     announcementsBytes <- B.readFile announcementPath
-    let Right (Announcements _announcements) = decodeEither' announcementsBytes
+    let Right (Announcements announcements) = decodeEither' announcementsBytes
 
     -- Accept & parse read capability
     case parse TD.pReadSDMF "<argv>" (T.pack dirReadCap) of
         Left e -> print $ "Failed to parse cap: " <> show e
-        Right r -> print r
-
--- where
---   go announcements cap = do
---       -- Download & decode the shares
---       result <- download announcements cap announcementToMutableStorageServer
-
---       -- Show the result
---       putStrLn "Your result:"
---       either print (C8.putStrLn . BL.toStrict) result
+        Right r -> go announcements r
+  where
+    go announcements cap = do
+        -- Download & decode the shares
+        result <- downloadDirectory announcements cap announcementToMutableStorageServer
+
+        -- Show the result
+        putStrLn "Your result:"
+        either print print result
diff --git a/src/Tahoe/Download.hs b/src/Tahoe/Download.hs
index 79f3bb6..dc62783 100644
--- a/src/Tahoe/Download.hs
+++ b/src/Tahoe/Download.hs
@@ -10,6 +10,7 @@ module Tahoe.Download (
     DiscoverError (..),
     discoverShares,
     download,
+    downloadDirectory,
     announcementToImmutableStorageServer,
     announcementToMutableStorageServer,
     getShareNumbers,
@@ -28,6 +29,8 @@ import qualified Data.Set as Set
 import Tahoe.Announcement (StorageServerAnnouncement)
 import Tahoe.CHK.Server (StorageServer (..), StorageServerID)
 import Tahoe.CHK.Types (ShareNum, StorageIndex)
+import Tahoe.Directory (Directory, DirectoryCapability (DirectoryCapability))
+import qualified Tahoe.Directory as Directory
 import Tahoe.Download.Internal.Capability
 import Tahoe.Download.Internal.Client
 import Tahoe.Download.Internal.Immutable
-- 
GitLab