mirror of
https://github.com/simonmichael/hledger.git
synced 2024-09-20 02:37:12 +03:00
only the add and web commands auto-create the journal file
This commit is contained in:
parent
992ce48093
commit
9b2a5f56e1
10
MANUAL.md
10
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
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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|<span>Added transaction:<small><pre>#{chomp $ show t'}</pre></small>|]
|
||||
redirectParams RedirectTemporary RegisterR [("add","1")]
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user