Skip to content

Commit

Permalink
Allow environment variables to set CLI defaults (#755)
Browse files Browse the repository at this point in the history
* Allow environment variables to set CLI defaults

* README: document env

Signed-off-by: William Woodruff <[email protected]>

* remove test dep, cleanup

Signed-off-by: William Woodruff <[email protected]>

* CHANGELOG: record changes

Signed-off-by: William Woodruff <[email protected]>

---------

Signed-off-by: William Woodruff <[email protected]>
Co-authored-by: William Woodruff <[email protected]>
Co-authored-by: William Woodruff <[email protected]>
  • Loading branch information
3 people authored May 10, 2024
1 parent a298a9e commit 81bcdf1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ All versions prior to 0.0.9 are untracked.

## [Unreleased]

### Added

* `pip-audit` now allows some CLI flags to be configured via environment
variables ([#755](https://github.com/pypa/pip-audit/pull/755))

## [2.7.3]

### Fixed
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ with support from Google. This is not an official Google or Trail of Bits produc
* [GitHub Actions](#github-actions)
* [`pre-commit` support](#pre-commit-support)
* [Usage](#usage)
* [Environment variables](#environment-variables)
* [Exit codes](#exit-codes)
* [Dry runs](#dry-runs)
* [Examples](#examples)
Expand Down Expand Up @@ -218,6 +219,20 @@ optional arguments:
```
<!-- @end-pip-audit-help@ -->
### Environment variables
`pip-audit` allows users to configure some flags via environment variables
instead:
| Flag | Environment equivalent | Example |
| ------------------------- | --------------------------------- | ------------------------------------- |
| `--format` | `PIP_AUDIT_FORMAT` | `PIP_AUDIT_FORMAT=markdown` |
| `--vulnerability-service` | `PIP_AUDIT_VULNERABILITY_SERVICE` | `PIP_AUDIT_VULNERABILITY_SERVICE=osv` |
| `--desc` | `PIP_AUDIT_DESC` | `PIP_AUDIT_DESC=off` |
| `--progress-spinner` | `PIP_AUDIT_PROGRESS_SPINNER` | `PIP_AUDIT_PROGRESS_SPINNER=off` |
| `--output` | `PIP_AUDIT_OUTPUT` | `PIP_AUDIT_OUTPUT=/tmp/example` |
### Exit codes
On completion, `pip-audit` will exit with a code indicating its status.
Expand Down
10 changes: 5 additions & 5 deletions pip_audit/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def _parser() -> argparse.ArgumentParser: # pragma: no cover
"--format",
type=OutputFormatChoice,
choices=OutputFormatChoice,
default=OutputFormatChoice.Columns,
default=os.environ.get("PIP_AUDIT_FORMAT", OutputFormatChoice.Columns),
metavar="FORMAT",
help=_enum_help("the format to emit audit results in", OutputFormatChoice),
)
Expand All @@ -234,7 +234,7 @@ def _parser() -> argparse.ArgumentParser: # pragma: no cover
"--vulnerability-service",
type=VulnerabilityServiceChoice,
choices=VulnerabilityServiceChoice,
default=VulnerabilityServiceChoice.Pypi,
default=os.environ.get("PIP_AUDIT_VULNERABILITY_SERVICE", VulnerabilityServiceChoice.Pypi),
metavar="SERVICE",
help=_enum_help(
"the vulnerability service to audit dependencies against",
Expand All @@ -260,7 +260,7 @@ def _parser() -> argparse.ArgumentParser: # pragma: no cover
choices=VulnerabilityDescriptionChoice,
nargs="?",
const=VulnerabilityDescriptionChoice.On,
default=VulnerabilityDescriptionChoice.Auto,
default=os.environ.get("PIP_AUDIT_DESC", VulnerabilityDescriptionChoice.Auto),
help="include a description for each vulnerability; "
"`auto` defaults to `on` for the `json` format. This flag has no "
"effect on the `cyclonedx-json` or `cyclonedx-xml` formats.",
Expand All @@ -285,7 +285,7 @@ def _parser() -> argparse.ArgumentParser: # pragma: no cover
"--progress-spinner",
type=ProgressSpinnerChoice,
choices=ProgressSpinnerChoice,
default=ProgressSpinnerChoice.On,
default=os.environ.get("PIP_AUDIT_PROGRESS_SPINNER", ProgressSpinnerChoice.On),
help="display a progress spinner",
)
parser.add_argument(
Expand Down Expand Up @@ -355,7 +355,7 @@ def _parser() -> argparse.ArgumentParser: # pragma: no cover
type=Path,
metavar="FILE",
help="output results to the given file",
default="stdout",
default=os.environ.get("PIP_AUDIT_OUTPUT", "stdout"),
)
parser.add_argument(
"--ignore-vuln",
Expand Down
25 changes: 23 additions & 2 deletions test/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import Path

import pretend # type: ignore
import pytest

Expand Down Expand Up @@ -98,7 +100,7 @@ def test_plurals(capsys, monkeypatch, args, vuln_count, pkg_count, expected):
monkeypatch.setattr(pip_audit._cli, "PipSource", lambda *a, **kw: dummysource)

parser = pip_audit._cli._parser()
monkeypatch.setattr(pip_audit._cli, "_parse_args", lambda x: parser.parse_args(args))
monkeypatch.setattr(pip_audit._cli, "_parse_args", lambda *a: parser.parse_args(args))

result = [
(
Expand Down Expand Up @@ -165,7 +167,7 @@ def test_print_format(monkeypatch, vuln_count, pkg_count, skip_count, print_form
monkeypatch.setattr(pip_audit._cli, "ColumnsFormat", lambda *a, **kw: dummyformat)

parser = pip_audit._cli._parser()
monkeypatch.setattr(pip_audit._cli, "_parse_args", lambda x: parser.parse_args([]))
monkeypatch.setattr(pip_audit._cli, "_parse_args", lambda *a: parser.parse_args([]))

result = [
(
Expand Down Expand Up @@ -215,3 +217,22 @@ def test_print_format(monkeypatch, vuln_count, pkg_count, skip_count, print_form
pass

assert bool(dummyformat.format.calls) == print_format


def test_environment_variable(monkeypatch):
"""Environment variables set before execution change CLI option default."""
monkeypatch.setenv("PIP_AUDIT_DESC", "off")
monkeypatch.setenv("PIP_AUDIT_FORMAT", "markdown")
monkeypatch.setenv("PIP_AUDIT_OUTPUT", "/tmp/fake")
monkeypatch.setenv("PIP_AUDIT_PROGRESS_SPINNER", "off")
monkeypatch.setenv("PIP_AUDIT_VULNERABILITY_SERVICE", "osv")

parser = pip_audit._cli._parser()
monkeypatch.setattr(pip_audit._cli, "_parse_args", lambda *a: parser.parse_args([]))
args = pip_audit._cli._parse_args(parser, [])

assert args.desc == VulnerabilityDescriptionChoice.Off
assert args.format == OutputFormatChoice.Markdown
assert args.output == Path("/tmp/fake")
assert not args.progress_spinner
assert args.vulnerability_service == VulnerabilityServiceChoice.Osv

0 comments on commit 81bcdf1

Please sign in to comment.