tf-project 0.2.1__tar.gz → 0.2.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (23) hide show
  1. {tf_project-0.2.1 → tf_project-0.2.3}/CHANGELOG.md +95 -28
  2. {tf_project-0.2.1 → tf_project-0.2.3}/PKG-INFO +67 -25
  3. {tf_project-0.2.1 → tf_project-0.2.3}/README.md +65 -23
  4. {tf_project-0.2.1 → tf_project-0.2.3}/pyproject.toml +2 -2
  5. {tf_project-0.2.1 → tf_project-0.2.3}/src/tf_project/__init__.py +1 -1
  6. tf_project-0.2.3/src/tf_project/__main__.py +4 -0
  7. {tf_project-0.2.1 → tf_project-0.2.3}/src/tf_project/__version__.py +1 -1
  8. {tf_project-0.2.1 → tf_project-0.2.3}/src/tf_project/banner.py +32 -2
  9. {tf_project-0.2.1 → tf_project-0.2.3}/src/tf_project/cli.py +112 -22
  10. {tf_project-0.2.1 → tf_project-0.2.3}/src/tf_project/commands.py +140 -23
  11. {tf_project-0.2.1 → tf_project-0.2.3}/src/tf_project/config.py +28 -3
  12. {tf_project-0.2.1 → tf_project-0.2.3}/src/tf_project/lock.py +4 -4
  13. {tf_project-0.2.1 → tf_project-0.2.3}/src/tf_project/migrate.py +5 -5
  14. {tf_project-0.2.1 → tf_project-0.2.3}/src/tf_project/models.py +3 -3
  15. {tf_project-0.2.1 → tf_project-0.2.3}/src/tf_project/preflight.py +1 -24
  16. {tf_project-0.2.1 → tf_project-0.2.3}/src/tf_project/secrets.py +2 -2
  17. {tf_project-0.2.1 → tf_project-0.2.3}/src/tf_project/self_commands.py +185 -37
  18. {tf_project-0.2.1 → tf_project-0.2.3}/src/tf_project/slots.py +1 -1
  19. {tf_project-0.2.1 → tf_project-0.2.3}/src/tf_project/state.py +1 -1
  20. {tf_project-0.2.1 → tf_project-0.2.3}/src/tf_project/terraform.py +2 -2
  21. tf_project-0.2.1/src/tf_project/__main__.py +0 -4
  22. {tf_project-0.2.1 → tf_project-0.2.3}/LICENSE +0 -0
  23. {tf_project-0.2.1 → tf_project-0.2.3}/src/tf_project/py.typed +0 -0
@@ -4,7 +4,74 @@ All notable changes to this project are documented here.
4
4
  The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
- ## [Unreleased]
7
+ ## [0.2.3] - 2026-06-26
8
+
9
+ ### Changed
10
+ - **Raised the minimum `typer` to `>=0.26.0`** (was `>=0.21.2`). That series
11
+ vendors its own click fork (`typer._click`) and drops the standalone
12
+ `click` dependency. Shell-completion callbacks are now wired through
13
+ typer's native `autocompletion=` API returning `(value, help)` tuples, so
14
+ `tfp` no longer imports the `click` package directly - matching the
15
+ environment we lock and test against.
16
+
17
+ ### Fixed
18
+ - **`tfp init` now re-binds the backend instead of reusing a stale one.**
19
+ Previously `tfp init` fell through to a bare `terraform init`, which
20
+ silently reused the backend already cached in `.terraform/` from the last
21
+ `tfp use` - so editing a banner's `state_key` / `backend_config` and
22
+ running `tfp init` left the slot pinned to the *old* remote state key
23
+ with no warning (and `tfp status` kept reporting the stale key). `init`
24
+ is now a wrapped command that re-resolves the live banner, runs
25
+ `terraform init -reconfigure` with the freshly-resolved `-backend-config`,
26
+ re-locks providers, and refreshes the saved slot state - the same backend
27
+ binding `tfp use` performs. Unlike `use`, it operates on the active slot
28
+ (honouring `-s` / `TFP_SLOT`) and does not move the active-slot pointer.
29
+ `tfp self trace init` traces the resulting argv.
30
+ - **Banner `state_key` must be an absolute, normalised pure POSIX path.**
31
+ tf-project is opinionated here: a `state_key` has to start with `/` and be
32
+ already normalised. Relative keys, backslashes, `.`/`..` segments,
33
+ redundant `//`, and trailing slashes are now rejected with a clear banner
34
+ error rather than silently rewritten - so the remote-state blob a banner
35
+ targets is unambiguous from the text alone. The one transformation applied
36
+ to a valid key is stripping its single leading slash: the azurerm backend
37
+ prepends the container name, so `/site-monitor.tfstate` previously produced
38
+ a double-slashed blob name (`.../tfstates//site-monitor.tfstate`) and now
39
+ resolves to `site-monitor.tfstate`.
40
+
41
+ ## [0.2.2] - 2026-05-24
42
+
43
+ ### Added
44
+ - **Auto-snapshot before state-mutating ops.** `tfp state rm`, `tfp state
45
+ mv`, `tfp state replace-provider`, and `tfp import` now `terraform state
46
+ pull` to `<tmp_dir>/snapshots/<slot>/<ts>-pre-<op>.tfstate` before
47
+ running - a failed pull aborts the op rather than destroying state you
48
+ can't roll back to. Recovery is `tfp self snapshot restore [<name>]`
49
+ (`terraform state push -force`, confirmation prompt; `-y` to skip), and
50
+ `tfp self snapshot list` enumerates per-slot snapshots newest first. New
51
+ `snapshot_retention` config field bounds the per-slot count (default
52
+ **200**, oldest-first pruning on every write; `0` disables auto-snapshot
53
+ entirely). `apply` is deliberately excluded - its plan/refresh cycle
54
+ already gives a recoverable diff and auto-snapshotting every apply would
55
+ dominate iteration time.
56
+
57
+ ### Changed
58
+ - **Cross-platform lockfile hashes are kept fresh automatically.** Every
59
+ `tfp use` that runs `terraform init` now chases it with `terraform
60
+ providers lock` for `linux_amd64` + `linux_arm64` (the common CI /
61
+ runtime targets) plus the host's own platform - so the committed
62
+ `.terraform.lock.hcl` is portable across the machines that actually
63
+ touch it without the user remembering to re-lock. Multi-platform teams
64
+ extend the set via the new `extra_lock_platforms` field in
65
+ `tf_project.toml` (e.g. `["windows_amd64"]`); entries are validated at
66
+ config-load time and deduped against the defaults. The previous
67
+ "lockfile missing" warning is gone - no longer reachable. `tfp self
68
+ providers lock [-p ...]` remains for ad-hoc re-locking.
69
+ - **`tfp self snapshot` is now a sub-app** (`create` / `list` / `restore`).
70
+ The old `tfp self snapshot` leaf becomes `tfp self snapshot create`;
71
+ snapshots live under `<tmp_dir>/snapshots/<slot>/` instead of
72
+ `<tmp_dir>/snapshot-<ts>.tfstate` so retention can prune per-slot.
73
+
74
+ ## [0.2.1] - 2026-05-24
8
75
 
