panorama-super-cli 0.1.0__py3-none-any.whl

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.
@@ -0,0 +1,110 @@
1
+ Metadata-Version: 2.4
2
+ Name: panorama-super-cli
3
+ Version: 0.1.0
4
+ Summary: Agent-friendly CLI for Palo Alto Panorama object management: find, dedup/merge, rename, and audit address/service objects safely.
5
+ Project-URL: Homepage, https://github.com/thomaschristory/panorama-super-cli
6
+ Project-URL: Documentation, https://thomaschristory.github.io/panorama-super-cli/
7
+ Project-URL: Issues, https://github.com/thomaschristory/panorama-super-cli/issues
8
+ Author-email: Thomas <mick27@gmail.com>
9
+ License-Expression: Apache-2.0
10
+ License-File: LICENSE
11
+ Keywords: automation,cli,firewall,netsec,paloalto,panorama,panos
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Environment :: Console
14
+ Classifier: Intended Audience :: System Administrators
15
+ Classifier: License :: OSI Approved :: Apache Software License
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Topic :: System :: Networking
19
+ Classifier: Topic :: System :: Systems Administration
20
+ Requires-Python: >=3.12
21
+ Requires-Dist: click>=8.1
22
+ Requires-Dist: defusedxml>=0.7
23
+ Requires-Dist: pan-os-python>=1.12
24
+ Requires-Dist: platformdirs>=4.2
25
+ Requires-Dist: pydantic>=2.7
26
+ Requires-Dist: rich>=13.7
27
+ Requires-Dist: ruamel-yaml>=0.18
28
+ Requires-Dist: structlog>=24.1
29
+ Requires-Dist: typer>=0.12
30
+ Description-Content-Type: text/markdown
31
+
32
+ # panorama-super-cli (`psc`)
33
+
34
+ [![PyPI](https://img.shields.io/pypi/v/panorama-super-cli.svg)](https://pypi.org/project/panorama-super-cli/)
35
+ [![CI](https://github.com/thomaschristory/panorama-super-cli/actions/workflows/test.yml/badge.svg)](https://github.com/thomaschristory/panorama-super-cli/actions/workflows/test.yml)
36
+ [![Docs](https://github.com/thomaschristory/panorama-super-cli/actions/workflows/docs.yml/badge.svg)](https://thomaschristory.github.io/panorama-super-cli/)
37
+ [![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)
38
+
39
+ **Agent-friendly object management for Palo Alto Panorama.** Find where an IP
40
+ lives, hunt down duplicate address/service objects, merge them safely (rewriting
41
+ every group and rule that referenced them), enforce naming conventions, and
42
+ audit object hygiene — all **dry-run by default**, with **PAN-OS `set`** and
43
+ **JSON** output for humans and agents alike.
44
+
45
+ ```console
46
+ $ psc --config panorama.xml find ip 10.0.0.10
47
+ $ psc --config panorama.xml dedup addresses
48
+ $ psc --config panorama.xml dedup merge --keep h-web1 --into web-primary --apply
49
+ ```
50
+
51
+ > ⚠️ **Alpha.** The CLI surface and JSON contracts may shift before v1.0.0.
52
+ > Writes are dry-run by default; nothing touches Panorama without `--apply`.
53
+
54
+ ## Why
55
+
56
+ Panorama configs rot: the same `10.0.0.10` ends up as `h-web1`, `web-primary`,
57
+ and `WEB_PRD_01`; services duplicate well-known ports; objects outlive the rules
58
+ that used them. `psc` gives you a fast, scriptable, **safe** way to see and fix
59
+ that — offline against an exported config, or live against Panorama.
60
+
61
+ ## Install
62
+
63
+ ```console
64
+ uv tool install panorama-super-cli # recommended
65
+ # or
66
+ pipx install panorama-super-cli
67
+ # or
68
+ pip install panorama-super-cli
69
+ ```
70
+
71
+ ## Two ways to point it at a config
72
+
73
+ - **Offline** (no credentials, totally safe): `psc --config exported.xml <cmd>`.
74
+ Export from Panorama (`scp export configuration ...` or the GUI) and audit it
75
+ on your laptop.
76
+ - **Live**: configure a profile (`psc init`, `psc login`) and `psc` talks the
77
+ PAN-OS XML API via [`pan-os-python`](https://github.com/PaloAltoNetworks/pan-os-python).
78
+ Reads are free; writes still require `--apply`.
79
+
80
+ ## What it does
81
+
82
+ | Area | Commands (v0.1) |
83
+ | --- | --- |
84
+ | **Find / resolve** | `psc find ip <ip>`, `find ip -f ips.txt`, `find object <name>` |
85
+ | **Duplicates** | `psc dedup addresses`, `dedup services`, `dedup merge` |
86
+ | **Naming** | `psc name suggest`, `name lint`, `name apply` (opt-in templates) |
87
+ | **References** | `psc refs <object>` (where-used), `refs unused` |
88
+ | **Output** | `--output json|set|table|yaml|csv|jsonl` |
89
+
90
+ See the [docs](https://thomaschristory.github.io/panorama-super-cli/) for the
91
+ full surface, the safety model, and the agent guide.
92
+
93
+ ## Safety model
94
+
95
+ - **Dry-run is the default.** Every mutating command prints a plan and exits
96
+ without touching anything unless you add `--apply`.
97
+ - **Side-effect aware.** Merging or renaming an object rewrites every address
98
+ group, security rule, and NAT rule that referenced it — across `shared` and
99
+ device-groups — or refuses and tells you why.
100
+ - **`--debug`** streams structured logs to stderr; stdout stays clean for pipes.
101
+
102
+ ## For AI agents
103
+
104
+ `psc` ships a bundled [Agent Skill](skills/panorama-super-cli/SKILL.md) and
105
+ emits a stable JSON envelope + exit-code contract. Pass `--output json` and
106
+ parse away. See [Using with AI agents](https://thomaschristory.github.io/panorama-super-cli/guides/using-with-ai-agents/).
107
+
108
+ ## License
109
+
110
+ [Apache-2.0](LICENSE).
@@ -0,0 +1,36 @@
1
+ psc/__init__.py,sha256=KR1on8TZD6cSa9O_PuQlhaGVncVlhH4wqhMqRorGnxw,145
2
+ psc/__main__.py,sha256=XxE5t3q2qvTPYHwHtLkrW1krSNT4pYzuYwJXqkFqFX4,99
3
+ psc/_version.py,sha256=nta-9BSLOfVajC8FQrW12L7exhfUuSuisXaXXIQR98Y,217
4
+ psc/cli/__init__.py,sha256=4o3VGuybs6yXrzRrGpfAeWU2YTUD8WlbjT2EZXSxXuQ,79
5
+ psc/cli/_plan.py,sha256=cB-zJvTQeDXlRuVaoYjEVP4N_Jiworb25kd7n80PaaI,2210
6
+ psc/cli/app.py,sha256=yFAEwQA2p5wWZxzDdWxxStgDeXB16G2TWRcmEfs0c3M,4141
7
+ psc/cli/dedup_cmds.py,sha256=tfLm2L2VM0iN1xeARiCLOACgx0qC3BAl-CkgieNKBWc,3359
8
+ psc/cli/find_cmds.py,sha256=yFDNJL8HB-ZBGqLP0gpIeJCw21cuVN2vEtzMLN3wLX4,3061
9
+ psc/cli/name_cmds.py,sha256=dVkV6npkNn-ZemP6xRrxgSsfoNoCKd-0m2m3S5o3J8o,3933
10
+ psc/cli/profile_cmds.py,sha256=sqXDkZin2HHLW7drenJa5BWrwfckfYzTXAKgCRKs3oo,2454
11
+ psc/cli/refs_cmds.py,sha256=xTgOXSJJzz8UtWYYmaF5zmgn_C6DIuubq8WyIpNSf4Y,3625
12
+ psc/cli/runtime.py,sha256=PVfErH8C0KhbyJG6bCnSe6AEaBOwm6cdT9VHA7snukI,2928
13
+ psc/config/__init__.py,sha256=ljOcFZaEbXW5f1hPuD97iHROnmadWK7G6KpHuKrDxB4,265
14
+ psc/config/loader.py,sha256=xAmB7u3mnb02plf8fKz9fvbJzySszxQ4jzzeZjHlJ40,1345
15
+ psc/config/models.py,sha256=mV9BuwmVBxE65fxoFt-5aSqXDIRMOWenMkASLvGu8XU,1209
16
+ psc/core/__init__.py,sha256=RERD9tJtJ3wnBUA2PtiODbY7kK9_OkFCjFTi_6Tgp2Q,279
17
+ psc/core/apply_xml.py,sha256=4eyIBr6EB_f3_uO_baA6JQdHfX02kUAUGjXmxhFu3JE,6455
18
+ psc/core/changeset.py,sha256=GEJi-d4JwgLYz0w1rWb_wbIHhgzFdxbaEsgl0NH-rHk,4017
19
+ psc/core/dedup.py,sha256=naZzpUcTwX6xxh0wdffPDUYrZMyAyg9oSsGYXY_SgPY,8167
20
+ psc/core/models.py,sha256=2gFADwtiOg-9hxa-CpVo-nNKCummbCR4kt29HXh5FAk,7656
21
+ psc/core/naming.py,sha256=i98ZpDJlt01CWY38fNstlLFFX8gIu4tRZjUAgKVe0as,6465
22
+ psc/core/normalize.py,sha256=vmQ2pt-Pj7q0ckekW5girTTADCvf44kc_j2r8cRPA0Q,6217
23
+ psc/core/parse.py,sha256=lOhbEJULefFCjOYTcxkAFzxTCcg_mIHppnmzp2WlvDo,8967
24
+ psc/core/refs.py,sha256=v2b-JH8U7hJYzgT09glfn7iAb0iYEr25zhXlDC2UEOU,14397
25
+ psc/core/resolve.py,sha256=KObdFo9Y1UENDXBB6uXgbXiMFK2n5Ep1Xt9AeEgmXVQ,6077
26
+ psc/core/setcmd.py,sha256=U409l5pHWJlFQcGSopJ_oFFW0JQhZ3MDVb3hadscN_M,6302
27
+ psc/core/source.py,sha256=-RdK1116GIzqHrTje84VJmFjDmX8FqGVM4dBbVlCU-I,4258
28
+ psc/output/__init__.py,sha256=fjOBTdTjj-xbIhjRxclNE8FHw_v1tjYtfkS2Tcl2udo,272
29
+ psc/output/errors.py,sha256=osq5FoGEDAg4niIgvEym8eFnmpF0_NiS_FUEoALQHQU,1976
30
+ psc/output/format.py,sha256=S8VYn7Qs9CEFvdDUSxexQZeU55X4V_bdUlMgGYTfLEM,4376
31
+ skills/panorama-super-cli/SKILL.md,sha256=Ooo8EDfkDaUFDst6Y0AZA24wIhGYsLzWnlPcs4Ki2o4,6269
32
+ panorama_super_cli-0.1.0.dist-info/METADATA,sha256=BuRf6PZ009tu0UVBVYgFMdMB6PnR94STTHsCSgRZaN8,4839
33
+ panorama_super_cli-0.1.0.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
34
+ panorama_super_cli-0.1.0.dist-info/entry_points.txt,sha256=yqPBZPuiQyn_sP2MCjRQgFRevsvuoTrafzD1KNiylCY,79
35
+ panorama_super_cli-0.1.0.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
36
+ panorama_super_cli-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.30.1
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,3 @@
1
+ [console_scripts]
2
+ panorama-super-cli = psc.cli.app:main
3
+ psc = psc.cli.app:main
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
psc/__init__.py ADDED
@@ -0,0 +1,5 @@
1
+ """panorama-super-cli (`psc`) — agent-friendly Panorama object management."""
2
+
3
+ from psc._version import __version__
4
+
5
+ __all__ = ["__version__"]
psc/__main__.py ADDED
@@ -0,0 +1,6 @@
1
+ """Enable `python -m psc`."""
2
+
3
+ from psc.cli.app import main
4
+
5
+ if __name__ == "__main__":
6
+ main()
psc/_version.py ADDED
@@ -0,0 +1,7 @@
1
+ """Single source of truth for the package version.
2
+
3
+ `release.yml` validates the git tag against this value before publishing.
4
+ Keep this in sync with `[project].version` in `pyproject.toml`.
5
+ """
6
+
7
+ __version__ = "0.1.0"
psc/cli/__init__.py ADDED
@@ -0,0 +1 @@
1
+ """The Typer frontend. Thin: parses args, calls `psc.core`, formats output."""
psc/cli/_plan.py ADDED
@@ -0,0 +1,62 @@
1
+ """Shared dry-run/apply completion for every mutating command.
2
+
3
+ Centralising this is what guarantees the safety contract is identical across
4
+ `dedup merge`, `name rename`, and any future write command: blocked plans are
5
+ refused, dry-run is the default, and `--apply` is the only path to a write.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from psc.cli.runtime import Runtime
11
+ from psc.core.changeset import ChangeSet
12
+ from psc.core.setcmd import render_changeset
13
+ from psc.output.errors import ErrorType, PscError
14
+ from psc.output.format import OutputFormat, render
15
+
16
+
17
+ def complete(rt: Runtime, cs: ChangeSet, *, apply: bool, out_path: str | None) -> None:
18
+ """Render `cs` in the chosen format, then apply it iff `apply`.
19
+
20
+ A blocked plan raises `CONFLICT` (exit 6) before any write. The structured
21
+ formats emit the whole plan (warnings + ops); table prints a readable
22
+ summary; `set` prints the PAN-OS script.
23
+ """
24
+ if cs.is_blocked:
25
+ raise PscError(
26
+ "plan blocked (unsafe): " + "; ".join(cs.blockers),
27
+ ErrorType.CONFLICT,
28
+ details={"blockers": cs.blockers, "warnings": cs.warnings},
29
+ )
30
+
31
+ fmt = rt.output
32
+ if fmt is OutputFormat.SET:
33
+ rt.stdout.print(
34
+ "\n".join(render_changeset(cs)), markup=False, highlight=False, soft_wrap=True
35
+ )
36
+ elif fmt in (OutputFormat.JSON, OutputFormat.JSONL, OutputFormat.YAML):
37
+ render(rt.stdout, fmt, model=cs)
38
+ else:
39
+ _print_human_plan(rt, cs)
40
+
41
+ if cs.is_empty:
42
+ rt.stderr.print("[dim]nothing to do[/dim]")
43
+ return
44
+
45
+ if apply:
46
+ result = rt.source().apply(cs, out_path=out_path)
47
+ rt.stderr.print(
48
+ f"[green]applied[/green] {result.ops} operation(s)"
49
+ + (f" → {result.out_path}" if result.out_path else "")
50
+ )
51
+ else:
52
+ rt.stderr.print("[yellow]dry-run[/yellow] — re-run with --apply to execute")
53
+
54
+
55
+ def _print_human_plan(rt: Runtime, cs: ChangeSet) -> None:
56
+ rt.stdout.print(f"[bold]{cs.title}[/bold]")
57
+ for w in cs.warnings:
58
+ rt.stdout.print(f" [yellow]! {w}[/yellow]")
59
+ if cs.is_empty:
60
+ return
61
+ for line in cs.summaries():
62
+ rt.stdout.print(f" • {line}")
psc/cli/app.py ADDED
@@ -0,0 +1,136 @@
1
+ """`psc` entry point: global options, the typed error contract, command wiring."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+
7
+ import click
8
+ import typer
9
+ from rich.console import Console
10
+
11
+ from psc import __version__
12
+ from psc.cli import dedup_cmds, find_cmds, name_cmds, profile_cmds, refs_cmds
13
+ from psc.cli.runtime import Runtime, configure_logging
14
+ from psc.config.loader import load_config
15
+ from psc.output.errors import PscError
16
+ from psc.output.format import OutputFormat
17
+
18
+ app = typer.Typer(
19
+ add_completion=True,
20
+ no_args_is_help=True,
21
+ rich_markup_mode="rich",
22
+ help="Agent-friendly Palo Alto Panorama object management — find, dedup, "
23
+ "merge, rename, and audit address/service objects. Dry-run by default.",
24
+ )
25
+
26
+ # Set by the root callback so the top-level error handler can honour -o json.
27
+ _ACTIVE: Runtime | None = None
28
+
29
+
30
+ def _version_cb(value: bool) -> None:
31
+ if value:
32
+ Console().print(f"psc {__version__}")
33
+ raise typer.Exit
34
+
35
+
36
+ @app.callback()
37
+ def root(
38
+ ctx: typer.Context,
39
+ config: str | None = typer.Option(
40
+ None,
41
+ "--config",
42
+ "-c",
43
+ help="Offline: path to an exported Panorama config XML.",
44
+ rich_help_panel="Source",
45
+ ),
46
+ profile: str | None = typer.Option(
47
+ None,
48
+ "--profile",
49
+ "-p",
50
+ help="Live: named profile from ~/.psc/config.yaml.",
51
+ rich_help_panel="Source",
52
+ ),
53
+ output: OutputFormat | None = typer.Option(
54
+ None,
55
+ "--output",
56
+ "-o",
57
+ help="Output format (default: table on a TTY, json when piped).",
58
+ rich_help_panel="Output",
59
+ ),
60
+ device_group: str | None = typer.Option(
61
+ None,
62
+ "--device-group",
63
+ "-d",
64
+ help="Scope to one device-group (plus inherited shared).",
65
+ rich_help_panel="Source",
66
+ ),
67
+ strict: bool = typer.Option(
68
+ False,
69
+ "--strict",
70
+ help="Exit non-zero when a lookup finds nothing.",
71
+ rich_help_panel="Safety",
72
+ ),
73
+ debug: bool = typer.Option(
74
+ False,
75
+ "--debug",
76
+ help="Verbose structured logs on stderr.",
77
+ rich_help_panel="Output",
78
+ ),
79
+ _version: bool = typer.Option(
80
+ False,
81
+ "--version",
82
+ callback=_version_cb,
83
+ is_eager=True,
84
+ help="Show version and exit.",
85
+ ),
86
+ ) -> None:
87
+ global _ACTIVE # noqa: PLW0603 — single process-wide handle for the error printer
88
+ configure_logging(debug)
89
+ rt = Runtime(
90
+ config=load_config(),
91
+ config_file=config,
92
+ profile=profile,
93
+ debug=debug,
94
+ device_group=device_group,
95
+ strict=strict,
96
+ _output=output,
97
+ )
98
+ ctx.obj = rt
99
+ _ACTIVE = rt
100
+
101
+
102
+ app.add_typer(find_cmds.app, name="find", help="Find objects by IP/value/name.")
103
+ app.add_typer(dedup_cmds.app, name="dedup", help="Find and merge duplicate objects.")
104
+ app.add_typer(refs_cmds.app, name="refs", help="Where-used, unused, and dangling references.")
105
+ app.add_typer(name_cmds.app, name="name", help="Naming-template lint and rename.")
106
+ app.add_typer(profile_cmds.app, name="profile", help="Manage live connection profiles.")
107
+
108
+
109
+ def _emit_error(err: PscError) -> None:
110
+ envelope = json.dumps(err.envelope(), ensure_ascii=False)
111
+ if _ACTIVE is not None and _ACTIVE.output is OutputFormat.JSON:
112
+ _ACTIVE.stdout.print(envelope, markup=False, highlight=False, soft_wrap=True)
113
+ else:
114
+ err_console = Console(stderr=True)
115
+ err_console.print(f"[red]error[/red]: {err.message}", highlight=False)
116
+ err_console.print(envelope, markup=False, highlight=False, soft_wrap=True)
117
+
118
+
119
+ def main() -> None:
120
+ try:
121
+ app(standalone_mode=False)
122
+ except PscError as err:
123
+ _emit_error(err)
124
+ raise SystemExit(err.exit_code) from None
125
+ except click.UsageError as err:
126
+ err.show()
127
+ raise SystemExit(2) from None
128
+ except (click.exceptions.Abort, KeyboardInterrupt):
129
+ Console(stderr=True).print("aborted")
130
+ raise SystemExit(130) from None
131
+ except click.exceptions.Exit as err:
132
+ raise SystemExit(err.exit_code) from None
133
+
134
+
135
+ if __name__ == "__main__":
136
+ main()