Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing filename via environment variable. #22

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ Usage: bean-grep [OPTIONS] [PATTERN] FILENAME...
To read from standard input, pass "-" as FILENAME, but beware that it
implies on-disk buffering of stdin.

The FILENAME can alternatively be specified via the environment variable
BEANCOUNT_FILENAME. This allows not having to specify it explicitly every
time bean-grep is run. Only a single FILENAME is allowed to be specified
this way.

All options can be set via environment variables, which is most useful if
you want to override the defaults. Each environment variable
consists of the prefix BEANGREP_ followed by the option name in uppercase.
Expand Down
21 changes: 16 additions & 5 deletions src/beangrep/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
import shutil
import sys
import os
from tempfile import NamedTemporaryFile

import beancount.loader # type: ignore
Expand Down Expand Up @@ -68,7 +69,6 @@
)
@click.argument(
"args",
required=True,
nargs=-1,
metavar="[PATTERN] FILENAME...", # override metavar to show what is required
)
Expand Down Expand Up @@ -278,10 +278,21 @@ def cli(
logging.basicConfig(level=log_level)

(pattern, filenames) = (None, [])
if len(args) == 1: # len(args) == 0 should not happen due to required=True
filenames = list(args)
elif len(args) >= 2:
(pattern, filenames) = (args[0], list(args[1:]))
# click has has support for setting arguments via environment
# variables but I can't figure out how to make it work with
# beangrep's combination of optional pattern and variadic filenames.
# So we just roll it ourselves.
if os.getenv("BEANCOUNT_FILENAME"):
filenames = [os.getenv("BEANCOUNT_FILENAME")]
if len(args) == 1:
pattern = args[0]
else:
if len(args) == 0:
raise click.BadArgumentUsage("Must specify at least one beancount file.")
elif len(args) == 1:
filenames = list(args)
elif len(args) >= 2:
(pattern, filenames) = (args[0], list(args[1:]))
if len(list(filter(lambda fname: fname == "-", filenames))) > 1:
raise click.BadArgumentUsage(
'Standard input ("-") cannot be specified multipled times'
Expand Down
25 changes: 24 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def test_errors():

# In theory any option could be set via envvars but let's just test the
# most likely choices
def test_envvars():
def test_envvars_options():
runner = CliRunner()

result = runner.invoke(
Expand All @@ -288,3 +288,26 @@ def test_envvars():
).exit_code
== 0
)


def test_envvar_beancount_file():
runner = CliRunner()

assert (
runner.invoke(
cli, env={"BEANCOUNT_FILENAME": "does_not_exist.beancount"}
).exit_code
== 2
)

result = runner.invoke(
cli, ["--amount", "=76.81 USD"], env={"BEANCOUNT_FILENAME": SAMPLE_LEDGER}
)
assert result.exit_code == 0
assert "Verizon Wireless" in result.output

result = runner.invoke(
cli, ["^a-day-in"], env={"BEANCOUNT_FILENAME": SAMPLE_LEDGER}
)
assert result.exit_code == 0
assert "Mercadito" in result.output
Loading