@@ -6,28 +6,62 @@ import ProjectM36.TransactionGraph.Persist
6import ProjectM36.Transaction
6import ProjectM36.Transaction
7import ProjectM36.Base
7import ProjectM36.Base
8import System.IO
8import System.IO
9+import Options.Applicative
10+import Text.Read (readEither)
9import Data.UUID.V4 (nextRandom)
11import Data.UUID.V4 (nextRandom)
10import qualified Data.Text as T
12import qualified Data.Text as T
18+-no arguments indicates to run without persistence
19+tutd -d /database-directory -p MinimalPersistence
20+-persist to the database directory
22+parseArgs :: Parser InterpreterConfig
23+parseArgs = InterpreterConfig <$> parsePersistenceStrategy
25+parsePersistenceStrategy :: Parser PersistenceStrategy
26+parsePersistenceStrategy = option (eitherReader readEither)
28+ long "persistence-strategy" <>
29+ metavar "strategy" <>
30+ value NoPersistence <>
34+configSetup :: InterpreterConfig -> IO (TransactionGraph)
35+configSetup config = do
36+ let failHard msg = do
39+ firstGraphUUID <- nextRandom
40+ let bootstrapGraph = bootstrapTransactionGraph firstGraphUUID dateExamples
41+ case persistenceStrategy config of
42+ --create date examples graph for now- probably should be empty context in the future
43+ NoPersistence -> return bootstrapGraph
44+ MinimalPersistence dbdir -> do
45+ err <- setupDatabaseDir dbdir bootstrapGraph
47+ Just err' -> failHard ("Failed to configure database: " ++ show err')
49+ graph <- transactionGraphLoad dbdir emptyTransactionGraph
51+ Left err' -> failHard ("Failed to load graph: " ++ show err')
52+ Right graph' -> return graph'
54+opts :: ParserInfo InterpreterConfig
55+opts = info parseArgs idm
12main :: IO ()
57main :: IO ()
14− freshUUID <- nextRandom
59+ interpreterConfig <- execParser opts
15 let currentHeadName = "master"
60 let currentHeadName = "master"
16− --if the database is empty, bootstrap it with the bootstrap graph
61+ freshGraph <- configSetup interpreterConfig
17− bootstrapGraph = bootstrapTransactionGraph freshUUID dateExamples
18− interpreterConfig = InterpreterConfig { persistenceStrategy = MinimalPersistence,
19− databaseDirectory = "/tmp/testdb" }
20− dbdir = databaseDirectory interpreterConfig
21− err <- setupDatabaseDir dbdir bootstrapGraph
23− Just err' -> hPutStrLn stderr $ "Database setup failed: " ++ show err'
25− freshGraph <- transactionGraphLoad dbdir emptyTransactionGraph
27− Left err'' -> hPutStrLn stderr $ "Database load failed: " ++ show err''
28− Right freshGraph' -> case transactionForHead currentHeadName freshGraph' of
62+ case transactionForHead currentHeadName freshGraph of
29− Nothing -> hPutStrLn stderr $ "Failed to find head transaction for " ++ T.unpack currentHeadName ++ "."
63+ Nothing -> hPutStrLn stderr $ "Failed to find head transaction for " ++ T.unpack currentHeadName ++ "."
30− Just headTransaction -> do
64+ Just headTransaction -> do
31− let currentTransaction = newDisconnectedTransaction (transactionUUID headTransaction) (transactionContext headTransaction)
65+ let currentTransaction = newDisconnectedTransaction (transactionUUID headTransaction) (transactionContext headTransaction)
32− reprLoop interpreterConfig currentTransaction freshGraph'
66+ reprLoop interpreterConfig currentTransaction freshGraph