9
76
  ### Added
10
77
  - **Recovery preflight.** Wrapped commands now bail out with an actionable
@@ -15,41 +82,41 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
15
82
  `.terraform.lock.hcl` is missing while the plugin cache is on, pointing
16
83
  at `tfp self providers lock` to repopulate cross-platform hashes.
17
84
 
18
- ## [0.2.0] — 2026-05-22
85
+ ## [0.2.0] - 2026-05-22
19
86
 
20
87
  ### Release pipeline
21
88
  - **Tag-driven releases.** The wheel's version is now derived from the git
22
89
  tag at build time via `pdm-backend`'s SCM source; `src/tf_project/__version__.py`
23
90
  reads the installed distribution's metadata at runtime. No more code-side
24
- version bump — push `vX.Y.Z` and the workflow takes over.
91
+ version bump - push `vX.Y.Z` and the workflow takes over.
25
92
  - **Sigstore-signed artifacts.** Every release attaches the `.whl`,
26
93
  `.tar.gz`, and matching `.sigstore` bundles to the GitHub Release.
27
94
  - **GitHub Releases automated.** Changelog notes are extracted from
28
95
  `## [X.Y.Z]` sections by `mindsers/changelog-reader-action`; pre-release
29
96
  tags (`rcN`, `aN`, `bN`, `devN`) are auto-flagged.
30
- - Removed the post-publish `pdm bump patch` step — the next release version
97
+ - Removed the post-publish `pdm bump patch` step - the next release version
31
98
  is whatever you tag next, not whatever a bot guessed.
32
99
 
33
- ### Added — slot model (BREAKING)
100
+ ### Added - slot model (BREAKING)
34
101
  - **Named slots replace the single-state model.** Each tfvars file is bound
35
102
  to a slot whose name defaults to the filename stem and can be overridden
36
103
  via a new banner field: `{"slot": "production"}`. State, tfplan, and
37
104
  `.terraform/` are stored per-slot under `tmp/slots/<slot>/`, so flipping
38
- between slots is now a pointer write — no `terraform init` when the slot
105
+ between slots is now a pointer write - no `terraform init` when the slot
39
106
  is already warm.
40
- - `tfp use <slot-or-path>` — sole verb for selecting a slot. Accepts a slot
107
+ - `tfp use <slot-or-path>` - sole verb for selecting a slot. Accepts a slot
41
108
  name or a path to a tfvars file (paths are convenient with shell
42
109
  completion). Runs `terraform init -reconfigure` only when the slot's
43
110
  `.terraform/` is missing or the backend signature has changed.
44
- - `tfp ls` — list all slots derived from tfvars banners. Marks the active
111
+ - `tfp ls` - list all slots derived from tfvars banners. Marks the active
45
112
  slot, shows project + backend key + warm-since mtime. `--json` for
46
113
  scripts. Orphans (slot dirs with no matching banner) are listed
47
114
  separately.
48
- - `tfp rm <slot>` — delete a slot's on-disk directory. Refuses on the
115
+ - `tfp rm <slot>` - delete a slot's on-disk directory. Refuses on the
49
116
  active slot unless `--force`. Never touches the tfvars file.
50
- - Global `-s/--slot <name>` flag — operate against a specific slot for one
117
+ - Global `-s/--slot <name>` flag - operate against a specific slot for one
51
118
  invocation, without rewriting the saved active.
52
- - `TFP_SLOT` env var — overrides the saved active for the current shell
119
+ - `TFP_SLOT` env var - overrides the saved active for the current shell
53
120
  (useful for parallel slots across terminals or in CI).
54
121
  - Banner `slot` field validated for uniqueness across `tfvars_dir/*.tfvars`
55
122
  on every `tfp use` / `tfp ls`. Conflicts name both files and whether each
@@ -58,13 +125,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
58
125
  independent provider/module/backend bindings on disk.
59
126
  - Plugin cache shared across all slots via `TF_PLUGIN_CACHE_DIR`
