Metadata-Version: 2.5
Name: tfbrief
Version: 0.1.8
Summary: Terraform plan formatter for CI logs and Markdown pull-request summaries — dependency-free stdlib CLI for terraform show -json
Project-URL: Homepage, https://github.com/infroware/tfbrief
Project-URL: Repository, https://github.com/infroware/tfbrief
Project-URL: Issues, https://github.com/infroware/tfbrief/issues
Project-URL: PyPI, https://pypi.org/project/tfbrief/
Author: infroware
License-Expression: MIT
License-File: LICENSE
Keywords: ci-cd,dependency-free,devops,github-actions,iac,infrastructure-as-code,markdown,opentofu,plan-review,pull-request,stdlib,terraform,terraform-plan,terraform-show-json,tfbrief
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: System :: Systems Administration
Requires-Python: >=3.10
Description-Content-Type: text/markdown

# TfBrief

[![PyPI version](https://img.shields.io/pypi/v/tfbrief.svg?logo=pypi&logoColor=white&label=PyPI&cacheSeconds=300&pkg_version=0.1.8)](https://pypi.org/project/tfbrief/)
[![Python](https://img.shields.io/pypi/pyversions/tfbrief.svg?logo=python&logoColor=white&cacheSeconds=300&pkg_version=0.1.8)](https://pypi.org/project/tfbrief/)

**TfBrief** is a **dependency-free** Python package that formats [`terraform show -json`](https://developer.hashicorp.com/terraform/cli/commands/show) into **human-readable CI logs** and **Markdown for pull-request reviews**: summary panels, resource actions, and attribute diffs—no jq scripts and no third-party runtime deps (stdlib only).

- **Terraform / OpenTofu plan formatter** for pipelines and **PR comments**
- **`--format markdown`** for `GITHUB_STEP_SUMMARY` and issue comments
- **`pip install tfbrief`** — one pipe on stdout; optional **`--has-changes`** apply gate

**Canonical docs:** this README on [GitHub `main`](https://github.com/infroware/tfbrief/blob/main/README.md). [PyPI](https://pypi.org/project/tfbrief/) copies it on each **release** (push a `v*.*.*` tag after merging doc changes).

**Install from PyPI:** [pypi.org/project/tfbrief](https://pypi.org/project/tfbrief/)

<!-- tfbrief-demo:start -->
<details open>
<summary><strong>Example output</strong> (fixture <a href="https://github.com/infroware/tfbrief/blob/v0.1.8/fixtures/minimal-plan.json"><code>minimal-plan.json</code></a>)</summary>

<div style="width:100%;overflow-x:auto">
  <img src="https://raw.githubusercontent.com/infroware/tfbrief/refs/tags/v0.1.8/docs/demo.svg" alt="TfBrief terminal output: summary, resource actions, and attribute diffs" style="display:block;width:auto;max-width:none;height:auto;background:#000000;border-radius:6px"/>
</div>

</details>
<!-- tfbrief-demo:end -->

## Why TfBrief?

Hashicorp’s JSON plan is complete but awkward in **pull request checks** and **pipeline logs**. TfBrief is a small Python CLI focused on **infrastructure-as-code** workflows: operators see what will **create, update, or delete** before apply, without a JSON viewer or custom jq scripts.

Typical fit: **Terraform** modules in **GitHub Actions**, **GitLab CI**, or any job that already runs `terraform plan -out=tfplan`.

### OpenTofu

[OpenTofu](https://opentofu.org/) uses the same JSON plan shape as Terraform. Pipe **`tofu show -json tfplan`** into TfBrief the same way as `terraform show -json`.

## Install

```bash
pip install tfbrief
```

Requires Python 3.10+. The `tfbrief` command is on your PATH after install.

**Contributors** (editable install from a clone):

```bash
pip install -e .
```

## Usage

After `terraform plan -out=tfplan`, pipe the JSON plan into TfBrief:

```bash
terraform show -json tfplan | tfbrief
```

That prints the summary panel, resource table, and attribute diffs (see example above).

### Markdown (pull request summaries)

For PR comments or `GITHUB_STEP_SUMMARY`, emit Markdown instead of ANSI terminal output:

```bash
terraform show -json tfplan | tfbrief --format markdown
```

Short form: `--format=markdown` or `--format md`.

**Saving Markdown to a file on Windows:** In [Windows PowerShell 5.1](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_redirection), `>` / `>>` use Unicode (often UTF-16 LE), not the process stdout encoding — `PYTHONIOENCODING` alone does not fix that. Prefer one of:

```powershell
Get-Content plan.json | python -m tfbrief --format markdown | Out-File -Encoding utf8 plan-summary.md
```

```bash
# Git Bash or WSL
terraform show -json tfplan | tfbrief --format markdown > plan-summary.md
```

See [about_Character_Encoding](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_character_encoding). Linux runners (`GITHUB_STEP_SUMMARY`, `>>`) use UTF-8 by default.

**Markdown in another repo:** copy [docs/examples/pr-plan-summary.yml](docs/examples/pr-plan-summary.yml) into `.github/workflows/`. The default install is **`pip install tfbrief`** from PyPI; optional env vars install from a Git ref for prerelease testing.

### Optional: `--has-changes` (apply gate)

Same JSON input, different output — **no** formatted report. Stdout is only `true` or `false` so scripts can skip apply when the plan is empty:

```bash
terraform show -json tfplan | tfbrief --has-changes
# true
```

Plans with only `no-op` / `read` actions (and no output changes) print `false`. Resource or output changes print `true`. Invalid JSON prints an error to stderr and exits `1`.

Typical pattern — log for humans, gate for apply:

```bash
terraform plan -out=tfplan
terraform show -json tfplan | tfbrief
if [ "$(terraform show -json tfplan | tfbrief --has-changes)" = "true" ]; then
  terraform apply tfplan
fi
```

You do **not** need `--has-changes` if you only want readable CI logs or apply manually.

### GitHub Actions

**Composite action** (no hand-rolled `pip install`):

```yaml
- run: terraform plan -out=tfplan
- run: terraform show -json tfplan > plan.json

- uses: infroware/tfbrief/.github/actions/tfbrief@v0.1.8
  with:
    plan-path: plan.json

- uses: infroware/tfbrief/.github/actions/tfbrief@v0.1.8
  with:
    plan-path: plan.json
    format: markdown
    append-summary: "true"

- id: gate
  uses: infroware/tfbrief/.github/actions/tfbrief@v0.1.8
  with:
    plan-path: plan.json
    has-changes: "true"

- if: steps.gate.outputs.has-changes == 'true'
  run: terraform apply tfplan
```

Pin the `@v0.1.8` ref to a release tag. Override `package: tfbrief==0.1.8` if you need a specific PyPI version.

**Shell one-liner:**

```yaml
- uses: actions/setup-python@v5
  with:
    python-version: "3.12"
- run: pip install -q tfbrief && terraform show -json tfplan | tfbrief
```

Post a Markdown plan summary on pull requests: use [docs/examples/pr-plan-summary.yml](docs/examples/pr-plan-summary.yml). It appends to `GITHUB_STEP_SUMMARY`, **updates one PR comment** (marker `<!-- tfbrief-plan-summary:v1 -->`), skips comments on **fork PRs** (read-only token), and tolerates comment permission errors.

## Security

TfBrief redacts values when:

- Terraform marks them in `before_sensitive` / `after_sensitive`, or
- The attribute path looks secret-like (`password`, `token`, `api_key`, `client_secret`, `db_master_password`, nested segments, and similar).

Set **`TFBRIEF_REDACT_PATTERNS`** to a comma-separated list of extra regexes (attribute path or segment) for custom names, e.g. `internal_id,^.+\.vault\.`.

Redaction is **best-effort** (naming heuristics miss custom fields). Treat all CI and PR output as sensitive; use Terraform `sensitive = true` on values that must not appear in logs.

## License

MIT — see [LICENSE](LICENSE).