-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathoutput.mts
More file actions
203 lines (187 loc) · 5.08 KB
/
output.mts
File metadata and controls
203 lines (187 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* Dry-run output utilities for Socket CLI commands.
*
* Provides standardized output formatting for dry-run mode that shows users
* what actions WOULD be performed without actually executing them.
*
* Output routes through stderr when the caller engaged no-log mode
* (`--no-log`) or asked for a machine-readable output stream, so dry-run
* preview text never pollutes `--json` / `--markdown` payloads piped to
* other tools. Otherwise stays on stdout where humans expect it.
*/
import { getDefaultLogger } from '@socketsecurity/lib/logger'
import { DRY_RUN_LABEL } from '../../constants/cli.mts'
import { isNoLogMode } from '../output/no-log.mts'
const logger = getDefaultLogger()
// Route to stderr only when the user asked for automation-friendly
// output. Keeps the human-readable default on stdout so existing
// interactive workflows and their tests are unaffected.
function out(message: string): void {
if (isNoLogMode()) {
logger.error(message)
} else {
logger.log(message)
}
}
export interface DryRunAction {
type:
| 'create'
| 'delete'
| 'execute'
| 'fetch'
| 'modify'
| 'upload'
| 'write'
description: string
target?: string
details?: Record<string, unknown>
}
export interface DryRunPreview {
summary: string
actions: DryRunAction[]
wouldSucceed?: boolean
}
/**
* Format and output a dry-run preview.
*/
export function outputDryRunPreview(preview: DryRunPreview): void {
out('')
out(`${DRY_RUN_LABEL}: ${preview.summary}`)
out('')
if (!preview.actions.length) {
out(' No actions would be performed.')
} else {
out(' Actions that would be performed:')
for (const action of preview.actions) {
const targetStr = action.target ? ` → ${action.target}` : ''
out(` - [${action.type}] ${action.description}${targetStr}`)
if (action.details) {
for (const [key, value] of Object.entries(action.details)) {
out(` ${key}: ${JSON.stringify(value)}`)
}
}
}
}
out('')
if (preview.wouldSucceed !== undefined) {
out(
preview.wouldSucceed
? ' Would complete successfully.'
: ' Would fail (see details above).',
)
}
out('')
out(' Run without --dry-run to execute these actions.')
out('')
}
/**
* Output a simple dry-run message for commands that just fetch/display data.
* These commands don't really need dry-run since they're read-only,
* but showing computed query parameters helps users verify their input.
*/
export function outputDryRunFetch(
resourceName: string,
queryParams?: Record<string, string | number | boolean | undefined>,
): void {
out('')
out(`${DRY_RUN_LABEL}: Would fetch ${resourceName}`)
out('')
if (queryParams && Object.keys(queryParams).length > 0) {
out(' Query parameters:')
for (const [key, value] of Object.entries(queryParams)) {
if (value !== undefined && value !== '') {
out(` ${key}: ${value}`)
}
}
out('')
}
out(' This is a read-only operation that does not modify any data.')
out(' Run without --dry-run to fetch and display the data.')
out('')
}
/**
* Output dry-run for commands that execute external tools.
*/
export function outputDryRunExecute(
command: string,
args: string[],
description?: string,
): void {
out('')
out(
`${DRY_RUN_LABEL}: Would execute ${description || 'external command'}`,
)
out('')
out(` Command: ${command}`)
if (args.length > 0) {
out(` Arguments: ${args.join(' ')}`)
}
out('')
out(' Run without --dry-run to execute this command.')
out('')
}
/**
* Output dry-run for file write operations.
*/
export function outputDryRunWrite(
filePath: string,
description: string,
changes?: string[],
): void {
out('')
out(`${DRY_RUN_LABEL}: Would ${description}`)
out('')
out(` Target file: ${filePath}`)
if (changes && changes.length > 0) {
out(' Changes:')
for (const change of changes) {
out(` - ${change}`)
}
}
out('')
out(' Run without --dry-run to apply these changes.')
out('')
}
/**
* Output dry-run for API upload operations.
*/
export function outputDryRunUpload(
resourceType: string,
details: Record<string, unknown>,
): void {
out('')
out(`${DRY_RUN_LABEL}: Would upload ${resourceType}`)
out('')
out(' Details:')
for (const [key, value] of Object.entries(details)) {
if (typeof value === 'object' && value !== null) {
out(` ${key}:`)
for (const [subKey, subValue] of Object.entries(
value as Record<string, unknown>,
)) {
out(` ${subKey}: ${JSON.stringify(subValue)}`)
}
} else {
out(` ${key}: ${JSON.stringify(value)}`)
}
}
out('')
out(' Run without --dry-run to perform this upload.')
out('')
}
/**
* Output dry-run for delete operations.
*/
export function outputDryRunDelete(
resourceType: string,
identifier: string,
): void {
out('')
out(`${DRY_RUN_LABEL}: Would delete ${resourceType}`)
out('')
out(` Target: ${identifier}`)
out('')
out(' This action cannot be undone.')
out(' Run without --dry-run to perform this deletion.')
out('')
}