60
127
  (default `tmp/plugin-cache/`, configurable). Providers download once and
61
- are linked into each slot's `.terraform/providers/` — first init for a
128
+ are linked into each slot's `.terraform/providers/` - first init for a
62
129
  new slot is seconds, not minutes. `TF_PLUGIN_CACHE_MAY_BREAK_DEPENDENCY_LOCK_FILE=1`
63
130
  is set automatically; pair with `tfp self providers lock` to populate
64
131
  multi-platform hashes for CI.
65
- - `tfp self providers lock [-p <platform>]` — wraps `terraform providers lock`
132
+ - `tfp self providers lock [-p <platform>]` - wraps `terraform providers lock`
66
133
  across `linux/darwin × amd64/arm64` by default.
67
- - `tfp self state show --all` / `tfp self state clear --all` — operate on
134
+ - `tfp self state show --all` / `tfp self state clear --all` - operate on
68
135
  every slot at once.
69
136
  - `tfp self doctor` adds slot-uniqueness and orphan checks.
70
137
  - Apply-safety hashes now also pin `project`, `state_key`, full
@@ -78,9 +145,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
78
145
  `tmp/slots/<slot>/state.json` on first run, with a breadcrumb left at
79
146
  `tmp/MIGRATED-<UTC-timestamp>.txt`.
80
147
 
81
- ### Changed — slot model (BREAKING)
148
+ ### Changed - slot model (BREAKING)
82
149
  - `tfp init <tfvars>` is **removed**. Use `tfp use <slot-or-path>` instead.
83
- - Banner `env` is no longer merged across `tfp use` invocations — the
150
+ - Banner `env` is no longer merged across `tfp use` invocations - the
84
151
  banner is the single source of truth at the moment of switching.
85
152
  - `tfp last` / `tfp status` / `tfp self state show` / `tfp self trace` /
86
153
  `tfp self snapshot` / `tfp self lock` are all slot-scoped. They consult
@@ -94,11 +161,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
94
161
  re-resolve.
95
162
 
96
163
  ### Added
97
- - `tfp state` subcommand group — all eight terraform state subcommands
164
+ - `tfp state` subcommand group - all eight terraform state subcommands
98
165
  (`list`, `show`, `mv`, `rm`, `pull`, `push`, `replace-provider`,
99
166
  `identities`) wrapped and discoverable in `tfp --help`. Each forwards
100
167
  `-chdir` + the saved env.
101
- - `tfp import <ADDRESS> <ID>` — promoted from raw passthrough to a wrapped
168
+ - `tfp import <ADDRESS> <ID>` - promoted from raw passthrough to a wrapped
102
169
  subcommand. Forwards the decrypted tfvars (`-var-file`) and the saved
103
170
  env so provider blocks that reference variables can resolve. Extra
104
171
  terraform flags after the positional args still passthrough.
@@ -111,13 +178,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
111
178
  backend-level lease break only if the polite path fails or the lock ID
112
179
  isn't recoverable. Pass `--blunt` to skip terraform entirely. Refuses
113
180
  to escalate to a backend-level break when no lock is present.
114
- - `tfp self snapshot` — pulls the remote tfstate to a local file via
181
+ - `tfp self snapshot` - pulls the remote tfstate to a local file via
115
182
  `terraform state pull`. Defaults to `<tmp_dir>/snapshot-<UTC-timestamp>.tfstate`.
116
- - `tfp self trace <subcommand>` — prints the exact terraform argv `tfp <subcommand>`
183
+ - `tfp self trace <subcommand>` - prints the exact terraform argv `tfp <subcommand>`
117
184
  would build, without invoking anything. Useful when copy-pasting a
118
185
  command to debug outside the wrapper. Tracing `use` works against a
119
- fresh slot — no saved state required.
120
- - `tfp last` — prints the most recent terraform invocation recorded by
186
+ fresh slot - no saved state required.
187
+ - `tfp last` - prints the most recent terraform invocation recorded by
121
188
  tfp (argv + exit code + timestamp). Persisted per-slot at
122
189
  `tmp/slots/<slot>/last.json`.
123
190
  - Tab-completion for `tfp use` (slot names + tfvars paths) and
@@ -141,11 +208,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
141
208
  Azure-specific `lease_state` / `lease_duration` fields are folded into
142
209
  `detail`.
143
210
  - Tfplan meta sidecar field renamed from `tfvars_sha256` to
144
- `inputs_sha256`. The legacy field name is **not** accepted on read —
211
+ `inputs_sha256`. The legacy field name is **not** accepted on read -
145
212
  saved plans from pre-rename versions must be re-created with
146
213
  `tfp plan`.
147
- - `tfp status` — one-line summary of the active slot.
148
- - `tfp self banner check <tfvars>` — validates a banner and prints the
214
+ - `tfp status` - one-line summary of the active slot.
215
+ - `tfp self banner check <tfvars>` - validates a banner and prints the
149
216
  resolved `source_root` / `backend_config` / `env` / `slot` without
150
217
  running terraform.
151
218
  - Global `--verbose` and `--dry-run` flags. `--verbose` logs the terraform
@@ -156,14 +223,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
156
223
  - Real-terraform end-to-end integration test covering use→plan→apply→destroy
157
224
  against a `null_resource` fixture, plus a two-slot warm-switch test.
158
225
  - `BannerError` for clean, file-pathed banner validation failures.
159
- - `py.typed` marker — the package ships type information explicitly.
226
+ - `py.typed` marker - the package ships type information explicitly.
160
227
  - Terraform exit codes propagate cleanly (no Python traceback when
161
228
  terraform fails). `terraform` not found exits 127.
