-
Notifications
You must be signed in to change notification settings - Fork 455
Expand file tree
/
Copy pathreset.ts
More file actions
105 lines (93 loc) · 3.55 KB
/
Copy pathreset.ts
File metadata and controls
105 lines (93 loc) · 3.55 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
import {Args, Command, Flags} from '@oclif/core'
import {
SettingsEvents,
type SettingsGetRequest,
type SettingsGetResponse,
type SettingsItemDTO,
type SettingsResetRequest,
type SettingsResetResponse,
} from '../../../shared/transport/events/settings-events.js'
import {formatCount, formatDuration} from '../../../shared/utils/format-duration.js'
import {type DaemonClientOptions, formatConnectionError, withDaemonRetry} from '../../lib/daemon-client.js'
import {writeJsonResponse} from '../../lib/json-response.js'
export default class SettingsReset extends Command {
public static args = {
key: Args.string({description: 'Settings key to reset', required: true}),
}
public static description =
'Restore one settings value to its default. Changes apply after `brv restart`.'
public static examples = [
'<%= config.bin %> settings reset agentPool.maxSize',
'<%= config.bin %> settings reset agentPool.maxSize --format json',
]
public static flags = {
format: Flags.string({
default: 'text',
description: 'Output format (text or json)',
options: ['text', 'json'],
}),
}
protected async fetchDescriptor(key: string, options?: DaemonClientOptions): Promise<SettingsGetResponse> {
return withDaemonRetry<SettingsGetResponse>(
async (client) =>
client.requestWithAck<SettingsGetResponse>(SettingsEvents.GET, {key} satisfies SettingsGetRequest),
options,
)
}
protected async resetSetting(key: string, options?: DaemonClientOptions): Promise<SettingsResetResponse> {
return withDaemonRetry<SettingsResetResponse>(
async (client) =>
client.requestWithAck<SettingsResetResponse>(SettingsEvents.RESET, {key} satisfies SettingsResetRequest),
options,
)
}
public async run(): Promise<void> {
const {args, flags} = await this.parse(SettingsReset)
const format = flags.format as 'json' | 'text'
try {
const descriptor = await this.fetchDescriptor(args.key)
if (!descriptor.ok) {
process.exitCode = 1
if (format === 'json') {
writeJsonResponse({command: 'settings reset', data: {error: descriptor.error}, success: false})
} else {
this.log(descriptor.error.message)
}
return
}
const response = await this.resetSetting(args.key)
if (response.ok) {
if (format === 'json') {
writeJsonResponse({
command: 'settings reset',
data: {restartRequired: response.restartRequired},
success: true,
})
} else {
const base = `Setting reset: ${args.key} back to default (${renderValue(descriptor, descriptor.default)}).`
this.log(descriptor.restartRequired ? `${base} Run \`brv restart\` to apply.` : base)
}
return
}
process.exitCode = 1
if (format === 'json') {
writeJsonResponse({command: 'settings reset', data: {error: response.error}, success: false})
} else {
this.log(response.error.message)
}
} catch (error) {
process.exitCode = 1
if (format === 'json') {
writeJsonResponse({command: 'settings reset', data: {error: formatConnectionError(error)}, success: false})
} else {
this.log(formatConnectionError(error))
}
}
}
}
function renderValue(item: SettingsItemDTO, value: boolean | number | string): string {
if (typeof value === 'boolean') return value ? 'true' : 'false'
if (typeof value === 'string') return value
if (item.unit === 'ms') return formatDuration(value)
return formatCount(value)
}