@@ -6,28 +6,62 @@ import ProjectM36.TransactionGraph.Persist
66import ProjectM36.Transaction
77import ProjectM36.Base
88import System.IO
9+import Options.Applicative
10+import Text.Read (readEither)
911import Data.UUID.V4 (nextRandom)
1012import qualified Data.Text as T
13+import System.Exit
1114
15+{-
16+invocation:
17+tutd
18+-no arguments indicates to run without persistence
19+tutd -d /database-directory -p MinimalPersistence
20+-persist to the database directory
21+-}
22+parseArgs :: Parser InterpreterConfig
23+parseArgs = InterpreterConfig <$> parsePersistenceStrategy
24+
25+parsePersistenceStrategy :: Parser PersistenceStrategy
26+parsePersistenceStrategy = option (eitherReader readEither)
27+ (short 'p' <>
28+ long "persistence-strategy" <>
29+ metavar "strategy" <>
30+ value NoPersistence <>
31+ showDefaultWith show
32+ )
33+
34+configSetup :: InterpreterConfig -> IO (TransactionGraph)
35+configSetup config = do
36+ let failHard msg = do
37+ hPutStrLn stderr msg
38+ exitFailure
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
46+ case err of
47+ Just err' -> failHard ("Failed to configure database: " ++ show err')
48+ Nothing -> do
49+ graph <- transactionGraphLoad dbdir emptyTransactionGraph
50+ case graph of
51+ Left err' -> failHard ("Failed to load graph: " ++ show err')
52+ Right graph' -> return graph'
53+
54+opts :: ParserInfo InterpreterConfig
55+opts = info parseArgs idm
56+
1257main :: IO ()
1358main = do
14− freshUUID <- nextRandom
59+ interpreterConfig <- execParser opts
1560 let currentHeadName = "master"
16− --if the database is empty, bootstrap it with the bootstrap graph
17− bootstrapGraph = bootstrapTransactionGraph freshUUID dateExamples
18− interpreterConfig = InterpreterConfig { persistenceStrategy = MinimalPersistence,
19− databaseDirectory = "/tmp/testdb" }
20− dbdir = databaseDirectory interpreterConfig
21− err <- setupDatabaseDir dbdir bootstrapGraph
22− case err of
23− Just err' -> hPutStrLn stderr $ "Database setup failed: " ++ show err'
24− Nothing -> do
25− freshGraph <- transactionGraphLoad dbdir emptyTransactionGraph
26− case freshGraph of
27− Left err'' -> hPutStrLn stderr $ "Database load failed: " ++ show err''
28− Right freshGraph' -> case transactionForHead currentHeadName freshGraph' of
29− Nothing -> hPutStrLn stderr $ "Failed to find head transaction for " ++ T.unpack currentHeadName ++ "."
30− Just headTransaction -> do
31− let currentTransaction = newDisconnectedTransaction (transactionUUID headTransaction) (transactionContext headTransaction)
32− reprLoop interpreterConfig currentTransaction freshGraph'
61+ freshGraph <- configSetup interpreterConfig
62+ case transactionForHead currentHeadName freshGraph of
63+ Nothing -> hPutStrLn stderr $ "Failed to find head transaction for " ++ T.unpack currentHeadName ++ "."
64+ Just headTransaction -> do
65+ let currentTransaction = newDisconnectedTransaction (transactionUUID headTransaction) (transactionContext headTransaction)
66+ reprLoop interpreterConfig currentTransaction freshGraph
3367