162
229
  - SIGINT during a running terraform command no longer kills Python first;
163
230
  terraform receives the signal via the shared process group and we wait
164
231
  for it to finish.
165
232
  - Passthrough invocations (`tfp <unknown-subcommand>`) `os.execvpe` the
166
- terraform process directly — native signal handling and exit code.
233
+ terraform process directly - native signal handling and exit code.
167
234
  `tfp -s <slot> <unknown>` refuses if the slot has no saved state, rather
168
235
  than silently dropping `-chdir`.
169
236
  - `state.save_state` takes an exclusive file lock so parallel `tfp use`
@@ -176,7 +243,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
176
243
  - `tfp state-mv` top-level command. Use `tfp state mv` instead.
177
244
  - `commands.do_state_mv` public helper. Use `commands.do_state(subcommand="mv", ...)`.
178
245
 
179
- ## [0.1.0] — 2026-05-14
246
+ ## [0.1.0] - 2026-05-14
180
247
 
181
248
  ### Added
182
249
  - Initial release. `tf-project` (aka `tfp`) CLI wrapping `terraform`
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tf-project
3
- Version: 0.2.1
3
+ Version: 0.2.3
4
4
  Summary: Terraform / OpenTofu wrapper with named per-tfvars slots, cached `.terraform/`, 1Password secret injection, and apply-safety guards.
5
5
  Keywords: terraform,opentofu,tofu,infrastructure-as-code,iac,devops,cli,wrapper,tfvars,multi-environment,workspaces,1password,op-cli,secrets,sops,azurerm,s3-backend,terraform-state,sigstore
6
6
  Author-Email: Ilja Orlovs <ilja@release.art>
@@ -29,7 +29,7 @@ Project-URL: Changelog, https://github.com/release-art/tf-project/blob/main/CHAN
29
29
  Project-URL: Issues, https://github.com/release-art/tf-project/issues
30
30
  Project-URL: Releases, https://github.com/release-art/tf-project/releases
31
31
  Requires-Python: >=3.11
32
- Requires-Dist: typer>=0.21.2
32
+ Requires-Dist: typer>=0.26.0
33
33
  Requires-Dist: pydantic>=2.13.4
34
34
  Description-Content-Type: text/markdown
35
35
 
@@ -41,7 +41,7 @@ Description-Content-Type: text/markdown
41
41
 
42
42
  A thin, opinionated **Terraform / OpenTofu** wrapper for multi-environment
43
43
  projects. Provides a single `tf-project` (aka `tfp`) CLI built around
44
- **named slots** — each tfvars file binds to a slot, and switching between
44
+ **named slots** - each tfvars file binds to a slot, and switching between
45
45
  slots is a pointer write, not a re-`terraform init`.
46
46
 
