|
| 1 | +import { Command } from 'commander'; |
| 2 | +import { getAdtClientV2 } from '../utils/adt-client-v2'; |
| 3 | + |
| 4 | +/** Extract entries from atomFeed parsed response */ |
| 5 | +function extractEntries( |
| 6 | + data: unknown, |
| 7 | +): { id?: string; title?: string; link?: { href: string }[] }[] { |
| 8 | + const feed = data as Record<string, unknown>; |
| 9 | + const feedData = feed.feed as Record<string, unknown> | undefined; |
| 10 | + if (!feedData) return []; |
| 11 | + |
| 12 | + const rawEntries = feedData.entry; |
| 13 | + if (!rawEntries) return []; |
| 14 | + return Array.isArray(rawEntries) ? rawEntries : [rawEntries]; |
| 15 | +} |
| 16 | + |
| 17 | +export const userCommand = new Command('user') |
| 18 | + .description('Look up SAP system users') |
| 19 | + .argument('[query]', 'Username or search query (supports wildcards like *)') |
| 20 | + .option('-m, --max <number>', 'Maximum number of results', '50') |
| 21 | + .option('--json', 'Output results as JSON') |
| 22 | + .action(async (query: string | undefined, options) => { |
| 23 | + try { |
| 24 | + const adtClient = await getAdtClientV2(); |
| 25 | + |
| 26 | + if (!query) { |
| 27 | + // No query: show current user via systeminformation |
| 28 | + console.log('🔄 Fetching current user...\n'); |
| 29 | + const sysInfo = |
| 30 | + (await adtClient.adt.core.http.systeminformation.getSystemInfo()) as Record< |
| 31 | + string, |
| 32 | + unknown |
| 33 | + >; |
| 34 | + |
| 35 | + if (options.json) { |
| 36 | + console.log( |
| 37 | + JSON.stringify( |
| 38 | + { |
| 39 | + userName: sysInfo.userName, |
| 40 | + userFullName: sysInfo.userFullName, |
| 41 | + systemID: sysInfo.systemID, |
| 42 | + client: sysInfo.client, |
| 43 | + }, |
| 44 | + null, |
| 45 | + 2, |
| 46 | + ), |
| 47 | + ); |
| 48 | + } else { |
| 49 | + console.log(`👤 Current User: ${sysInfo.userName}`); |
| 50 | + if (sysInfo.userFullName) { |
| 51 | + console.log(` Full Name: ${sysInfo.userFullName}`); |
| 52 | + } |
| 53 | + console.log( |
| 54 | + ` System: ${sysInfo.systemID} (client ${sysInfo.client})`, |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + console.log('\n✅ Done!'); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + // Check if query looks like an exact username (no wildcards) |
| 63 | + const isExactLookup = !query.includes('*') && !query.includes('?'); |
| 64 | + |
| 65 | + if (isExactLookup) { |
| 66 | + // Get specific user |
| 67 | + console.log(`🔍 Looking up user: ${query.toUpperCase()}...\n`); |
| 68 | + const result = await adtClient.adt.system.users.get( |
| 69 | + query.toUpperCase(), |
| 70 | + ); |
| 71 | + |
| 72 | + const entries = extractEntries(result); |
| 73 | + if (entries.length === 0) { |
| 74 | + console.log('No user found.'); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + if (options.json) { |
| 79 | + console.log( |
| 80 | + JSON.stringify( |
| 81 | + entries.map((e) => ({ username: e.id, fullName: e.title })), |
| 82 | + null, |
| 83 | + 2, |
| 84 | + ), |
| 85 | + ); |
| 86 | + } else { |
| 87 | + for (const entry of entries) { |
| 88 | + console.log(`👤 ${entry.id}`); |
| 89 | + if (entry.title) console.log(` Full Name: ${entry.title}`); |
| 90 | + if (entry.link?.[0]?.href) |
| 91 | + console.log(` URI: ${entry.link[0].href}`); |
| 92 | + } |
| 93 | + } |
| 94 | + } else { |
| 95 | + // Search users |
| 96 | + const maxcount = parseInt(options.max, 10); |
| 97 | + console.log(`🔍 Searching users: "${query}" (max: ${maxcount})...\n`); |
| 98 | + const result = await adtClient.adt.system.users.search({ |
| 99 | + querystring: query, |
| 100 | + maxcount, |
| 101 | + }); |
| 102 | + |
| 103 | + const entries = extractEntries(result); |
| 104 | + if (entries.length === 0) { |
| 105 | + console.log('No users found matching your query.'); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + if (options.json) { |
| 110 | + console.log( |
| 111 | + JSON.stringify( |
| 112 | + entries.map((e) => ({ username: e.id, fullName: e.title })), |
| 113 | + null, |
| 114 | + 2, |
| 115 | + ), |
| 116 | + ); |
| 117 | + } else { |
| 118 | + console.log(`Found ${entries.length} user(s):\n`); |
| 119 | + for (const entry of entries) { |
| 120 | + console.log(` ${entry.id?.padEnd(12)} ${entry.title || ''}`); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + console.log('\n✅ Done!'); |
| 126 | + } catch (error) { |
| 127 | + console.error( |
| 128 | + '❌ User lookup failed:', |
| 129 | + error instanceof Error ? error.message : String(error), |
| 130 | + ); |
| 131 | + if (error instanceof Error && error.stack) { |
| 132 | + console.error('\nStack trace:', error.stack); |
| 133 | + } |
| 134 | + process.exit(1); |
| 135 | + } |
| 136 | + }); |
0 commit comments