@@ -355,71 +355,53 @@ export const getContainersByAppLabel = async (
355355} ;
356356
357357export const getContainerLogs = async (
358- appName : string ,
358+ appNameOrId : string ,
359359 tail = 100 ,
360360 since = "all" ,
361361 search ?: string ,
362362 serverId ?: string | null ,
363+ useContainerIdDirectly = false ,
363364) : Promise < string > => {
364- // First, find the real container ID by appName filter
365- const findCommand = `docker ps -q --filter "name=^${ appName } " | head -1` ;
366- const findResult = serverId
367- ? await execAsyncRemote ( serverId , findCommand )
368- : await execAsync ( findCommand ) ;
369-
370- const containerId = findResult . stdout . trim ( ) ;
371- if ( ! containerId ) {
372- // Fallback: try as a swarm service
373- const svcCommand = `docker service ls -q --filter "name=${ appName } " | head -1` ;
374- const svcResult = serverId
375- ? await execAsyncRemote ( serverId , svcCommand )
376- : await execAsync ( svcCommand ) ;
377-
378- const serviceId = svcResult . stdout . trim ( ) ;
379- if ( ! serviceId ) {
380- throw new Error ( `No container or service found for: ${ appName } ` ) ;
381- }
365+ const exec = ( cmd : string ) =>
366+ serverId ? execAsyncRemote ( serverId , cmd ) : execAsync ( cmd ) ;
367+
368+ let target = appNameOrId ;
369+ let isService = false ;
370+
371+ if ( ! useContainerIdDirectly ) {
372+ // Find the real container ID by appName filter
373+ const findResult = await exec (
374+ `docker ps -q --filter "name=^${ appNameOrId } " | head -1` ,
375+ ) ;
376+ const containerId = findResult . stdout . trim ( ) ;
382377
383- // Use docker service logs for swarm
384- const sinceFlag = since === "all" ? "" : `--since ${ since } ` ;
385- const baseCommand = `docker service logs --timestamps --raw --tail ${ tail } ${ sinceFlag } ${ appName } ` ;
386- const escapedSearch = search ?. replace ( / ' / g, "'\\''" ) ?? "" ;
387- const command = search
388- ? `${ baseCommand } 2>&1 | grep -iF '${ escapedSearch } '`
389- : `${ baseCommand } 2>&1` ;
390-
391- try {
392- const result = serverId
393- ? await execAsyncRemote ( serverId , command )
394- : await execAsync ( command ) ;
395- return result . stdout ;
396- } catch ( error : unknown ) {
397- if (
398- error &&
399- typeof error === "object" &&
400- "stdout" in error &&
401- typeof ( error as { stdout : string } ) . stdout === "string" &&
402- ( error as { stdout : string } ) . stdout . length > 0
403- ) {
404- return ( error as { stdout : string } ) . stdout ;
378+ if ( ! containerId ) {
379+ // Fallback: try as a swarm service
380+ const svcResult = await exec (
381+ `docker service ls -q --filter "name=${ appNameOrId } " | head -1` ,
382+ ) ;
383+ const serviceId = svcResult . stdout . trim ( ) ;
384+ if ( ! serviceId ) {
385+ throw new Error ( `No container or service found for: ${ appNameOrId } ` ) ;
405386 }
406- throw error ;
387+ isService = true ;
388+ } else {
389+ target = containerId ;
407390 }
408391 }
409392
410393 const sinceFlag = since === "all" ? "" : `--since ${ since } ` ;
411- const baseCommand = `docker container logs --timestamps --tail ${ tail } ${ sinceFlag } ${ containerId } ` ;
394+ const baseCommand = isService
395+ ? `docker service logs --timestamps --raw --tail ${ tail } ${ sinceFlag } ${ target } `
396+ : `docker container logs --timestamps --tail ${ tail } ${ sinceFlag } ${ target } ` ;
412397
413398 const escapedSearch = search ?. replace ( / ' / g, "'\\''" ) ?? "" ;
414399 const command = search
415400 ? `${ baseCommand } 2>&1 | grep -iF '${ escapedSearch } '`
416401 : `${ baseCommand } 2>&1` ;
417402
418403 try {
419- const result = serverId
420- ? await execAsyncRemote ( serverId , command )
421- : await execAsync ( command ) ;
422-
404+ const result = await exec ( command ) ;
423405 return result . stdout ;
424406 } catch ( error : unknown ) {
425407 if (
0 commit comments