-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconfig.ts
More file actions
142 lines (122 loc) · 3.81 KB
/
Copy pathconfig.ts
File metadata and controls
142 lines (122 loc) · 3.81 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
/**
* Config Command
* Handles configuration management
*/
import { ConfigService } from '../../storage/config.js';
const BOOLEAN_KEYS = [
'cache.enabled',
'output.verbose',
'output.color',
'watch.autoCommit',
'defaults.preserveFormatting',
];
const NUMERIC_KEYS = [
'cache.maxSize',
'cache.ttl',
'watch.debounceMs',
];
export class ConfigCommand {
private config: ConfigService;
constructor(config: ConfigService) {
this.config = config;
}
/**
* Get config value
*/
async get(key?: string): Promise<unknown> {
if (key) {
const value = this.config.getValue(key);
if (key === 'auth.apiKey' && typeof value === 'string' && value.length > 8) {
return value.substring(0, 4) + '...' + value.substring(value.length - 4);
}
return value;
}
return this.maskSensitiveValues(this.config.get() as unknown as Record<string, unknown>);
}
/**
* Set config value
*/
async set(key: string, value: string): Promise<void> {
// Parse value based on type
const parsedValue = this.parseValue(key, value);
this.config.set(key, parsedValue);
}
/**
* List all config values
*/
async list(): Promise<Record<string, unknown>> {
const config = this.config.get();
// Mask sensitive values
return this.maskSensitiveValues(config as unknown as Record<string, unknown>);
}
/**
* Reset config to defaults
*/
async reset(): Promise<void> {
this.config.clear();
}
/**
* Format a single config value for human-readable text output
*/
formatValue(key: string | undefined, value: unknown): string {
if (key) {
const displayValue = value === undefined ? '(not set)' : String(value);
return `${key} = ${displayValue}`;
}
return this.formatConfig(value as Record<string, unknown>);
}
/**
* Format full config as human-readable text output
*/
formatConfig(config: Record<string, unknown>): string {
const lines: string[] = [];
this.flattenConfig(config, '', lines);
return lines.join('\n');
}
private flattenConfig(obj: Record<string, unknown>, prefix: string, lines: string[]): void {
for (const [key, value] of Object.entries(obj)) {
const fullKey = prefix ? `${prefix}.${key}` : key;
if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
this.flattenConfig(value as Record<string, unknown>, fullKey, lines);
} else {
const display = value === undefined ? '(not set)' : JSON.stringify(value);
lines.push(`${fullKey} = ${display}`);
}
}
}
/**
* Parse value based on key
*/
private parseValue(key: string, value: string): unknown {
// Handle array values (comma-separated)
if (key.includes('targetLangs') || value.includes(',')) {
return value.split(',').map(v => v.trim());
}
// Auto-coerce string values to booleans for known boolean config keys
if (BOOLEAN_KEYS.includes(key)) {
if (value === 'true') return true;
if (value === 'false') return false;
}
// Auto-coerce string values to numbers for known numeric config keys
if (NUMERIC_KEYS.includes(key)) {
const num = parseInt(value, 10);
if (!isNaN(num)) return num;
}
return value;
}
/**
* Mask sensitive values like API keys
*/
private maskSensitiveValues(config: Record<string, unknown>): Record<string, unknown> {
const masked = JSON.parse(JSON.stringify(config)) as Record<string, unknown>;
// Mask API key
if (masked['auth'] && typeof masked['auth'] === 'object') {
const auth = masked['auth'] as Record<string, unknown>;
if (auth['apiKey'] && typeof auth['apiKey'] === 'string') {
const apiKey = auth['apiKey'];
auth['apiKey'] = apiKey.substring(0, 4) + '...' + apiKey.substring(apiKey.length - 4);
}
}
return masked;
}
}