feat(ui): add type filter toggles
This commit is contained in:
64
server/config.ts
Normal file
64
server/config.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { HostConfig } from './types';
|
||||
|
||||
const CONFIG_FILE = path.join(__dirname, 'config.json');
|
||||
|
||||
function parseEnvHosts(): HostConfig[] {
|
||||
const hostsEnv = process.env.SSH_HOSTS;
|
||||
if (!hostsEnv) return [];
|
||||
|
||||
const hosts: HostConfig[] = [];
|
||||
const entries = hostsEnv.split(',').map(h => h.trim()).filter(Boolean);
|
||||
|
||||
for (const entry of entries) {
|
||||
const [name, ip] = entry.split(':');
|
||||
if (name && ip) {
|
||||
hosts.push({
|
||||
name: name.trim(),
|
||||
ip: ip.trim(),
|
||||
sshUser: process.env.SSH_USER || 'bear',
|
||||
sshKeyPath: process.env.SSH_KEY,
|
||||
sshPort: process.env.SSH_PORT ? parseInt(process.env.SSH_PORT, 10) : 22,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return hosts;
|
||||
}
|
||||
|
||||
function parseJsonConfig(): HostConfig[] {
|
||||
if (!fs.existsSync(CONFIG_FILE)) return [];
|
||||
|
||||
try {
|
||||
const content = fs.readFileSync(CONFIG_FILE, 'utf-8');
|
||||
const data = JSON.parse(content);
|
||||
|
||||
if (!data.hosts || !Array.isArray(data.hosts)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return data.hosts.map((h: Partial<HostConfig>) => ({
|
||||
name: h.name || '',
|
||||
ip: h.ip || '',
|
||||
sshUser: h.sshUser || 'bear',
|
||||
sshKeyPath: h.sshKeyPath,
|
||||
sshPort: h.sshPort || 22,
|
||||
})).filter((h: HostConfig) => h.name && h.ip);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export function getHostConfigs(): HostConfig[] {
|
||||
const envHosts = parseEnvHosts();
|
||||
if (envHosts.length > 0) {
|
||||
return envHosts;
|
||||
}
|
||||
|
||||
return parseJsonConfig();
|
||||
}
|
||||
|
||||
export function hasConfig(): boolean {
|
||||
return getHostConfigs().length > 0;
|
||||
}
|
||||
Reference in New Issue
Block a user