Skip to content

Commit 3a4a2df

Browse files
committed
proper-docs2
1 parent 9807b9a commit 3a4a2df

File tree

1,042 files changed

+77354
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,042 files changed

+77354
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
## Package main (github.com/redhat-best-practices-for-k8s/certsuite/cmd/certsuite)
2+
3+
# certsuite – Command‑line entry point
4+
5+
`cmd/certsuite/main.go` is the sole executable package of the **certsuite** tool.
6+
It wires together a set of sub‑commands (check, claim, generate, info, run,
7+
upload, version) and hands control over to Cobra’s command dispatcher.
8+
9+
---
10+
11+
## Core functions
12+
13+
| Function | Purpose | Key interactions |
14+
|----------|---------|------------------|
15+
| `newRootCmd() *cobra.Command` | Builds the top‑level command tree. | Calls `NewCommand()` from each sub‑package (`check`, `claim`, …) and registers them via `AddCommand`. |
16+
| `main()` | Entry point for the binary. | Instantiates the root command, executes it with `Execute()`, logs any errors (`log.Error`) and exits with `os.Exit(1)` on failure. |
17+
18+
---
19+
20+
## How the command tree is built
21+
22+
```mermaid
23+
graph TD;
24+
main --> newRootCmd
25+
newRootCmd -->|AddCommand| check.NewCommand()
26+
newRootCmd --> claim.NewCommand()
27+
newRootCmd --> generate.NewCommand()
28+
newRootCmd --> info.NewCommand()
29+
newRootCmd --> run.NewCommand()
30+
newRootCmd --> upload.NewCommand()
31+
newRootCmd --> version.NewCommand()
32+
```
33+
34+
Each `NewCommand()` returns a fully configured `*cobra.Command` that knows how
35+
to parse its own flags and perform the requested action.
36+
The root command simply acts as a dispatcher; all heavy lifting happens in
37+
those sub‑packages.
38+
39+
---
40+
41+
## Error handling
42+
43+
```go
44+
if err := cmd.Execute(); err != nil {
45+
log.Error(err)
46+
os.Exit(1)
47+
}
48+
```
49+
50+
* `log` is an internal wrapper around the standard logger, providing a unified
51+
error output format.
52+
* On any failure during command parsing or execution the program terminates with
53+
status code **1**.
54+
55+
---
56+
57+
## Summary
58+
59+
- The package contains only two functions: `main()` and `newRootCmd()`.
60+
- `newRootCmd` constructs a Cobra root command and attaches sub‑commands from
61+
dedicated packages.
62+
- `main` runs the command tree, logs errors, and exits appropriately.
63+
64+
This design keeps the executable thin; all domain logic lives in the
65+
sub‑packages, making the CLI straightforward to maintain and extend.
66+
67+
### Functions
68+
69+
70+
### Call graph (exported symbols, partial)
71+
72+
```mermaid
73+
graph LR
74+
```
75+
76+
### Symbol docs
77+
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
## Package check (github.com/redhat-best-practices-for-k8s/certsuite/cmd/certsuite/check)
2+
3+
# Package `check`
4+
5+
The **`check`** package is a thin wrapper that stitches together the CLI commands for CertSuite’s
6+
*image‑certificate* checks and result handling.
7+
It lives under `github.com/redhat-best-practices-for-k8s/certsuite/cmd/certsuite/check`.
8+
9+
---
10+
11+
## Global state
12+
13+
| Name | Type (inferred) | Purpose |
14+
|-----------|-----------------|---------|
15+
| `checkCmd` | `*cobra.Command` | Root command that will be returned by `NewCommand`. It is the entry point for all sub‑commands added in this package. |
16+
17+
> The variable is unexported because it is only used internally when constructing the root
18+
> command tree.
19+
20+
---
21+
22+
## Key function
23+
24+
### `NewCommand() *cobra.Command`
25+
26+
```go
27+
func NewCommand() *cobra.Command
28+
```
29+
30+
* **What it does**
31+
- Instantiates a new `checkCmd` root command (via its zero value, which is a `cobra.Command`).
32+
- Adds sub‑commands from the two internal packages:
33+
- `image_cert_status.NewCommand()` – handles checks against image certificates.
34+
- `results.NewCommand()` – deals with displaying or storing test results.
35+
- Returns the fully built command tree so that it can be wired into the top‑level CertSuite CLI.
36+
37+
* **How it works**
38+
39+
```go
40+
func NewCommand() *cobra.Command {
41+
// Add subcommands to the root
42+
checkCmd.AddCommand(image_cert_status.NewCommand())
43+
checkCmd.AddCommand(results.NewCommand())
44+
45+
return checkCmd
46+
}
47+
```
48+
49+
Each `AddCommand` call registers a new child command, allowing users to run e.g.
50+
51+
```bash
52+
certsuite check image-cert-status … # delegated to image_cert_status package
53+
certsuite check results … # delegated to results package
54+
```
55+
56+
* **Dependencies**
57+
58+
| Dependency | Why it is needed |
59+
|------------|-----------------|
60+
| `github.com/spf13/cobra` | Provides the command‑line framework. |
61+
| `image_cert_status.NewCommand()` | Supplies the image‑certificate check subcommand. |
62+
| `results.NewCommand()` | Supplies the results subcommand. |
63+
64+
---
65+
66+
## How everything connects
67+
68+
```
69+
certsuite (top level)
70+
└── check
71+
├─ NewCommand() → returns *cobra.Command
72+
│ ├─ rootCmd := checkCmd
73+
│ ├─ add image_cert_status.NewCommand()
74+
│ └─ add results.NewCommand()
75+
└── sub‑commands:
76+
├─ image-cert-status (from image_cert_status package)
77+
└─ results (from results package)
78+
```
79+
80+
The `check` command acts purely as a *command router*; it does not contain business logic itself.
81+
All real functionality lives in the two imported packages, each of which follows the same
82+
Cobra pattern for defining flags and execution logic.
83+
84+
---
85+
86+
## Summary
87+
88+
- **Global**: `checkCmd` – the root Cobra command.
89+
- **Function**: `NewCommand()` builds the command tree by attaching sub‑commands from
90+
`image_cert_status` and `results`.
91+
- The package itself is a lightweight glue layer; all heavy lifting happens in its child packages.
92+
93+
### Functions
94+
95+
- **NewCommand** — func()(*cobra.Command)
96+
97+
### Globals
98+
99+
100+
### Call graph (exported symbols, partial)
101+
102+
```mermaid
103+
graph LR
104+
NewCommand --> AddCommand
105+
NewCommand --> AddCommand
106+
```
107+
108+
### Symbol docs
109+
110+
- [function NewCommand](symbols/function_NewCommand.md)
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
## Package imagecert (github.com/redhat-best-practices-for-k8s/certsuite/cmd/certsuite/check/image_cert_status)
2+
3+
## `imagecert` Package – Overview
4+
5+
| Topic | Summary |
6+
|-------|---------|
7+
| **Purpose** | Implements the `check image-cert-status` command for CertSuite, reporting whether a container image is certified and what policy it satisfies. |
8+
| **Key Exported API** | `NewCommand() *cobra.Command` – creates the Cobra command that gets wired into CertSuite’s CLI tree. |
9+
| **Internal State** | A single unexported global variable: `checkImageCertStatusCmd`, a `*cobra.Command`. |
10+
11+
---
12+
13+
### Global Variable
14+
15+
```go
16+
var checkImageCertStatusCmd *cobra.Command
17+
```
18+
19+
- Holds the command instance created by `NewCommand`.
20+
- Used only internally; not exposed to other packages.
21+
22+
---
23+
24+
### Core Functions
25+
26+
#### 1. `checkImageCertStatus(cmd *cobra.Command, args []string) error`
27+
28+
* **Role** – The actual execution logic for the command.
29+
* **Workflow**:
30+
1. **Read Flags** – Uses `cmd.Flags().GetString(...)` to obtain values for
31+
- `image`
32+
- `registry-url`
33+
- `policy-name`
34+
- `policy-version`
35+
2. **Validation** – Calls `cmd.GetValidator()` (from the `certdb` package) and checks that the provided image is certified:
36+
```go
37+
if err := validator.IsContainerCertified(image, registryURL, policyName, policyVersion); err != nil {
38+
// error handling
39+
}
40+
```
41+
3. **Output** – Prints human‑readable status using `fmt` and color helpers from `github.com/fatih/color`.
42+
- Certified → green “YES”
43+
- Not certified → red “NO” plus reason.
44+
4. **Error Handling** – Returns descriptive errors for missing flags or validation failures.
45+
46+
#### 2. `NewCommand() *cobra.Command`
47+
48+
* **Role** – Constructs and configures the Cobra command that will be registered under CertSuite’s CLI tree.
49+
* **Key Steps**:
50+
- Instantiates `checkImageCertStatusCmd` with usage, short description, and the RunE handler set to `checkImageCertStatus`.
51+
- Declares persistent flags (`image`, `registry-url`, `policy-name`, `policy-version`) using `cmd.PersistentFlags().String(...)`.
52+
- Enforces flag rules:
53+
* `MarkFlagsRequiredTogether` – ensures that `image` is always supplied with the other three flags.
54+
* `MarkFlagsMutuallyExclusive` – guarantees no conflicting combinations (e.g., image vs. registry only).
55+
* **Return** – The fully configured command pointer.
56+
57+
---
58+
59+
### How Things Connect
60+
61+
```mermaid
62+
graph TD;
63+
A[User runs: certsuite check image-cert-status] --> B[NewCommand registers cmd];
64+
B --> C[checkImageCertStatus executes on RunE];
65+
C --> D{Read Flags};
66+
D --> E[Validator.IsContainerCertified];
67+
E -- certified --> F[Print green YES];
68+
E -- not certified --> G[Print red NO + reason];
69+
F & G --> H[Return nil or error];
70+
```
71+
72+
1. **CLI entry point** (`certsuite check image-cert-status`) triggers Cobra’s command lookup.
73+
2. `NewCommand` supplies the command definition to Cobra during initialization.
74+
3. When executed, `checkImageCertStatus` pulls flag values, delegates certification logic to `certdb.Validator`, and formats output.
75+
76+
---
77+
78+
### Dependencies
79+
80+
| Package | Role |
81+
|---------|------|
82+
| `github.com/fatih/color` | ANSI color helpers for terminal output. |
83+
| `github.com/redhat-best-practices-for-k8s/oct/pkg/certdb` | Provides the `Validator` interface and certification logic. |
84+
| `github.com/spf13/cobra` | CLI framework; handles command registration, flag parsing, and execution flow. |
85+
86+
---
87+
88+
### Summary
89+
90+
The `imagecert` package is a thin wrapper around CertSuite’s validation engine that exposes a user‑friendly CLI subcommand. It relies on Cobra for argument handling and `certdb.Validator` for the actual certification check, presenting results with colorized console output. The design keeps all state local to the command instance (`checkImageCertStatusCmd`) and offers no exported types beyond the constructor.
91+
92+
### Functions
93+
94+
- **NewCommand** — func()(*cobra.Command)
95+
96+
### Globals
97+
98+
99+
### Call graph (exported symbols, partial)
100+
101+
```mermaid
102+
graph LR
103+
NewCommand --> String
104+
NewCommand --> PersistentFlags
105+
NewCommand --> String
106+
NewCommand --> PersistentFlags
107+
NewCommand --> String
108+
NewCommand --> PersistentFlags
109+
NewCommand --> String
110+
NewCommand --> PersistentFlags
111+
```
112+
113+
### Symbol docs
114+
115+
- [function NewCommand](symbols/function_NewCommand.md)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
NewCommand` – CLI entry point for the *image‑cert‑status* sub‑command
2+
3+
```go
4+
func NewCommand() (*cobra.Command)
5+
```
6+
7+
---
8+
9+
## Purpose
10+
Creates and configures a **Cobra** command that checks the status of container image certificates.
11+
The returned `*cobra.Command` is intended to be added to the top‑level *check* command of the CertSuite CLI.
12+
13+
---
14+
15+
## Inputs / Outputs
16+
17+
| Parameter | Type | Description |
18+
|-----------|------|-------------|
19+
| none | – | The function receives no arguments; all configuration comes from package‑wide defaults and flag values. |
20+
21+
| Return value | Type | Meaning |
22+
|--------------|------|---------|
23+
| `*cobra.Command` | pointer to a Cobra command | Fully configured command ready for execution. Returns `nil` only if an internal error occurs (which never happens in the current implementation). |
24+
25+
---
26+
27+
## Key Flag Configuration
28+
29+
The function registers **six persistent string flags** on the command:
30+
31+
| Flag name | Description |
32+
|-----------|-------------|
33+
| `--image-name` | Target image name to inspect. |
34+
| `--namespace` | Kubernetes namespace where the image is deployed. |
35+
| `--pod-name` | Pod name that runs the target container. |
36+
| `--container-name` | Specific container inside the pod. |
37+
| `--cluster-url` | URL of the cluster API server (optional; may default to in‑cluster config). |
38+
| `--ca-file` | Path to a custom CA bundle for TLS verification. |
39+
40+
> **Flag relationships**
41+
> *All six flags are required together* – the command will fail if any subset is omitted.
42+
> Flags are also marked as *mutually exclusive*, preventing conflicting combinations (e.g., specifying both `--pod-name` and `--container-name` without a namespace).
43+
44+
These constraints are enforced via:
45+
46+
```go
47+
MarkFlagsRequiredTogether(...)
48+
MarkFlagsMutuallyExclusive(...)
49+
```
50+
51+
---
52+
53+
## Dependencies & Side Effects
54+
55+
| Dependency | Role |
56+
|------------|------|
57+
| `github.com/spf13/cobra` | Provides the command structure, flag handling, and execution plumbing. |
58+
| Global variable `checkImageCertStatusCmd` | Holds the constructed command; used elsewhere in the package to register sub‑commands. |
59+
60+
The function has **no observable side effects** beyond populating the global variable. It does not modify any external state or perform I/O.
61+
62+
---
63+
64+
## Package Context
65+
66+
*Package*: `imagecert` (path:
67+
`github.com/redhat-best-practices-for-k8s/certsuite/cmd/certsuite/check/image_cert_status`)
68+
69+
This package implements a single sub‑command of the CertSuite CLI that verifies whether a container image has an appropriate certificate attached. `NewCommand` is the public entry point that creates this command; callers typically invoke it during application bootstrap:
70+
71+
```go
72+
checkCmd.AddCommand(imagecert.NewCommand())
73+
```
74+
75+
---
76+
77+
## Suggested Mermaid Diagram (Optional)
78+
79+
```mermaid
80+
graph TD
81+
A[CertSuite CLI] --> B[check Command]
82+
B --> C[image_cert_status.Command]
83+
C -.-> D[Flags: image-name, namespace, pod-name, container-name, cluster-url, ca-file]
84+
```
85+
86+
This visual clarifies how the command fits into the overall CLI hierarchy.

0 commit comments

Comments
 (0)