Skip to content

Commit d5d93d4

Browse files
📝 Add docstrings to k8s-cli
Docstrings generation was requested by @JakeSCahill. * #103 (comment) The following files were modified: * `bin/doc-tools.js` * `cli-utils/convert-doc-links.js` * `cli-utils/self-managed-docs-branch.js`
1 parent 3b4b49c commit d5d93d4

File tree

3 files changed

+81
-21
lines changed

3 files changed

+81
-21
lines changed

bin/doc-tools.js

100755100644
Lines changed: 63 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ const fetchFromGithub = require('../tools/fetch-from-github.js');
1010
const { urlToXref } = require('../cli-utils/convert-doc-links.js');
1111

1212

13+
/**
14+
* Searches upward from a starting directory to locate the repository root.
15+
*
16+
* Traverses parent directories from the specified start path, returning the first directory containing either a `.git` folder or a `package.json` file. Exits the process with an error if no such directory is found.
17+
*
18+
* @param {string} [start] - The directory to begin the search from. Defaults to the current working directory.
19+
* @returns {string} The absolute path to the repository root directory.
20+
*/
1321
function findRepoRoot(start = process.cwd()) {
1422
let dir = start;
1523
while (dir !== path.parse(dir).root) {
@@ -26,19 +34,25 @@ function findRepoRoot(start = process.cwd()) {
2634

2735
// --------------------------------------------------------------------
2836
// Dependency check functions
29-
// --------------------------------------------------------------------
37+
/**
38+
* Prints an error message to stderr and exits the process with a non-zero status.
39+
*
40+
* @param {string} msg - The error message to display before exiting.
41+
*/
3042
function fail(msg) {
3143
console.error(`❌ ${msg}`);
3244
process.exit(1);
3345
}
3446

3547
/**
36-
* Ensure a tool is installed (and optionally that it responds to a version flag).
48+
* Ensures that a specified command-line tool is installed and operational.
49+
*
50+
* Attempts to execute the tool with a version flag to verify its presence. If the tool is missing or fails to run, the process exits with an error message and optional installation hint.
3751
*
38-
* @param {string} cmd The binary name (such as 'docker', 'helm-docs')
39-
* @param {object} [opts]
40-
* @param {string} [opts.versionFlag='--version'] What to append to check it runs
41-
* @param {string} [opts.help] A one-liner hint (URL or install command)
52+
* @param {string} cmd - The name of the tool to check (e.g., 'docker', 'helm-docs').
53+
* @param {object} [opts] - Optional settings.
54+
* @param {string} [opts.versionFlag='--version'] - The flag used to test the tool's execution.
55+
* @param {string} [opts.help] - An optional hint or installation instruction shown on failure.
4256
*/
4357
function requireTool(cmd, { versionFlag = '--version', help = '' } = {}) {
4458
try {
@@ -49,14 +63,28 @@ function requireTool(cmd, { versionFlag = '--version', help = '' } = {}) {
4963
}
5064
}
5165

52-
// Simple existence only (no version flag)
66+
/**
67+
* Ensures that a command-line tool is installed by checking if it responds to the `--help` flag.
68+
*
69+
* @param {string} cmd - The name of the command-line tool to check.
70+
* @param {string} [help] - Optional help text to display if the tool is not found.
71+
*
72+
* @throws {Error} If the specified command is not found or does not respond to `--help`.
73+
*/
5374
function requireCmd(cmd, help) {
5475
requireTool(cmd, { versionFlag: '--help', help });
5576
}
5677

5778
// --------------------------------------------------------------------
5879
// Special validators
59-
// --------------------------------------------------------------------
80+
/**
81+
* Ensures that Python with a minimum required version is installed and available in the system PATH.
82+
*
83+
* Checks for either `python3` or `python` executables and verifies that the version is at least the specified minimum (default: 3.10). Exits the process with an error message if the requirement is not met.
84+
*
85+
* @param {number} [minMajor=3] - Minimum required major version of Python.
86+
* @param {number} [minMinor=10] - Minimum required minor version of Python.
87+
*/
6088

6189
function requirePython(minMajor = 3, minMinor = 10) {
6290
const candidates = ['python3', 'python'];
@@ -77,6 +105,11 @@ function requirePython(minMajor = 3, minMinor = 10) {
77105
`\n→ install with your package manager, or https://python.org`);
78106
}
79107

108+
/**
109+
* Ensures that the Docker CLI is installed and the Docker daemon is running.
110+
*
111+
* @throws {Error} If Docker is not installed or the Docker daemon is not running.
112+
*/
80113
function requireDockerDaemon() {
81114
requireTool('docker', { help: 'https://docs.docker.com/get-docker/' });
82115
try {
@@ -88,19 +121,33 @@ function requireDockerDaemon() {
88121

89122
// --------------------------------------------------------------------
90123
// Grouped checks
91-
// --------------------------------------------------------------------
124+
/**
125+
* Ensures that required dependencies for generating CRD documentation are installed.
126+
*
127+
* Verifies the presence of the {@link git} and {@link crd-ref-docs} command-line tools, exiting the process with an error message if either is missing.
128+
*/
92129

93130
function verifyCrdDependencies() {
94131
requireCmd('git', 'install Git: https://git-scm.com/downloads');
95132
requireCmd('crd-ref-docs', 'https://github.com/elastic/crd-ref-docs');
96133
}
97134

135+
/**
136+
* Ensures that all required tools for Helm documentation generation are installed.
137+
*
138+
* Checks for the presence of `helm-docs`, `pandoc`, and `git`, exiting the process with an error if any are missing.
139+
*/
98140
function verifyHelmDependencies() {
99141
requireCmd('helm-docs', 'https://github.com/norwoodj/helm-docs');
100142
requireCmd('pandoc', 'brew install pandoc or https://pandoc.org');
101143
requireCmd('git', 'install Git: https://git-scm.com/downloads');
102144
}
103145

146+
/**
147+
* Ensures all dependencies required for generating property documentation are installed.
148+
*
149+
* Checks for the presence of `make`, Python 3.10 or newer, and at least one C++ compiler (`gcc` or `clang`). Exits the process with an error message if any dependency is missing.
150+
*/
104151
function verifyPropertyDependencies() {
105152
requireCmd('make', 'your OS package manager');
106153
requirePython();
@@ -112,6 +159,13 @@ function verifyPropertyDependencies() {
112159
}
113160
}
114161

162+
/**
163+
* Ensures all required dependencies for generating Redpanda metrics documentation are installed.
164+
*
165+
* Verifies that Python 3.10+, `curl`, and `tar` are available, and that the Docker daemon is running.
166+
*
167+
* @throws {Error} If any required dependency is missing or the Docker daemon is not running.
168+
*/
115169
function verifyMetricsDependencies() {
116170
requirePython();
117171
requireCmd('curl');

cli-utils/convert-doc-links.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
const { URL } = require('url');
22

33
/**
4-
* Convert a docs.redpanda.com URL (optionally suffixed with “[label]”) into
5-
* an Antora xref resource ID, preserving that label in brackets.
4+
* Converts a docs.redpanda.com URL, optionally suffixed with a label in brackets, into an Antora xref resource ID string.
65
*
7-
* Example:
8-
* urlToXref('https://docs.redpanda.com/v25.1/reference/configuration/[Config]')
9-
* // → 'xref:reference:configuration.adoc[Config]'
6+
* If the input includes a label in square brackets (e.g., `[Label]`), the label is preserved and appended to the resulting xref.
107
*
11-
* @param {string} input
12-
* @returns {string}
8+
* @param {string} input - A docs.redpanda.com URL, optionally followed by a label in square brackets.
9+
* @returns {string} The corresponding Antora xref resource ID, with the label preserved if present.
10+
*
11+
* @throws {Error} If the input is not a valid URL or does not belong to docs.redpanda.com.
1312
*/
1413
function urlToXref(input) {
1514
// Peel off an optional “[label]”

cli-utils/self-managed-docs-branch.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ const yaml = require('js-yaml');
33
const { spawnSync } = require('child_process');
44

55
/**
6-
* Fetches the latest documented Self-Managed version from the remote docs repo's antora.yml
7-
* @returns {Promise<string>} example: "25.1"
6+
* Retrieves the current Self-Managed documentation version from the remote antora.yml file.
7+
*
8+
* @returns {Promise<string>} Resolves with the version string (e.g., "25.1").
9+
*
10+
* @throws {Error} If the antora.yml file cannot be fetched, parsed, or if the version field is missing.
811
*/
912
function fetchRemoteAntoraVersion() {
1013
const url = 'https://raw.githubusercontent.com/redpanda-data/docs/main/antora.yml'
@@ -32,10 +35,14 @@ function fetchRemoteAntoraVersion() {
3235
}
3336

3437
/**
35-
* Given an operator tag (such as "operator/v25.1.2" or "v25.1.2") and
36-
* the remote Antora version (such as "25.1"), decide which docs branch to use.
38+
* Determines the appropriate documentation branch for a given operator tag based on the remote Antora version.
39+
*
40+
* Normalizes the input tag, extracts the major.minor version, and applies version-specific logic to select the correct branch. Verifies that the chosen branch exists in the remote repository.
41+
*
42+
* @param {string} operatorTag - The operator tag to evaluate (e.g., "operator/v25.1.2" or "v25.1.2").
43+
* @returns {Promise<string>} The name of the documentation branch to use.
3744
*
38-
* Throws if the chosen branch does not exist on origin.
45+
* @throws {Error} If the tag cannot be parsed or if the determined branch does not exist in the remote repository.
3946
*/
4047
async function determineDocsBranch(operatorTag) {
4148
// Strip any "operator/" prefix

0 commit comments

Comments
 (0)