You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: AGENTS.md
+21Lines changed: 21 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,20 @@
2
2
3
3
Infrahub Python SDK - async/sync client for Infrahub infrastructure management.
4
4
5
+
## Product context
6
+
7
+
The SDK is the foundational library for programmatically interacting with Infrahub. It abstracts away the underlying API so developers can work with infrastructure data using native Python objects.
8
+
9
+
**Primary audience:** Network automation engineers and software developers.
10
+
11
+
**Three main use cases:**
12
+
13
+
-**Automate inside Infrahub** — Write transforms, generators, and checks that run as part of Infrahub's pipeline.
14
+
-**Integrate with external systems** — Query and sync data between Infrahub and existing tools. `infrahubctl` and the Infrahub Ansible collection both use this SDK internally.
15
+
-**Build custom applications** — Use Infrahub as a data backend for Python projects entirely outside of Infrahub's own pipeline.
16
+
17
+
**Why the SDK over direct API calls:** eliminates the need to learn Infrahub's API structure, provides Python-native interfaces with built-in auth, adds advanced capabilities (batching, caching, tracking), and reduces boilerplate.
18
+
5
19
## Commands
6
20
7
21
```bash
@@ -73,6 +87,13 @@ Key rules:
73
87
- Modify generated code (protocols.py)
74
88
- Bypass type checking without justification
75
89
90
+
## Knowledge base
91
+
92
+
Deep-dive docs on architecture and workflows live in `dev/knowledge/`. Read these before making changes to the areas they cover.
93
+
94
+
-[dev/knowledge/cli-architecture.md](dev/knowledge/cli-architecture.md) - CLI command hierarchy and design rules
95
+
-[dev/knowledge/doc-generation.md](dev/knowledge/doc-generation.md) - How docs are auto-generated from code
The `infrahubctl` CLI is built with [Typer](https://typer.tiangolo.com/) via a custom `AsyncTyper` subclass that supports async command functions.
4
+
5
+
## Entry point
6
+
7
+
The main Typer app lives in `infrahub_sdk/ctl/cli_commands.py`. It is re-exported through `infrahub_sdk/ctl/cli.py` which adds the `infrahubctl` entry point name.
8
+
9
+
## Command hierarchy
10
+
11
+
Commands are organized in two tiers:
12
+
13
+
-**Root commands** are registered directly on the main app with `app.command()`. These are standalone operations that don't belong to a logical group (e.g. `dump`, `load`, `check`, `render`, `run`, `transform`, `protocols`, `version`, `info`).
14
+
-**Subcommand groups** are separate `AsyncTyper()` instances registered with `app.add_typer(sub_app, name="group")`. Each group lives in its own module under `infrahub_sdk/ctl/`. Current groups: `branch`, `schema`, `validate`, `repository`, `menu`, `object`, `graphql`, `task`.
15
+
16
+
## Adding a new command
17
+
18
+
For a **root command**, define the function in the appropriate module and register it in `cli_commands.py`:
19
+
20
+
```python
21
+
app.command(name="mycommand")(my_function)
22
+
```
23
+
24
+
For a **subcommand**, add it to the relevant group's package or module. For example, object subcommands live in `infrahub_sdk/ctl/object/` and are registered on the object app in `__init__.py`.
25
+
26
+
## Group packages
27
+
28
+
When a subcommand group has multiple commands, it lives as a package (directory with `__init__.py`) rather than a single module file. The `object` group is the reference example:
Each command file contains a single command function. Shared logic goes in `utils.py`. The `__init__.py` wires everything together by importing and registering commands on the group's `AsyncTyper` app. Other groups that grow beyond a single file should follow this same pattern.
41
+
42
+
## Decorators
43
+
44
+
-`@catch_exception(console=console)` wraps commands for consistent error handling via Rich.
45
+
- Async commands work natively thanks to `AsyncTyper`.
46
+
47
+
## Design rules
48
+
49
+
**Always check if a new command belongs in an existing group before adding it at the root.** A command that operates on a specific resource type (objects, branches, schemas, etc.) should go under the matching subgroup, not at the top level. Root-level commands are reserved for cross-cutting or standalone operations (e.g. `run`, `version`, `info`).
50
+
51
+
When in doubt, look at what the command acts on and find the group that matches. For example, anything that creates, reads, updates, or deletes Infrahub objects belongs under `object`, not at the root.
- A **subcommand group** named `bar` produces `infrahubctl-bar.mdx` using:
26
+
`uv run typer infrahub_sdk.ctl.bar utils docs --name "infrahubctl bar"`
27
+
28
+
The group variant documents all subcommands within that group automatically.
29
+
30
+
### Key implication
31
+
32
+
Moving a command from root to a group (or vice versa) changes which mdx files get generated. The old files are cleaned up automatically by the glob delete, but the new ones only appear after running `docs-generate`. Always regenerate and commit the result.
33
+
34
+
## SDK documentation
35
+
36
+
Other doc generators cover SDK config, compatibility matrix, templates, and API reference. These are independent of CLI structure and are also triggered by `docs-generate`.
37
+
38
+
## Validation in CI
39
+
40
+
`docs-validate` diffs the generated output against the committed files. If they don't match, CI fails. This ensures docs stay in sync with code.
0 commit comments