tf-project 0.2.1__tar.gz → 0.2.2__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.2}/CHANGELOG.md +61 -28
  2. {tf_project-0.2.1 → tf_project-0.2.2}/PKG-INFO +63 -23
  3. {tf_project-0.2.1 → tf_project-0.2.2}/README.md +62 -22
  4. {tf_project-0.2.1 → tf_project-0.2.2}/pyproject.toml +1 -1
  5. {tf_project-0.2.1 → tf_project-0.2.2}/src/tf_project/__init__.py +1 -1
  6. tf_project-0.2.2/src/tf_project/__main__.py +4 -0
  7. {tf_project-0.2.1 → tf_project-0.2.2}/src/tf_project/__version__.py +1 -1
  8. {tf_project-0.2.1 → tf_project-0.2.2}/src/tf_project/banner.py +1 -1
  9. {tf_project-0.2.1 → tf_project-0.2.2}/src/tf_project/cli.py +73 -7
  10. {tf_project-0.2.1 → tf_project-0.2.2}/src/tf_project/commands.py +84 -14
  11. {tf_project-0.2.1 → tf_project-0.2.2}/src/tf_project/config.py +28 -3
  12. {tf_project-0.2.1 → tf_project-0.2.2}/src/tf_project/lock.py +4 -4
  13. {tf_project-0.2.1 → tf_project-0.2.2}/src/tf_project/migrate.py +5 -5
  14. {tf_project-0.2.1 → tf_project-0.2.2}/src/tf_project/models.py +3 -3
  15. {tf_project-0.2.1 → tf_project-0.2.2}/src/tf_project/preflight.py +1 -24
  16. {tf_project-0.2.1 → tf_project-0.2.2}/src/tf_project/secrets.py +2 -2
  17. {tf_project-0.2.1 → tf_project-0.2.2}/src/tf_project/self_commands.py +180 -33
  18. {tf_project-0.2.1 → tf_project-0.2.2}/src/tf_project/slots.py +1 -1
  19. {tf_project-0.2.1 → tf_project-0.2.2}/src/tf_project/state.py +1 -1
  20. {tf_project-0.2.1 → tf_project-0.2.2}/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.2}/LICENSE +0 -0
  23. {tf_project-0.2.1 → tf_project-0.2.2}/src/tf_project/py.typed +0 -0
@@ -4,7 +4,40 @@ 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.2] - 2026-05-24
8
+
9
+ ### Added
10
+ - **Auto-snapshot before state-mutating ops.** `tfp state rm`, `tfp state
11
+ mv`, `tfp state replace-provider`, and `tfp import` now `terraform state
12
+ pull` to `<tmp_dir>/snapshots/<slot>/<ts>-pre-<op>.tfstate` before
13
+ running - a failed pull aborts the op rather than destroying state you
14
+ can't roll back to. Recovery is `tfp self snapshot restore [<name>]`
15
+ (`terraform state push -force`, confirmation prompt; `-y` to skip), and
16
+ `tfp self snapshot list` enumerates per-slot snapshots newest first. New
17
+ `snapshot_retention` config field bounds the per-slot count (default
18
+ **200**, oldest-first pruning on every write; `0` disables auto-snapshot
19
+ entirely). `apply` is deliberately excluded - its plan/refresh cycle
20
+ already gives a recoverable diff and auto-snapshotting every apply would
21
+ dominate iteration time.
22
+
23
+ ### Changed
24
+ - **Cross-platform lockfile hashes are kept fresh automatically.** Every
25
+ `tfp use` that runs `terraform init` now chases it with `terraform
26
+ providers lock` for `linux_amd64` + `linux_arm64` (the common CI /
27
+ runtime targets) plus the host's own platform - so the committed
28
+ `.terraform.lock.hcl` is portable across the machines that actually
29
+ touch it without the user remembering to re-lock. Multi-platform teams
30
+ extend the set via the new `extra_lock_platforms` field in
31
+ `tf_project.toml` (e.g. `["windows_amd64"]`); entries are validated at
32
+ config-load time and deduped against the defaults. The previous
33
+ "lockfile missing" warning is gone - no longer reachable. `tfp self
34
+ providers lock [-p ...]` remains for ad-hoc re-locking.
35
+ - **`tfp self snapshot` is now a sub-app** (`create` / `list` / `restore`).
36
+ The old `tfp self snapshot` leaf becomes `tfp self snapshot create`;
37
+ snapshots live under `<tmp_dir>/snapshots/<slot>/` instead of
38
+ `<tmp_dir>/snapshot-<ts>.tfstate` so retention can prune per-slot.
39
+
40
+ ## [0.2.1] - 2026-05-24
8
41
 
