@katmer/core 0.0.3
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.
- package/README.md +1 -0
- package/cli/katmer.js +28 -0
- package/cli/run.ts +16 -0
- package/index.ts +5 -0
- package/lib/config.ts +82 -0
- package/lib/interfaces/config.interface.ts +113 -0
- package/lib/interfaces/executor.interface.ts +13 -0
- package/lib/interfaces/module.interface.ts +170 -0
- package/lib/interfaces/provider.interface.ts +214 -0
- package/lib/interfaces/task.interface.ts +100 -0
- package/lib/katmer.ts +126 -0
- package/lib/lookup/env.lookup.ts +13 -0
- package/lib/lookup/file.lookup.ts +23 -0
- package/lib/lookup/index.ts +46 -0
- package/lib/lookup/url.lookup.ts +21 -0
- package/lib/lookup/var.lookup.ts +13 -0
- package/lib/module.ts +560 -0
- package/lib/module_registry.ts +64 -0
- package/lib/modules/apt-repository/apt-repository.module.ts +435 -0
- package/lib/modules/apt-repository/apt-sources-list.ts +363 -0
- package/lib/modules/apt.module.ts +546 -0
- package/lib/modules/archive.module.ts +280 -0
- package/lib/modules/become.module.ts +119 -0
- package/lib/modules/copy.module.ts +807 -0
- package/lib/modules/cron.module.ts +541 -0
- package/lib/modules/debug.module.ts +231 -0
- package/lib/modules/gather_facts.module.ts +605 -0
- package/lib/modules/git.module.ts +243 -0
- package/lib/modules/hostname.module.ts +213 -0
- package/lib/modules/http/http.curl.module.ts +342 -0
- package/lib/modules/http/http.local.module.ts +253 -0
- package/lib/modules/http/http.module.ts +298 -0
- package/lib/modules/index.ts +14 -0
- package/lib/modules/package.module.ts +283 -0
- package/lib/modules/script.module.ts +121 -0
- package/lib/modules/set_fact.module.ts +171 -0
- package/lib/modules/systemd_service.module.ts +373 -0
- package/lib/modules/template.module.ts +478 -0
- package/lib/providers/local.provider.ts +336 -0
- package/lib/providers/provider_response.ts +20 -0
- package/lib/providers/ssh/ssh.provider.ts +420 -0
- package/lib/providers/ssh/ssh.utils.ts +31 -0
- package/lib/schemas/katmer_config.schema.json +358 -0
- package/lib/target_resolver.ts +298 -0
- package/lib/task/controls/environment.control.ts +42 -0
- package/lib/task/controls/index.ts +13 -0
- package/lib/task/controls/loop.control.ts +89 -0
- package/lib/task/controls/register.control.ts +23 -0
- package/lib/task/controls/until.control.ts +64 -0
- package/lib/task/controls/when.control.ts +25 -0
- package/lib/task/task.ts +225 -0
- package/lib/utils/ajv.utils.ts +24 -0
- package/lib/utils/cls.ts +4 -0
- package/lib/utils/datetime.utils.ts +15 -0
- package/lib/utils/errors.ts +25 -0
- package/lib/utils/execute-shell.ts +116 -0
- package/lib/utils/file.utils.ts +68 -0
- package/lib/utils/http.utils.ts +10 -0
- package/lib/utils/json.utils.ts +15 -0
- package/lib/utils/number.utils.ts +9 -0
- package/lib/utils/object.utils.ts +11 -0
- package/lib/utils/os.utils.ts +31 -0
- package/lib/utils/path.utils.ts +9 -0
- package/lib/utils/renderer/render_functions.ts +3 -0
- package/lib/utils/renderer/renderer.ts +89 -0
- package/lib/utils/renderer/twig.ts +191 -0
- package/lib/utils/string.utils.ts +33 -0
- package/lib/utils/typed-event-emitter.ts +26 -0
- package/lib/utils/unix.utils.ts +91 -0
- package/lib/utils/windows.utils.ts +92 -0
- package/package.json +67 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
// modules/debug.module.ts
|
|
2
|
+
import {
|
|
3
|
+
type ModuleCommonReturn,
|
|
4
|
+
type ModuleConstraints
|
|
5
|
+
} from "../interfaces/module.interface"
|
|
6
|
+
import type { Katmer } from "../interfaces/task.interface"
|
|
7
|
+
import type { KatmerProvider } from "../interfaces/provider.interface"
|
|
8
|
+
import { get } from "es-toolkit/compat"
|
|
9
|
+
import { evalTemplate } from "../utils/renderer/renderer"
|
|
10
|
+
import { wrapInArray } from "../utils/json.utils"
|
|
11
|
+
import { KatmerModule } from "../module"
|
|
12
|
+
declare module "../interfaces/task.interface" {
|
|
13
|
+
export namespace Katmer {
|
|
14
|
+
export interface TaskActions {
|
|
15
|
+
debug?: DebugModuleOptions | string | string[]
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Print messages and/or variable values for troubleshooting.
|
|
21
|
+
*
|
|
22
|
+
* @remarks
|
|
23
|
+
* - Accepts either a **string shorthand** (printed as a message) or a **full options object**.
|
|
24
|
+
* - `msg` entries support template expressions rendered against `ctx.variables` (via `evalExpr`).
|
|
25
|
+
* - When `var` is provided, values are looked up from `ctx.variables` using dot-paths (e.g. `"app.version"`).
|
|
26
|
+
* - If nothing is provided, prints a default `"ok"` line.
|
|
27
|
+
*
|
|
28
|
+
* @examples
|
|
29
|
+
* ```yaml
|
|
30
|
+
* # String shorthand
|
|
31
|
+
* - name: quick debug
|
|
32
|
+
* debug: "Hello from Katmer 🎉"
|
|
33
|
+
*
|
|
34
|
+
* # Multiple messages with expression evaluation
|
|
35
|
+
* - name: deployment banner
|
|
36
|
+
* debug:
|
|
37
|
+
* msg:
|
|
38
|
+
* - "deploying {{ app.name }}"
|
|
39
|
+
* - "version {{ app.version }}"
|
|
40
|
+
*
|
|
41
|
+
* # Show specific variables (dot-paths allowed)
|
|
42
|
+
* - name: print variables
|
|
43
|
+
* debug:
|
|
44
|
+
* label: "context"
|
|
45
|
+
* var:
|
|
46
|
+
* - "app.name"
|
|
47
|
+
* - "env.stage"
|
|
48
|
+
*
|
|
49
|
+
* # Inline values and compact output
|
|
50
|
+
* - name: inline map (no pretty)
|
|
51
|
+
* debug:
|
|
52
|
+
* pretty: false
|
|
53
|
+
* vars:
|
|
54
|
+
* region: "eu-east"
|
|
55
|
+
* replicas: 3
|
|
56
|
+
*
|
|
57
|
+
* # Quiet mode (no logger output, result only)
|
|
58
|
+
* - name: return only (no logs)
|
|
59
|
+
* debug:
|
|
60
|
+
* msg: "this is silent"
|
|
61
|
+
* quiet: true
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export class DebugModule extends KatmerModule<
|
|
65
|
+
DebugModuleOptions,
|
|
66
|
+
DebugModuleResult,
|
|
67
|
+
KatmerProvider
|
|
68
|
+
> {
|
|
69
|
+
static name = "debug" as const
|
|
70
|
+
|
|
71
|
+
constraints = {
|
|
72
|
+
platform: {
|
|
73
|
+
any: true
|
|
74
|
+
}
|
|
75
|
+
} satisfies ModuleConstraints
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Validate parameters (always allowed; empty input prints "ok").
|
|
79
|
+
*/
|
|
80
|
+
async check(_ctx: Katmer.TaskContext<KatmerProvider>): Promise<void> {}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Initialize resources (no-op).
|
|
84
|
+
*/
|
|
85
|
+
async initialize(_ctx: Katmer.TaskContext<KatmerProvider>): Promise<void> {}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Cleanup resources (no-op).
|
|
89
|
+
*/
|
|
90
|
+
async cleanup(_ctx: Katmer.TaskContext<KatmerProvider>): Promise<void> {}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Render messages and/or variable values and log them.
|
|
94
|
+
*
|
|
95
|
+
* @param ctx - Task context, whose `variables` are used for expression evaluation and lookups.
|
|
96
|
+
* @returns A {@link DebugModuleResult} containing the final printed text and the structured values.
|
|
97
|
+
*/
|
|
98
|
+
async execute(
|
|
99
|
+
ctx: Katmer.TaskContext<KatmerProvider>
|
|
100
|
+
): Promise<DebugModuleResult> {
|
|
101
|
+
const opts = this.#normalizeParams(this.params)
|
|
102
|
+
|
|
103
|
+
const {
|
|
104
|
+
msg,
|
|
105
|
+
var: varNames,
|
|
106
|
+
vars,
|
|
107
|
+
label,
|
|
108
|
+
level = "info",
|
|
109
|
+
pretty = true,
|
|
110
|
+
changed = false,
|
|
111
|
+
quiet = false
|
|
112
|
+
} = opts
|
|
113
|
+
|
|
114
|
+
const values: Record<string, unknown> = {}
|
|
115
|
+
const lines: string[] = []
|
|
116
|
+
|
|
117
|
+
if (label) lines.push(String(label))
|
|
118
|
+
|
|
119
|
+
// Messages (with expression evaluation)
|
|
120
|
+
const messages = wrapInArray(msg)
|
|
121
|
+
for (const m of messages) {
|
|
122
|
+
if (m) {
|
|
123
|
+
lines.push(await evalTemplate(m, ctx.variables || {}))
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// var: string | string[]
|
|
128
|
+
if (typeof varNames === "string") {
|
|
129
|
+
values[varNames] = get(ctx.variables ?? {}, varNames)
|
|
130
|
+
} else if (Array.isArray(varNames)) {
|
|
131
|
+
for (const key of varNames) {
|
|
132
|
+
values[key] = get(ctx.variables ?? {}, key)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// vars: inline object
|
|
137
|
+
if (vars && typeof vars === "object") {
|
|
138
|
+
for (const [k, v] of Object.entries(vars)) values[k] = v
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Append structured values
|
|
142
|
+
if (Object.keys(values).length > 0) {
|
|
143
|
+
lines.push(this.#formatValue(values, pretty))
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (lines.length === 0) lines.push("ok")
|
|
147
|
+
|
|
148
|
+
const output = lines.join("\n")
|
|
149
|
+
|
|
150
|
+
// Log unless quiet
|
|
151
|
+
if (!quiet) {
|
|
152
|
+
const logger = ctx.logger as any
|
|
153
|
+
if (logger?.[level]) logger[level](output)
|
|
154
|
+
else if (typeof ctx.log === "function") ctx.log(level as any, output)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
ctx.logger.info({
|
|
158
|
+
msg: output,
|
|
159
|
+
values
|
|
160
|
+
})
|
|
161
|
+
return {}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
#normalizeParams(
|
|
165
|
+
p?: DebugModuleOptions | string | string[]
|
|
166
|
+
): DebugModuleOptions {
|
|
167
|
+
if (typeof p === "string") return { msg: p }
|
|
168
|
+
if (Array.isArray(p)) return { msg: p }
|
|
169
|
+
return p ?? { msg: "Hello World" }
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
#formatValue(val: unknown, pretty: boolean): string {
|
|
173
|
+
try {
|
|
174
|
+
if (pretty) return JSON.stringify(val, null, 2)
|
|
175
|
+
return typeof val === "string" ? val : JSON.stringify(val)
|
|
176
|
+
} catch {
|
|
177
|
+
return String(val)
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Options for the debug module.
|
|
184
|
+
* @public
|
|
185
|
+
*/
|
|
186
|
+
export interface DebugModuleOptions {
|
|
187
|
+
/**
|
|
188
|
+
* Text to print. Accepts a single string or an array of strings.
|
|
189
|
+
* Entries support expression syntax.
|
|
190
|
+
*/
|
|
191
|
+
msg: string | string[]
|
|
192
|
+
/**
|
|
193
|
+
* Variable name or list of names to print from variables.
|
|
194
|
+
* Dot-paths are supported, e.g. `"app.version"`.
|
|
195
|
+
*/
|
|
196
|
+
var?: string | string[]
|
|
197
|
+
/**
|
|
198
|
+
* Inline values to include in the output. Printed as JSON (pretty by default).
|
|
199
|
+
*/
|
|
200
|
+
vars?: Record<string, unknown>
|
|
201
|
+
/**
|
|
202
|
+
* Optional header line prepended to the output.
|
|
203
|
+
*/
|
|
204
|
+
label?: string
|
|
205
|
+
/**
|
|
206
|
+
* Pretty-print JSON output for objects/maps.
|
|
207
|
+
* @defaultValue true
|
|
208
|
+
*/
|
|
209
|
+
pretty?: boolean
|
|
210
|
+
/**
|
|
211
|
+
* Log level.
|
|
212
|
+
* @defaultValue "info"
|
|
213
|
+
*/
|
|
214
|
+
level?: "fatal" | "error" | "warn" | "info" | "debug" | "trace"
|
|
215
|
+
/**
|
|
216
|
+
* If `true`, do not log—only return the output.
|
|
217
|
+
* @defaultValue false
|
|
218
|
+
*/
|
|
219
|
+
quiet?: boolean
|
|
220
|
+
/**
|
|
221
|
+
* Force the returned `changed` flag in the result.
|
|
222
|
+
* @defaultValue false
|
|
223
|
+
*/
|
|
224
|
+
changed?: boolean
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Result of debug module execution.
|
|
229
|
+
* @public
|
|
230
|
+
*/
|
|
231
|
+
export interface DebugModuleResult extends ModuleCommonReturn {}
|