47
47
  If you maintain one tfvars per environment (`dev.tfvars`, `staging.tfvars`,
@@ -52,14 +52,14 @@ time you switch, this tool replaces that ritual with `tfp use dev`.
52
52
  tfvars files. `tfp use dev` flips the active slot; `tfp plan` / `tfp apply`
53
53
  operate against it.
54
54
  - **One `.terraform/` per slot** via `TF_DATA_DIR`. Two slots pointing at the
55
- same project keep independent provider/module/backend bindings on disk —
55
+ same project keep independent provider/module/backend bindings on disk -
56
56
  swapping between them is instant when both are warm.
57
57
  - **Shared provider cache** via `TF_PLUGIN_CACHE_DIR`. Providers download
58
58
  once and link into every slot's `.terraform/providers/`.
59
59
  - **Versioned, pydantic-validated state files.** Every persisted JSON carries
60
60
  `schema_version`; unknown fields fail loudly (except in user-authored
61
61
  banners, where they are tolerated and logged).
62
- - Pluggable tfvars preprocessing — defaults to [1Password's `op inject`][op]
62
+ - Pluggable tfvars preprocessing - defaults to [1Password's `op inject`][op]
63
63
  so you can keep `op://...` references in tfvars committed to git.
64
64
 
65
65
  [op]: https://developer.1password.com/docs/cli/secrets-template-syntax/
@@ -79,8 +79,8 @@ cd path/to/your-terraform-repo
79
79
  tfp self init
80
80
  ```
81
81
 
82
- This drops a `tf_project.toml` at the repo root, or — if a `pyproject.toml`
83
- is already present — appends a `[tool.tf_project]` section to it. It refuses
82
+ This drops a `tf_project.toml` at the repo root, or - if a `pyproject.toml`
83
+ is already present - appends a `[tool.tf_project]` section to it. It refuses
84
84
  to overwrite an existing config.
85
85
 
86
86
  You can also write the file yourself:
@@ -135,18 +135,20 @@ Banner fields:
135
135
  | `header` | Must be `"terraform"`. Identifies the comment line as a tf-project banner. |
136
136
  | `project` | Subdirectory under `terraform_dir/` containing the source `.tf` files. |
137
137
  | `slot` | Slot name. Defaults to the tfvars filename stem. Must be unique across all tfvars in `tfvars_dir`. |
138
- | `state_key` | Full remote-state backend key. Overrides the default `<state_key_prefix><tfvars-stem>.tfstate`. Use to share state across files. |
138
+ | `state_key` | Full remote-state backend key. Overrides the default `<state_key_prefix><tfvars-stem>.tfstate`. Must be an absolute, normalised pure POSIX path (`/shared/global.tfstate`); relative keys, `.`/`..`, `//`, trailing `/`, and backslashes are rejected. The single leading slash is stripped before use (azurerm prepends the container, so a leading slash would double it). Use to share state across files. |
139
139
  | `env` | JSON object of `string → string` env vars passed to terraform when running against this slot. |
140
140
  | `backend_config` | JSON object of extra `-backend-config k=v` pairs. Wins over the config-level `[tf_project.backend_config]` table. |
141
141
 
142
142
  Unknown banner fields are tolerated (forward-compatible across tfp versions)
143
- but logged to stderr — so a typo doesn't disappear silently.
143
+ but logged to stderr - so a typo doesn't disappear silently.
144
144
 
145
145
  ## Usage
146
146
 
147
147
  ```sh
148
148
  tfp use dev # switch to slot "dev" (no init if already warm)
149
149
  tfp use tfvars/prod.tfvars # path also accepted (useful with shell completion)
150
+ tfp init # re-bind the active slot's backend from its banner
151
+ # (run after editing a banner's state_key/backend_config)
150
152
  tfp ls # list slots; active marked with `*`
151
153
  tfp rm staging # remove a slot's on-disk directory
152
154
 
@@ -174,13 +176,13 @@ tfp last # last terraform invocation for the active slot
174
176
 
175
177
  ### Choosing the active slot
176
178
 
177
- Three precedence levels — the most specific wins:
179
+ Three precedence levels - the most specific wins:
178
180
 
179
181
  | Source | Wins over | Lifetime |
180
182
  | ------------------ | ------------ | ----------------------------------------- |
181
183
  | `-s/--slot <name>` | everything | single invocation only (does NOT save) |
182
184
  | `TFP_SLOT=<name>` | saved file | current shell |
183
- | `tmp/active` file | — | persistent until next `tfp use` |
185
+ | `tmp/active` file | - | persistent until next `tfp use` |
184
186
 
185
187
  ```sh
186
188
  tfp -s prod plan # one-off, doesn't touch the saved active
@@ -190,9 +192,9 @@ tfp use prod # writes tmp/active (affects all shells without
190
192
 
191
193
  ### Global flags
192
194
 
193
- - `--verbose` — echo the terraform argv to stderr before exec.
194
- - `--dry-run` — print the argv and skip execution.
195
- - `-s/--slot <name>` — operate against a specific slot for one invocation.
195
+ - `--verbose` - echo the terraform argv to stderr before exec.
196
+ - `--dry-run` - print the argv and skip execution.
197
+ - `-s/--slot <name>` - operate against a specific slot for one invocation.
196
198
 
197
199
  ```sh
198
200
  tfp --dry-run plan -t module.foo
@@ -206,14 +208,29 @@ tfp -s prod --verbose apply
206
208
  slot's `.terraform/providers/`, so the second slot's first init is seconds.
207
209
 
208
210
  The trade-off: `TF_PLUGIN_CACHE_MAY_BREAK_DEPENDENCY_LOCK_FILE=1` is set so
209
- Terraform tolerates the cache's missing per-platform hashes. The first time
210
- your code is init'd on a new platform, the `.terraform.lock.hcl` will only
211
- record that platform's hashes. To pre-populate hashes for CI platforms you
212
- don't run on locally, run:
211
+ Terraform tolerates the cache's missing per-platform hashes. To keep
212
+ `.terraform.lock.hcl` portable across machines, every `tfp use` that runs
213
+ `terraform init` chases it with `terraform providers lock` for
214
+ `linux_amd64` + `linux_arm64` (the common CI / runtime targets) plus the
215
+ host's own platform - so on an Apple Silicon Mac you get three sets of
216
+ hashes (the two Linux ones + `darwin_arm64`), on Linux you just get the
217
+ two Linux ones, etc.
218
+
219
+ Teams that span multiple host platforms can extend the auto-lock set via
220
+ `tf_project.toml` so everyone's hashes land in the same lockfile regardless
221
+ of who ran `tfp use` last:
222
+
223
+ ```toml
224
+ [tf_project]
225
+ # linux_amd64, linux_arm64, and the host's own platform are always included;
226
+ # anything listed here is added on top.
227
+ extra_lock_platforms = ["windows_amd64", "darwin_amd64"]
228
+ ```
229
+
230
+ To re-lock against a custom platform set (e.g. add `windows_amd64`):
213
231
 
214
232
  ```sh
215
- tfp self providers lock # default: linux/darwin × amd64/arm64
216
- tfp self providers lock -p linux_amd64 -p windows_amd64 # custom platform set
233
+ tfp self providers lock -p linux_amd64 -p windows_amd64
217
234
  ```
218
235
 
219
236
  Disable the cache by setting `plugin_cache_dir = ""` in the config.
@@ -231,9 +248,9 @@ tfp force-unlock <LOCK_ID> # backend-agnostic, via terraform passthrough
231
248
 
232
249
  `tfp self lock` auto-detects the backend from the active slot's saved state:
233
250
 
234
- - **azurerm** — `storage_account_name` + `container_name` present. Shells out
251
+ - **azurerm** - `storage_account_name` + `container_name` present. Shells out
235
252
  to `az storage blob`.
236
- - **s3** — `bucket` + `dynamodb_table` present. Shells out to `aws dynamodb`.
253
+ - **s3** - `bucket` + `dynamodb_table` present. Shells out to `aws dynamodb`.
237
254
 
238
255
  Both need the respective CLI installed and authenticated.
239
256
 
@@ -242,6 +259,29 @@ runs `terraform force-unlock <ID>` first, falling back to a backend-level
242
259
  lease break only if the polite path fails. Pass `--blunt` to skip terraform
243
260
  entirely.
244
261
 
262
+ ### Auto-snapshot before state-mutating ops
263
+
264
+ `tfp state rm`, `tfp state mv`, `tfp state replace-provider`, and `tfp
265
+ import` each run `terraform state pull` first and save the result to
266
+ `<tmp_dir>/snapshots/<slot>/<ts>-pre-<op>.tfstate` before the mutating
267
+ command runs. If the pull fails (auth blip, network) the whole op aborts -
268
+ no point destroying state you can't roll back to. Recovery:
269
+
270
+ ```sh
271
+ tfp self snapshot list
272
+ tfp self snapshot restore # newest, with confirmation
273
+ tfp self snapshot restore <name> -y # specific snapshot, no prompt
274
+ ```
275
+
276
+ Restoration uses `terraform state push -force`, which overwrites the
277
+ remote state regardless of serial - by definition you're undoing a change,
278
+ so the serial check is exactly what you want to skip.
279
+
280
+ The retention budget is `snapshot_retention` in `tf_project.toml` (default
281
+ **200** per slot, pruned newest-first on every write). Set it to `0` to
282
+ disable auto-snapshotting entirely; `tfp self snapshot create` still works
283
+ either way.
284
+
245
285
  ### Apply safety
246
286
 
247
287
  `tfp plan` records a SHA-256 of the decrypted tfvars + every `.tf` under
@@ -283,7 +323,9 @@ tfp self state clear # delete the active slot's saved state
283
323
  tfp self state clear --all # nuke every slot's saved state
284
324
  tfp self doctor # sanity-check the environment (PATH, dirs, slot uniqueness, orphans)
285
325
  tfp self banner check <tfvars> # validate a banner; print resolved slot + fields
286
- tfp self snapshot # `terraform state pull` to <tmp_dir>/snapshot-<ts>.tfstate
326
+ tfp self snapshot create # `terraform state pull` to <tmp_dir>/snapshots/<slot>/<ts>-manual.tfstate
327
+ tfp self snapshot list # list snapshots for the active slot, newest first
328
+ tfp self snapshot restore [<name>] # `terraform state push -force` the named snapshot (default: newest)
287
329
  tfp self trace <subcommand> # print the argv tfp would build; no exec
288
330
  tfp self lock status # remote-state lock state (azurerm or s3+dynamodb)
289
331
  tfp self lock break # polite-by-default release; --blunt skips terraform
@@ -316,12 +358,12 @@ pdm run pytest -m integration # smoke tests (need `terraform` on PATH)
316
358
  ## Release
317
359
 
318
360
  Releases are **tag-driven**. The wheel's version comes from the git tag,
319
- not a code edit — `src/tf_project/__version__.py` exists but reads the
361
+ not a code edit - `src/tf_project/__version__.py` exists but reads the
320
362
  installed distribution's metadata at runtime; there is nothing in code to
321
363
  bump.
322
364
 
323
365
  1. Move the `## [Unreleased]` block in `CHANGELOG.md` under a new
324
- `## [X.Y.Z] — YYYY-MM-DD` heading. Commit to `main`.
366
+ `## [X.Y.Z] - YYYY-MM-DD` heading. Commit to `main`.
325
367
  2. Tag the commit: `git tag vX.Y.Z && git push origin vX.Y.Z`.
326
368
  3. The **Release** workflow fires on the tag push:
327
369
  - Re-runs CI against the tagged commit.
@@ -6,7 +6,7 @@
6
6
 
7
7
  A thin, opinionated **Terraform / OpenTofu** wrapper for multi-environment
8
8
  projects. Provides a single `tf-project` (aka `tfp`) CLI built around
9
- **named slots** — each tfvars file binds to a slot, and switching between
9
+ **named slots** - each tfvars file binds to a slot, and switching between
10
10
  slots is a pointer write, not a re-`terraform init`.
11
11
 
12
12
  If you maintain one tfvars per environment (`dev.tfvars`, `staging.tfvars`,
@@ -17,14 +17,14 @@ time you switch, this tool replaces that ritual with `tfp use dev`.
17
17
  tfvars files. `tfp use dev` flips the active slot; `tfp plan` / `tfp apply`
18
18
  operate against it.
19
19
  - **One `.terraform/` per slot** via `TF_DATA_DIR`. Two slots pointing at the
20
- same project keep independent provider/module/backend bindings on disk —
20
+ same project keep independent provider/module/backend bindings on disk -
21
21
  swapping between them is instant when both are warm.
22
22
  - **Shared provider cache** via `TF_PLUGIN_CACHE_DIR`. Providers download
23
23
  once and link into every slot's `.terraform/providers/`.
24
24
  - **Versioned, pydantic-validated state files.** Every persisted JSON carries
25
25
  `schema_version`; unknown fields fail loudly (except in user-authored
26
26
  banners, where they are tolerated and logged).
27
- - Pluggable tfvars preprocessing — defaults to [1Password's `op inject`][op]
27
+ - Pluggable tfvars preprocessing - defaults to [1Password's `op inject`][op]
28
28
  so you can keep `op://...` references in tfvars committed to git.
29
29
 
30
30
  [op]: https://developer.1password.com/docs/cli/secrets-template-syntax/
@@ -44,8 +44,8 @@ cd path/to/your-terraform-repo
44
44
  tfp self init
45
45
  ```
46
46
 
47
- This drops a `tf_project.toml` at the repo root, or — if a `pyproject.toml`
48
- is already present — appends a `[tool.tf_project]` section to it. It refuses
47
+ This drops a `tf_project.toml` at the repo root, or - if a `pyproject.toml`
48
+ is already present - appends a `[tool.tf_project]` section to it. It refuses
49
49
  to overwrite an existing config.
50
50
 
51
51
  You can also write the file yourself:
@@ -100,18 +100,20 @@ Banner fields:
100
100
  | `header` | Must be `"terraform"`. Identifies the comment line as a tf-project banner. |
101
101
  | `project` | Subdirectory under `terraform_dir/` containing the source `.tf` files. |
102
102
  | `slot` | Slot name. Defaults to the tfvars filename stem. Must be unique across all tfvars in `tfvars_dir`. |
103
- | `state_key` | Full remote-state backend key. Overrides the default `<state_key_prefix><tfvars-stem>.tfstate`. Use to share state across files. |
103
+ | `state_key` | Full remote-state backend key. Overrides the default `<state_key_prefix><tfvars-stem>.tfstate`. Must be an absolute, normalised pure POSIX path (`/shared/global.tfstate`); relative keys, `.`/`..`, `//`, trailing `/`, and backslashes are rejected. The single leading slash is stripped before use (azurerm prepends the container, so a leading slash would double it). Use to share state across files. |
104
104
  | `env` | JSON object of `string → string` env vars passed to terraform when running against this slot. |
105
105
  | `backend_config` | JSON object of extra `-backend-config k=v` pairs. Wins over the config-level `[tf_project.backend_config]` table. |
106
106
 
107
107
  Unknown banner fields are tolerated (forward-compatible across tfp versions)
108
- but logged to stderr — so a typo doesn't disappear silently.
108
+ but logged to stderr - so a typo doesn't disappear silently.
109
109
 
110
110
  ## Usage
111
111
 
112
112
  ```sh
113
113
  tfp use dev # switch to slot "dev" (no init if already warm)
114
114
  tfp use tfvars/prod.tfvars # path also accepted (useful with shell completion)
115
+ tfp init # re-bind the active slot's backend from its banner
116
+ # (run after editing a banner's state_key/backend_config)
115
117
  tfp ls # list slots; active marked with `*`
116
118
  tfp rm staging # remove a slot's on-disk directory
117
119
 
@@ -139,13 +141,13 @@ tfp last # last terraform invocation for the active slot
139
141
 
140
142
  ### Choosing the active slot
141
143
 
142
- Three precedence levels — the most specific wins:
144
+ Three precedence levels - the most specific wins:
143
145
 
144
146
  | Source | Wins over | Lifetime |
145
147
  | ------------------ | ------------ | ----------------------------------------- |
146
148
  | `-s/--slot <name>` | everything | single invocation only (does NOT save) |
147
149
  | `TFP_SLOT=<name>` | saved file | current shell |
148
- | `tmp/active` file | — | persistent until next `tfp use` |
150
+ | `tmp/active` file | - | persistent until next `tfp use` |
149
151
 
150
152
  ```sh
151
153
  tfp -s prod plan # one-off, doesn't touch the saved active
@@ -155,9 +157,9 @@ tfp use prod # writes tmp/active (affects all shells without
155
157
 
156
158
  ### Global flags
157
159
 
158
- - `--verbose` — echo the terraform argv to stderr before exec.
159
- - `--dry-run` — print the argv and skip execution.
160
- - `-s/--slot <name>` — operate against a specific slot for one invocation.
160
+ - `--verbose` - echo the terraform argv to stderr before exec.
161
+ - `--dry-run` - print the argv and skip execution.
162
+ - `-s/--slot <name>` - operate against a specific slot for one invocation.
161
163
 
162
164
  ```sh
163
165
  tfp --dry-run plan -t module.foo
@@ -171,14 +173,29 @@ tfp -s prod --verbose apply
171
173
  slot's `.terraform/providers/`, so the second slot's first init is seconds.
172
174
 
173
175
  The trade-off: `TF_PLUGIN_CACHE_MAY_BREAK_DEPENDENCY_LOCK_FILE=1` is set so
174
- Terraform tolerates the cache's missing per-platform hashes. The first time
175
- your code is init'd on a new platform, the `.terraform.lock.hcl` will only
176
- record that platform's hashes. To pre-populate hashes for CI platforms you
177
- don't run on locally, run:
176
+ Terraform tolerates the cache's missing per-platform hashes. To keep
177
+ `.terraform.lock.hcl` portable across machines, every `tfp use` that runs
178
+ `terraform init` chases it with `terraform providers lock` for
179
+ `linux_amd64` + `linux_arm64` (the common CI / runtime targets) plus the
180
+ host's own platform - so on an Apple Silicon Mac you get three sets of
181
+ hashes (the two Linux ones + `darwin_arm64`), on Linux you just get the
182
+ two Linux ones, etc.
183
+
184
+ Teams that span multiple host platforms can extend the auto-lock set via
185
+ `tf_project.toml` so everyone's hashes land in the same lockfile regardless
186
+ of who ran `tfp use` last:
187
+
188
+ ```toml
189
+ [tf_project]
190
+ # linux_amd64, linux_arm64, and the host's own platform are always included;
191
+ # anything listed here is added on top.
192
+ extra_lock_platforms = ["windows_amd64", "darwin_amd64"]
193
+ ```
194
+
195
+ To re-lock against a custom platform set (e.g. add `windows_amd64`):
178
196
 
179
197
  ```sh
180
- tfp self providers lock # default: linux/darwin × amd64/arm64
181
- tfp self providers lock -p linux_amd64 -p windows_amd64 # custom platform set
198
+ tfp self providers lock -p linux_amd64 -p windows_amd64
182
199
  ```
183
200
 
184
201
  Disable the cache by setting `plugin_cache_dir = ""` in the config.
@@ -196,9 +213,9 @@ tfp force-unlock <LOCK_ID> # backend-agnostic, via terraform passthrough
196
213
 
197
214
  `tfp self lock` auto-detects the backend from the active slot's saved state:
198
215
 
199
- - **azurerm** — `storage_account_name` + `container_name` present. Shells out
216
+ - **azurerm** - `storage_account_name` + `container_name` present. Shells out
200
217
  to `az storage blob`.
201
- - **s3** — `bucket` + `dynamodb_table` present. Shells out to `aws dynamodb`.
218
+ - **s3** - `bucket` + `dynamodb_table` present. Shells out to `aws dynamodb`.
202
219
 
203
220
  Both need the respective CLI installed and authenticated.
204
221
 
@@ -207,6 +224,29 @@ runs `terraform force-unlock <ID>` first, falling back to a backend-level
207
224
  lease break only if the polite path fails. Pass `--blunt` to skip terraform
208
225
  entirely.
209
226
 
227
+ ### Auto-snapshot before state-mutating ops
228
+
229
+ `tfp state rm`, `tfp state mv`, `tfp state replace-provider`, and `tfp
230
+ import` each run `terraform state pull` first and save the result to
231
+ `<tmp_dir>/snapshots/<slot>/<ts>-pre-<op>.tfstate` before the mutating
232
+ command runs. If the pull fails (auth blip, network) the whole op aborts -
233
+ no point destroying state you can't roll back to. Recovery:
234
+
235
+ ```sh
236
+ tfp self snapshot list
237
+ tfp self snapshot restore # newest, with confirmation
238
+ tfp self snapshot restore <name> -y # specific snapshot, no prompt
239
+ ```
240
+
241
+ Restoration uses `terraform state push -force`, which overwrites the
242
+ remote state regardless of serial - by definition you're undoing a change,
243
+ so the serial check is exactly what you want to skip.
244
+
245
+ The retention budget is `snapshot_retention` in `tf_project.toml` (default
246
+ **200** per slot, pruned newest-first on every write). Set it to `0` to
247
+ disable auto-snapshotting entirely; `tfp self snapshot create` still works
248
+ either way.
249
+
210
250
  ### Apply safety
211
251
 
212
252
  `tfp plan` records a SHA-256 of the decrypted tfvars + every `.tf` under
@@ -248,7 +288,9 @@ tfp self state clear # delete the active slot's saved state
248
288
  tfp self state clear --all # nuke every slot's saved state
249
289
  tfp self doctor # sanity-check the environment (PATH, dirs, slot uniqueness, orphans)
250
290
  tfp self banner check <tfvars> # validate a banner; print resolved slot + fields
251
- tfp self snapshot # `terraform state pull` to <tmp_dir>/snapshot-<ts>.tfstate
291
+ tfp self snapshot create # `terraform state pull` to <tmp_dir>/snapshots/<slot>/<ts>-manual.tfstate
292
+ tfp self snapshot list # list snapshots for the active slot, newest first
293
+ tfp self snapshot restore [<name>] # `terraform state push -force` the named snapshot (default: newest)
252
294
  tfp self trace <subcommand> # print the argv tfp would build; no exec
253
295
  tfp self lock status # remote-state lock state (azurerm or s3+dynamodb)
254
296
  tfp self lock break # polite-by-default release; --blunt skips terraform
@@ -281,12 +323,12 @@ pdm run pytest -m integration # smoke tests (need `terraform` on PATH)
281
323
  ## Release
282
324
 
283
325
  Releases are **tag-driven**. The wheel's version comes from the git tag,
284
- not a code edit — `src/tf_project/__version__.py` exists but reads the
326
+ not a code edit - `src/tf_project/__version__.py` exists but reads the
285
327
  installed distribution's metadata at runtime; there is nothing in code to
286
328
  bump.
287
329
 
288
330
  1. Move the `## [Unreleased]` block in `CHANGELOG.md` under a new
289
- `## [X.Y.Z] — YYYY-MM-DD` heading. Commit to `main`.
331
+ `## [X.Y.Z] - YYYY-MM-DD` heading. Commit to `main`.
290
332
  2. Tag the commit: `git tag vX.Y.Z && git push origin vX.Y.Z`.
291
333
  3. The **Release** workflow fires on the tag push:
292
334
  - Re-runs CI against the tagged commit.
@@ -32,7 +32,7 @@ dynamic = []
32
32
  requires-python = ">=3.11"
33
33
  license = "MIT"
34
34
  dependencies = [
35
- "typer>=0.21.2",
35
+ "typer>=0.26.0",
36
36
  "pydantic>=2.13.4",
37
37
  ]
38
38
  readme = "README.md"
@@ -54,7 +54,7 @@ classifiers = [
54
54
  "Topic :: Utilities",
55
55
  "Typing :: Typed",
56
56
  ]
57
- version = "0.2.1"
57
+ version = "0.2.3"
58
58
 
59
59
  [project.scripts]
60
60
  tf-project = "tf_project.cli:main"
@@ -1,4 +1,4 @@
1
- """tf_project — redistributable Terraform project wrapper."""
1
+ """tf_project - redistributable Terraform project wrapper."""
2
2
 
3
3
  from tf_project.__version__ import __version__
4
4
  from tf_project.banner import BannerError, ProjectInfoNotFoundError