9
42
  ### Added
10
43
  - **Recovery preflight.** Wrapped commands now bail out with an actionable
@@ -15,41 +48,41 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
15
48
  `.terraform.lock.hcl` is missing while the plugin cache is on, pointing
16
49
  at `tfp self providers lock` to repopulate cross-platform hashes.
17
50
 
18
- ## [0.2.0] — 2026-05-22
51
+ ## [0.2.0] - 2026-05-22
19
52
 
20
53
  ### Release pipeline
21
54
  - **Tag-driven releases.** The wheel's version is now derived from the git
22
55
  tag at build time via `pdm-backend`'s SCM source; `src/tf_project/__version__.py`
23
56
  reads the installed distribution's metadata at runtime. No more code-side
24
- version bump — push `vX.Y.Z` and the workflow takes over.
57
+ version bump - push `vX.Y.Z` and the workflow takes over.
25
58
  - **Sigstore-signed artifacts.** Every release attaches the `.whl`,
26
59
  `.tar.gz`, and matching `.sigstore` bundles to the GitHub Release.
27
60
  - **GitHub Releases automated.** Changelog notes are extracted from
28
61
  `## [X.Y.Z]` sections by `mindsers/changelog-reader-action`; pre-release
29
62
  tags (`rcN`, `aN`, `bN`, `devN`) are auto-flagged.
30
- - Removed the post-publish `pdm bump patch` step — the next release version
63
+ - Removed the post-publish `pdm bump patch` step - the next release version
31
64
  is whatever you tag next, not whatever a bot guessed.
32
65
 
33
- ### Added — slot model (BREAKING)
66
+ ### Added - slot model (BREAKING)
34
67
  - **Named slots replace the single-state model.** Each tfvars file is bound
35
68
  to a slot whose name defaults to the filename stem and can be overridden
36
69
  via a new banner field: `{"slot": "production"}`. State, tfplan, and
