From 9b2a5f56e16517f0cfa95274dbc20c7dc1cb2d1f Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 23 Sep 2011 03:53:14 +0000 Subject: [PATCH] only the add and web commands auto-create the journal file --- MANUAL.md | 10 ++++----- hledger-lib/Hledger/Read.hs | 32 ++++++++++++++++++++--------- hledger-web/Hledger/Web/Handlers.hs | 3 ++- hledger/Hledger/Cli/Main.hs | 3 ++- tests/no-such-file.test | 4 ++-- 5 files changed, 32 insertions(+), 20 deletions(-) diff --git a/MANUAL.md b/MANUAL.md index 44cbb2bc7..08d3deec4 100644 --- a/MANUAL.md +++ b/MANUAL.md @@ -81,9 +81,7 @@ Most [commands](#commands) query or operate on a [journal file](#the-journal-file), which by default is `.hledger.journal` in your home directory. You can specify a different file with the `-f` option or `LEDGER_FILE` environment variable, or standard input with `-f --`. If the journal file does not exist, an empty one will be -created. Aside from this, only the `add` and `web` commands can modify the -journal. +-`. Options are similar across most commands, with some variations; use `hledger COMMAND --help` for details. Most options must appear somewhere @@ -93,13 +91,13 @@ Arguments are also command-specific, but usually they are [filter patterns](#filter-patterns) which select a subset of the journal, eg transactions in a certain account. -To get started quickly, run `hledger add` and follow the prompts to enter -some transactions. Or, save this +To create an initial journal, run `hledger add` and follow the prompts to +enter some transactions. Or, save this [sample file](http://joyful.com/repos/hledger/data/sample.journal) as `.hledger.journal` in your home directory. Now try commands like these: $ hledger # show available commands - $ hledger add # add some new transactions to the journal file + $ hledger add # add more transactions to the journal file $ hledger balance # all accounts with aggregated balances $ hledger balance --help # show help for balance command $ hledger balance --depth 1 # only top-level accounts diff --git a/hledger-lib/Hledger/Read.hs b/hledger-lib/Hledger/Read.hs index b3191453f..2c2abb6b3 100644 --- a/hledger-lib/Hledger/Read.hs +++ b/hledger-lib/Hledger/Read.hs @@ -16,7 +16,9 @@ module Hledger.Read ( myTimelog, someamount, journalenvvar, - journaldefaultfilename + journaldefaultfilename, + requireJournalFile, + ensureJournalFile, ) where import Control.Monad.Error @@ -25,6 +27,7 @@ import Data.List import Safe (headDef) import System.Directory (doesFileExist, getHomeDirectory) import System.Environment (getEnv) +import System.Exit (exitFailure) import System.FilePath (()) import System.IO (IOMode(..), withFile, stderr) import Test.HUnit @@ -36,8 +39,8 @@ import Hledger.Data.Journal (nullctx) import Hledger.Read.JournalReader as JournalReader import Hledger.Read.TimelogReader as TimelogReader import Hledger.Utils -import Prelude hiding (getContents) -import Hledger.Utils.UTF8 (getContents, hGetContents) +import Prelude hiding (getContents, writeFile) +import Hledger.Utils.UTF8 (getContents, hGetContents, writeFile) journalenvvar = "LEDGER_FILE" @@ -91,21 +94,30 @@ journalFromPathAndString format fp s = do readJournalFile :: Maybe String -> FilePath -> IO (Either String Journal) readJournalFile format "-" = getContents >>= journalFromPathAndString format "(stdin)" readJournalFile format f = do - ensureJournalFile f + requireJournalFile f withFile f ReadMode $ \h -> hGetContents h >>= journalFromPathAndString format f --- | Ensure there is a journal at the given file path, creating an empty one if needed. +-- | If the specified journal file does not exist, give a helpful error and quit. +requireJournalFile :: FilePath -> IO () +requireJournalFile f = do + exists <- doesFileExist f + when (not exists) $ do + hPrintf stderr "The hledger journal file \"%s\" was not found.\n" f + hPrintf stderr "Please create it first, eg with hledger add, hledger web, or a text editor.\n" + hPrintf stderr "Or, specify an existing journal file with -f or LEDGER_FILE.\n" + exitFailure + +-- | Ensure there is a journal file at the given path, creating an empty one if needed. ensureJournalFile :: FilePath -> IO () ensureJournalFile f = do exists <- doesFileExist f when (not exists) $ do - hPrintf stderr "No journal file \"%s\", creating it.\n" f - hPrintf stderr "Edit this file or use \"hledger add\" or \"hledger web\" to add transactions.\n" - emptyJournal >>= writeFile f + hPrintf stderr "Creating hledger journal file \"%s\".\n" f + newJournalContent >>= writeFile f -- | Give the content for a new auto-created journal file. -emptyJournal :: IO String -emptyJournal = do +newJournalContent :: IO String +newJournalContent = do d <- getCurrentDay return $ printf "; journal created %s by hledger\n\n" (show d) diff --git a/hledger-web/Hledger/Web/Handlers.hs b/hledger-web/Hledger/Web/Handlers.hs index e38306316..fcf595a01 100644 --- a/hledger-web/Hledger/Web/Handlers.hs +++ b/hledger-web/Hledger/Web/Handlers.hs @@ -515,7 +515,8 @@ handleAdd = do Right t -> do let t' = txnTieKnot t -- XXX move into balanceTransaction - liftIO $ appendToJournalFile journalpath $ showTransaction t' + liftIO $ do ensureJournalFile journalpath + appendToJournalFile journalpath $ showTransaction t' -- setMessage $ toHtml $ (printf "Added transaction:\n%s" (show t') :: String) setMessage [$hamlet|Added transaction:
#{chomp $ show t'}
|] redirectParams RedirectTemporary RegisterR [("add","1")] diff --git a/hledger/Hledger/Cli/Main.hs b/hledger/Hledger/Cli/Main.hs index 169277b89..3b5b6a6b4 100644 --- a/hledger/Hledger/Cli/Main.hs +++ b/hledger/Hledger/Cli/Main.hs @@ -47,6 +47,7 @@ import System.Exit import System.Process import Text.Printf +import Hledger (ensureJournalFile) import Hledger.Cli.Add import Hledger.Cli.Balance import Hledger.Cli.Convert @@ -72,7 +73,7 @@ main = do | "version" `in_` (rawopts_ opts) = putStrLn progversion | "binary-filename" `in_` (rawopts_ opts) = putStrLn $ binaryfilename progname | null cmd = putStr $ showModeHelp mainmode' - | cmd `isPrefixOf` "add" = showModeHelpOr addmode $ withJournalDo opts add + | cmd `isPrefixOf` "add" = showModeHelpOr addmode $ journalFilePathFromOpts opts >>= ensureJournalFile >> withJournalDo opts add | cmd `isPrefixOf` "convert" = showModeHelpOr convertmode $ convert opts | cmd `isPrefixOf` "test" = showModeHelpOr testmode $ runtests opts | any (cmd `isPrefixOf`) ["accounts","balance"] = showModeHelpOr accountsmode $ withJournalDo opts balance diff --git a/tests/no-such-file.test b/tests/no-such-file.test index d23181813..d48673142 100644 --- a/tests/no-such-file.test +++ b/tests/no-such-file.test @@ -3,10 +3,10 @@ rm -f $$; bin/hledger register -f $$; rm -f $$ >>> ->>>2 /No journal file.*creating it/ +>>>2 /journal file.*not found/ >>>=0 rm -f $$; bin/hledger balance --no-total -f $$; rm -f $$ >>> ->>>2 /No journal file.*creating it/ +>>>2 /journal file.*not found/ >>>=0