37
70
  `.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
71
+ between slots is now a pointer write - no `terraform init` when the slot
39
72
  is already warm.
40
- - `tfp use <slot-or-path>` — sole verb for selecting a slot. Accepts a slot
73
+ - `tfp use <slot-or-path>` - sole verb for selecting a slot. Accepts a slot
41
74
  name or a path to a tfvars file (paths are convenient with shell
42
75
  completion). Runs `terraform init -reconfigure` only when the slot's
43
76
  `.terraform/` is missing or the backend signature has changed.
44
- - `tfp ls` — list all slots derived from tfvars banners. Marks the active
77
+ - `tfp ls` - list all slots derived from tfvars banners. Marks the active
45
78
  slot, shows project + backend key + warm-since mtime. `--json` for
46
79
  scripts. Orphans (slot dirs with no matching banner) are listed
47
80
  separately.
48
- - `tfp rm <slot>` — delete a slot's on-disk directory. Refuses on the
81
+ - `tfp rm <slot>` - delete a slot's on-disk directory. Refuses on the
49
82
  active slot unless `--force`. Never touches the tfvars file.
50
- - Global `-s/--slot <name>` flag — operate against a specific slot for one
83
+ - Global `-s/--slot <name>` flag - operate against a specific slot for one
51
84
  invocation, without rewriting the saved active.
52
- - `TFP_SLOT` env var — overrides the saved active for the current shell
85
+ - `TFP_SLOT` env var - overrides the saved active for the current shell
53
86
  (useful for parallel slots across terminals or in CI).
54
87
  - Banner `slot` field validated for uniqueness across `tfvars_dir/*.tfvars`
55
88
  on every `tfp use` / `tfp ls`. Conflicts name both files and whether each
@@ -58,13 +91,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
58
91
  independent provider/module/backend bindings on disk.
59
92
  - Plugin cache shared across all slots via `TF_PLUGIN_CACHE_DIR`
60
93
  (default `tmp/plugin-cache/`, configurable). Providers download once and
61
- are linked into each slot's `.terraform/providers/` — first init for a
94
+ are linked into each slot's `.terraform/providers/` - first init for a
62
95
  new slot is seconds, not minutes. `TF_PLUGIN_CACHE_MAY_BREAK_DEPENDENCY_LOCK_FILE=1`
63
96
  is set automatically; pair with `tfp self providers lock` to populate
64
97
  multi-platform hashes for CI.
65
- - `tfp self providers lock [-p <platform>]` — wraps `terraform providers lock`
98
+ - `tfp self providers lock [-p <platform>]` - wraps `terraform providers lock`
66
99
  across `linux/darwin × amd64/arm64` by default.
67
- - `tfp self state show --all` / `tfp self state clear --all` — operate on
100
+ - `tfp self state show --all` / `tfp self state clear --all` - operate on
68
101
  every slot at once.
69
102
  - `tfp self doctor` adds slot-uniqueness and orphan checks.
70
103
  - Apply-safety hashes now also pin `project`, `state_key`, full
@@ -78,9 +111,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
78
111
  `tmp/slots/<slot>/state.json` on first run, with a breadcrumb left at
79
112
  `tmp/MIGRATED-<UTC-timestamp>.txt`.
80
113
 
81
- ### Changed — slot model (BREAKING)
114
+ ### Changed - slot model (BREAKING)
82
115
  - `tfp init <tfvars>` is **removed**. Use `tfp use <slot-or-path>` instead.
83
- - Banner `env` is no longer merged across `tfp use` invocations — the
116
+ - Banner `env` is no longer merged across `tfp use` invocations - the
84
117
  banner is the single source of truth at the moment of switching.
85
118
  - `tfp last` / `tfp status` / `tfp self state show` / `tfp self trace` /
86
119
  `tfp self snapshot` / `tfp self lock` are all slot-scoped. They consult
@@ -94,11 +127,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
94
127
  re-resolve.
95
128
 
96
129
  ### Added
97
- - `tfp state` subcommand group — all eight terraform state subcommands
130
+ - `tfp state` subcommand group - all eight terraform state subcommands
98
131
  (`list`, `show`, `mv`, `rm`, `pull`, `push`, `replace-provider`,
99
132
  `identities`) wrapped and discoverable in `tfp --help`. Each forwards
100
133
  `-chdir` + the saved env.
101
- - `tfp import <ADDRESS> <ID>` — promoted from raw passthrough to a wrapped
134
+ - `tfp import <ADDRESS> <ID>` - promoted from raw passthrough to a wrapped
102
135
  subcommand. Forwards the decrypted tfvars (`-var-file`) and the saved
103
136
  env so provider blocks that reference variables can resolve. Extra
104
137
  terraform flags after the positional args still passthrough.
@@ -111,13 +144,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
111
144
  backend-level lease break only if the polite path fails or the lock ID
112
145
  isn't recoverable. Pass `--blunt` to skip terraform entirely. Refuses
113
146
  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
147
+ - `tfp self snapshot` - pulls the remote tfstate to a local file via
115
148
  `terraform state pull`. Defaults to `<tmp_dir>/snapshot-<UTC-timestamp>.tfstate`.
116
- - `tfp self trace <subcommand>` — prints the exact terraform argv `tfp <subcommand>`
149
+ - `tfp self trace <subcommand>` - prints the exact terraform argv `tfp <subcommand>`
117
150
  would build, without invoking anything. Useful when copy-pasting a
118
151
  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
152
+ fresh slot - no saved state required.
153
+ - `tfp last` - prints the most recent terraform invocation recorded by
121
154
  tfp (argv + exit code + timestamp). Persisted per-slot at
122
155
  `tmp/slots/<slot>/last.json`.
123
156
  - Tab-completion for `tfp use` (slot names + tfvars paths) and
@@ -141,11 +174,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
141
174
  Azure-specific `lease_state` / `lease_duration` fields are folded into
142
175
  `detail`.
143
176
  - Tfplan meta sidecar field renamed from `tfvars_sha256` to
144
- `inputs_sha256`. The legacy field name is **not** accepted on read —
177
+ `inputs_sha256`. The legacy field name is **not** accepted on read -
145
178
  saved plans from pre-rename versions must be re-created with
146
179
  `tfp plan`.
147
- - `tfp status` — one-line summary of the active slot.
148
- - `tfp self banner check <tfvars>` — validates a banner and prints the
180
+ - `tfp status` - one-line summary of the active slot.
181
+ - `tfp self banner check <tfvars>` - validates a banner and prints the
149
182
  resolved `source_root` / `backend_config` / `env` / `slot` without
150
183
  running terraform.
151
184
  - Global `--verbose` and `--dry-run` flags. `--verbose` logs the terraform
@@ -156,14 +189,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
156
189
  - Real-terraform end-to-end integration test covering use→plan→apply→destroy
157
190
  against a `null_resource` fixture, plus a two-slot warm-switch test.
158
191
  - `BannerError` for clean, file-pathed banner validation failures.
159
- - `py.typed` marker — the package ships type information explicitly.
192
+ - `py.typed` marker - the package ships type information explicitly.
160
193
  - Terraform exit codes propagate cleanly (no Python traceback when
161
194
  terraform fails). `terraform` not found exits 127.
162
195
  - SIGINT during a running terraform command no longer kills Python first;
163
196
  terraform receives the signal via the shared process group and we wait
164
197
  for it to finish.
165
198
  - Passthrough invocations (`tfp <unknown-subcommand>`) `os.execvpe` the
166
- terraform process directly — native signal handling and exit code.
199
+ terraform process directly - native signal handling and exit code.
167
200
  `tfp -s <slot> <unknown>` refuses if the slot has no saved state, rather
168
201
  than silently dropping `-chdir`.
169
202
  - `state.save_state` takes an exclusive file lock so parallel `tfp use`
@@ -176,7 +209,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
176
209
  - `tfp state-mv` top-level command. Use `tfp state mv` instead.
177
210
  - `commands.do_state_mv` public helper. Use `commands.do_state(subcommand="mv", ...)`.
178
211
 
179
- ## [0.1.0] — 2026-05-14
212
+ ## [0.1.0] - 2026-05-14
180
213
 
181
214
  ### Added
182
215
  - 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.2
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>
@@ -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:
@@ -140,7 +140,7 @@ Banner fields:
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
 
@@ -174,13 +174,13 @@ tfp last # last terraform invocation for the active slot
174
174
 
175
175
  ### Choosing the active slot
176
176
 
177
- Three precedence levels — the most specific wins:
177
+ Three precedence levels - the most specific wins:
178
178
 
179
179
  | Source | Wins over | Lifetime |
180
180
  | ------------------ | ------------ | ----------------------------------------- |
181
181
  | `-s/--slot <name>` | everything | single invocation only (does NOT save) |
182
182
  | `TFP_SLOT=<name>` | saved file | current shell |
183
- | `tmp/active` file | — | persistent until next `tfp use` |
183
+ | `tmp/active` file | - | persistent until next `tfp use` |
184
184
 
185
185
  ```sh
186
186
  tfp -s prod plan # one-off, doesn't touch the saved active
@@ -190,9 +190,9 @@ tfp use prod # writes tmp/active (affects all shells without
190
190
 
191
191
  ### Global flags
192
192
 
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.
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.
196
196
 
197
197
  ```sh
198
198
  tfp --dry-run plan -t module.foo
@@ -206,14 +206,29 @@ tfp -s prod --verbose apply
206
206
  slot's `.terraform/providers/`, so the second slot's first init is seconds.
207
207
 
208
208
  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:
209
+ Terraform tolerates the cache's missing per-platform hashes. To keep
210
+ `.terraform.lock.hcl` portable across machines, every `tfp use` that runs
211
+ `terraform init` chases it with `terraform providers lock` for
212
+ `linux_amd64` + `linux_arm64` (the common CI / runtime targets) plus the
213
+ host's own platform - so on an Apple Silicon Mac you get three sets of
214
+ hashes (the two Linux ones + `darwin_arm64`), on Linux you just get the
215
+ two Linux ones, etc.
216
+
217
+ Teams that span multiple host platforms can extend the auto-lock set via
218
+ `tf_project.toml` so everyone's hashes land in the same lockfile regardless
219
+ of who ran `tfp use` last:
220
+
221
+ ```toml
222
+ [tf_project]
223
+ # linux_amd64, linux_arm64, and the host's own platform are always included;
224
+ # anything listed here is added on top.
225
+ extra_lock_platforms = ["windows_amd64", "darwin_amd64"]
226
+ ```
227
+
228
+ To re-lock against a custom platform set (e.g. add `windows_amd64`):
213
229
 
214
230
  ```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
231
+ tfp self providers lock -p linux_amd64 -p windows_amd64
217
232
  ```
218
233
 
219
234
  Disable the cache by setting `plugin_cache_dir = ""` in the config.
@@ -231,9 +246,9 @@ tfp force-unlock <LOCK_ID> # backend-agnostic, via terraform passthrough
231
246
 
232
247
  `tfp self lock` auto-detects the backend from the active slot's saved state:
233
248
 
234
- - **azurerm** — `storage_account_name` + `container_name` present. Shells out
249
+ - **azurerm** - `storage_account_name` + `container_name` present. Shells out
235
250
  to `az storage blob`.
236
- - **s3** — `bucket` + `dynamodb_table` present. Shells out to `aws dynamodb`.
251
+ - **s3** - `bucket` + `dynamodb_table` present. Shells out to `aws dynamodb`.
237
252
 
238
253
  Both need the respective CLI installed and authenticated.
239
254
 
@@ -242,6 +257,29 @@ runs `terraform force-unlock <ID>` first, falling back to a backend-level
242
257
  lease break only if the polite path fails. Pass `--blunt` to skip terraform
243
258
  entirely.
244
259
 
260
+ ### Auto-snapshot before state-mutating ops
261
+
262
+ `tfp state rm`, `tfp state mv`, `tfp state replace-provider`, and `tfp
263
+ import` each run `terraform state pull` first and save the result to
264
+ `<tmp_dir>/snapshots/<slot>/<ts>-pre-<op>.tfstate` before the mutating
265
+ command runs. If the pull fails (auth blip, network) the whole op aborts -
266
+ no point destroying state you can't roll back to. Recovery:
267
+
268
+ ```sh
269
+ tfp self snapshot list
270
+ tfp self snapshot restore # newest, with confirmation
271
+ tfp self snapshot restore <name> -y # specific snapshot, no prompt
272
+ ```
273
+
274
+ Restoration uses `terraform state push -force`, which overwrites the
275
+ remote state regardless of serial - by definition you're undoing a change,
276
+ so the serial check is exactly what you want to skip.
277
+
278
+ The retention budget is `snapshot_retention` in `tf_project.toml` (default
279
+ **200** per slot, pruned newest-first on every write). Set it to `0` to
280
+ disable auto-snapshotting entirely; `tfp self snapshot create` still works
281
+ either way.
282
+
245
283
  ### Apply safety
246
284
 
247
285
  `tfp plan` records a SHA-256 of the decrypted tfvars + every `.tf` under
@@ -283,7 +321,9 @@ tfp self state clear # delete the active slot's saved state
283
321
  tfp self state clear --all # nuke every slot's saved state
284
322
  tfp self doctor # sanity-check the environment (PATH, dirs, slot uniqueness, orphans)
285
323
  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
324
+ tfp self snapshot create # `terraform state pull` to <tmp_dir>/snapshots/<slot>/<ts>-manual.tfstate
325
+ tfp self snapshot list # list snapshots for the active slot, newest first
326
+ tfp self snapshot restore [<name>] # `terraform state push -force` the named snapshot (default: newest)
287
327
  tfp self trace <subcommand> # print the argv tfp would build; no exec
288
328
  tfp self lock status # remote-state lock state (azurerm or s3+dynamodb)
289
329
  tfp self lock break # polite-by-default release; --blunt skips terraform
@@ -316,12 +356,12 @@ pdm run pytest -m integration # smoke tests (need `terraform` on PATH)
316
356
  ## Release
317
357
 
318
358
  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
359
+ not a code edit - `src/tf_project/__version__.py` exists but reads the
320
360
  installed distribution's metadata at runtime; there is nothing in code to
321
361
  bump.
322
362
 
323
363
  1. Move the `## [Unreleased]` block in `CHANGELOG.md` under a new
324
- `## [X.Y.Z] — YYYY-MM-DD` heading. Commit to `main`.
364
+ `## [X.Y.Z] - YYYY-MM-DD` heading. Commit to `main`.
325
365
  2. Tag the commit: `git tag vX.Y.Z && git push origin vX.Y.Z`.
326
366
  3. The **Release** workflow fires on the tag push:
327
367
  - 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:
@@ -105,7 +105,7 @@ Banner fields:
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
 
@@ -139,13 +139,13 @@ tfp last # last terraform invocation for the active slot
139
139
 
140
140
  ### Choosing the active slot
141
141
 
142
- Three precedence levels — the most specific wins:
142
+ Three precedence levels - the most specific wins:
143
143
 
144
144
  | Source | Wins over | Lifetime |
145
145
  | ------------------ | ------------ | ----------------------------------------- |
146
146
  | `-s/--slot <name>` | everything | single invocation only (does NOT save) |
147
147
  | `TFP_SLOT=<name>` | saved file | current shell |
148
- | `tmp/active` file | — | persistent until next `tfp use` |
148
+ | `tmp/active` file | - | persistent until next `tfp use` |
149
149
 
150
150
  ```sh
151
151
  tfp -s prod plan # one-off, doesn't touch the saved active
@@ -155,9 +155,9 @@ tfp use prod # writes tmp/active (affects all shells without
155
155
 
156
156
  ### Global flags
157
157
 
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.
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.
161
161
 
162
162
  ```sh
163
163
  tfp --dry-run plan -t module.foo
@@ -171,14 +171,29 @@ tfp -s prod --verbose apply
171
171
  slot's `.terraform/providers/`, so the second slot's first init is seconds.
172
172
 
173
173
  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:
174
+ Terraform tolerates the cache's missing per-platform hashes. To keep
175
+ `.terraform.lock.hcl` portable across machines, every `tfp use` that runs
176
+ `terraform init` chases it with `terraform providers lock` for
177
+ `linux_amd64` + `linux_arm64` (the common CI / runtime targets) plus the
178
+ host's own platform - so on an Apple Silicon Mac you get three sets of
179
+ hashes (the two Linux ones + `darwin_arm64`), on Linux you just get the
180
+ two Linux ones, etc.
181
+
182
+ Teams that span multiple host platforms can extend the auto-lock set via
183
+ `tf_project.toml` so everyone's hashes land in the same lockfile regardless
184
+ of who ran `tfp use` last:
185
+
186
+ ```toml
187
+ [tf_project]
188
+ # linux_amd64, linux_arm64, and the host's own platform are always included;
189
+ # anything listed here is added on top.
190
+ extra_lock_platforms = ["windows_amd64", "darwin_amd64"]
191
+ ```
192
+
193
+ To re-lock against a custom platform set (e.g. add `windows_amd64`):
178
194
 
179
195
  ```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
196
+ tfp self providers lock -p linux_amd64 -p windows_amd64
182
197
  ```
183
198
 
184
199
  Disable the cache by setting `plugin_cache_dir = ""` in the config.
@@ -196,9 +211,9 @@ tfp force-unlock <LOCK_ID> # backend-agnostic, via terraform passthrough
196
211
 
197
212
  `tfp self lock` auto-detects the backend from the active slot's saved state:
198
213
 
199
- - **azurerm** — `storage_account_name` + `container_name` present. Shells out
214
+ - **azurerm** - `storage_account_name` + `container_name` present. Shells out
200
215
  to `az storage blob`.
201
- - **s3** — `bucket` + `dynamodb_table` present. Shells out to `aws dynamodb`.
216
+ - **s3** - `bucket` + `dynamodb_table` present. Shells out to `aws dynamodb`.
202
217
 
203
218
  Both need the respective CLI installed and authenticated.
204
219
 
@@ -207,6 +222,29 @@ runs `terraform force-unlock <ID>` first, falling back to a backend-level
207
222
  lease break only if the polite path fails. Pass `--blunt` to skip terraform
208
223
  entirely.
209
224
 
225
+ ### Auto-snapshot before state-mutating ops
226
+
227
+ `tfp state rm`, `tfp state mv`, `tfp state replace-provider`, and `tfp
228
+ import` each run `terraform state pull` first and save the result to
229
+ `<tmp_dir>/snapshots/<slot>/<ts>-pre-<op>.tfstate` before the mutating
230
+ command runs. If the pull fails (auth blip, network) the whole op aborts -
231
+ no point destroying state you can't roll back to. Recovery:
232
+
233
+ ```sh
234
+ tfp self snapshot list
235
+ tfp self snapshot restore # newest, with confirmation
236
+ tfp self snapshot restore <name> -y # specific snapshot, no prompt
237
+ ```
238
+
239
+ Restoration uses `terraform state push -force`, which overwrites the
240
+ remote state regardless of serial - by definition you're undoing a change,
241
+ so the serial check is exactly what you want to skip.
242
+
243
+ The retention budget is `snapshot_retention` in `tf_project.toml` (default
244
+ **200** per slot, pruned newest-first on every write). Set it to `0` to
245
+ disable auto-snapshotting entirely; `tfp self snapshot create` still works
246
+ either way.
247
+
210
248
  ### Apply safety
211
249
 
212
250
  `tfp plan` records a SHA-256 of the decrypted tfvars + every `.tf` under
@@ -248,7 +286,9 @@ tfp self state clear # delete the active slot's saved state
248
286
  tfp self state clear --all # nuke every slot's saved state
249
287
  tfp self doctor # sanity-check the environment (PATH, dirs, slot uniqueness, orphans)
250
288
  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
289
+ tfp self snapshot create # `terraform state pull` to <tmp_dir>/snapshots/<slot>/<ts>-manual.tfstate
290
+ tfp self snapshot list # list snapshots for the active slot, newest first
291
+ tfp self snapshot restore [<name>] # `terraform state push -force` the named snapshot (default: newest)
252
292
  tfp self trace <subcommand> # print the argv tfp would build; no exec
253
293
  tfp self lock status # remote-state lock state (azurerm or s3+dynamodb)
254
294
  tfp self lock break # polite-by-default release; --blunt skips terraform
@@ -281,12 +321,12 @@ pdm run pytest -m integration # smoke tests (need `terraform` on PATH)
281
321
  ## Release
282
322
 
283
323
  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
324
+ not a code edit - `src/tf_project/__version__.py` exists but reads the
285
325
  installed distribution's metadata at runtime; there is nothing in code to
286
326
  bump.
287
327
 
288
328
  1. Move the `## [Unreleased]` block in `CHANGELOG.md` under a new
289
- `## [X.Y.Z] — YYYY-MM-DD` heading. Commit to `main`.
329
+ `## [X.Y.Z] - YYYY-MM-DD` heading. Commit to `main`.
290
330
  2. Tag the commit: `git tag vX.Y.Z && git push origin vX.Y.Z`.
291
331
  3. The **Release** workflow fires on the tag push:
292
332
  - Re-runs CI against the tagged commit.
@@ -54,7 +54,7 @@ classifiers = [
54
54
  "Topic :: Utilities",
55
55
  "Typing :: Typed",
56
56
  ]
57
- version = "0.2.1"
57
+ version = "0.2.2"
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
@@ -0,0 +1,4 @@
1
+ from tf_project.cli import main
2
+
3
+ if __name__ == "__main__": # pragma: no branch - the only other branch (import-as-module) is not exercised standalone
4
+ main()
@@ -16,5 +16,5 @@ from importlib import metadata
16
16
 
17
17
  try:
18
18
  __version__ = metadata.version("tf-project")
19
- except metadata.PackageNotFoundError: # pragma: no cover — editable w/o dist
19
+ except metadata.PackageNotFoundError: # pragma: no cover - editable w/o dist
20
20
  __version__ = "0.0.0+unknown"
@@ -81,7 +81,7 @@ def resolve_backend_config(
81
81
  (only if `state_key_prefix` is set).
82
82
  2. `[tf_project.backend_config]` from config.
83
83
  3. `backend_config` from the banner.
84
- 4. `state_key` from the banner — final word on `key`.
84
+ 4. `state_key` from the banner - final word on `key`.
85
85
  """
86
86
  merged: dict[str, str] = {}
87
87
  if config.state_key_prefix: