5454
5555local function generate_common_run_command_args (data )
5656 local run_args = nil
57- -- TODO: Add support for remoteEnv?
5857 if data .forwardPorts then
5958 run_args = run_args or {}
6059 for _ , v in ipairs (data .forwardPorts ) do
6766
6867local function generate_run_command_args (data , attaching )
6968 local run_args = generate_common_run_command_args (data )
69+ -- TODO: Add support for containerEnv!
7070 if data .containerUser then
7171 run_args = run_args or {}
7272 table.insert (run_args , " --user" )
7373 table.insert (run_args , data .containerUser )
7474 end
7575 if data .workspaceFolder or data .workspaceMount then
76- if data .workspaceMount == nil or data .workspaceFolder == nil then
77- vim .notify (" workspaceFolder and workspaceMount have to both be defined to be used!" , vim .log .levels .WARN )
78- else
79- run_args = run_args or {}
80- table.insert (run_args , " --mount" )
81- table.insert (run_args , data .workspaceMount )
82- end
76+ -- if data.workspaceMount == nil or data.workspaceFolder == nil then
77+ -- vim.notify("workspaceFolder and workspaceMount have to both be defined to be used!", vim.log.levels.WARN)
78+ -- else
79+ run_args = run_args or {}
80+ table.insert (run_args , " --mount" )
81+ table.insert (run_args , data .workspaceMount )
82+ -- end
8383 end
8484 if data .mounts then
8585 run_args = run_args or {}
@@ -95,7 +95,7 @@ local function generate_run_command_args(data, attaching)
9595 table.insert (run_args , v )
9696 end
9797 end
98- if attaching and plugin_config .attach_mounts then
98+ if plugin_config . attach_mounts and ( attaching or plugin_config .attach_mounts . always ) then
9999 run_args = run_args or {}
100100 local am = plugin_config .attach_mounts
101101
@@ -152,6 +152,21 @@ local function generate_run_command_args(data, attaching)
152152 return run_args
153153end
154154
155+ local function generate_exec_command_args (data )
156+ local exec_args = nil
157+ -- remoteEnv currently unsupported
158+ if data .workspaceFolder or data .workspaceMount then
159+ -- if data.workspaceMount == nil or data.workspaceFolder == nil then
160+ -- vim.notify("workspaceFolder and workspaceMount have to both be defined to be used!", vim.log.levels.WARN)
161+ -- else
162+ exec_args = exec_args or {}
163+ table.insert (exec_args , " --workdir" )
164+ table.insert (exec_args , data .workspaceFolder )
165+ -- end
166+ end
167+ return exec_args
168+ end
169+
155170local function generate_compose_up_command_args (data )
156171 local run_args = nil
157172 if data .runServices then
@@ -339,23 +354,55 @@ function M.docker_image_run(callback)
339354 end )
340355end
341356
342- local function spawn_docker_build_and_run (data , on_success , add_neovim )
357+ local function attach_to_container (data , container_id , on_success )
358+ docker .exec (container_id , {
359+ tty = true ,
360+ command = " nvim" ,
361+ args = generate_exec_command_args (data ),
362+ on_success = on_success ,
363+ on_fail = function ()
364+ vim .notify (" Attaching to container (" .. container_id .. " ) failed!" , vim .log .levels .ERROR )
365+ end ,
366+ })
367+ end
368+
369+ local function attach_to_compose_service (data , on_success )
370+ if not data .service then
371+ vim .notify (
372+ " service must be defined in " .. data .metadata .file_path .. " to attach to docker compose" ,
373+ vim .log .levels .ERROR
374+ )
375+ return
376+ end
377+ vim .notify (" Found docker compose file definition. Attaching to service: " .. data .service )
378+ docker_compose .get_container_id (data .dockerComposeFile , data .service , {
379+ on_success = function (container_id )
380+ attach_to_container (data , container_id , function ()
381+ on_success (data )
382+ end )
383+ end ,
384+ })
385+ end
386+
387+ local function spawn_docker_build_and_run (data , on_success , add_neovim , attach )
343388 docker .build (data .build .dockerfile , data .build .context , {
344389 args = generate_build_command_args (data ),
345390 add_neovim = add_neovim ,
346391 on_success = function (image_id )
347392 docker .run (image_id , {
348393 args = generate_run_command_args (data , add_neovim ),
349- tty = add_neovim ,
350- -- TODO: Potentially add in the future for better compatibility
351- -- or (data.overrideCommand and {
352- -- "/bin/sh",
353- -- "-c",
354- -- "'while sleep 1000; do :; done'",
355- -- })
356- command = (add_neovim and " nvim" ) or nil ,
394+ -- -- TODO: Potentially add in the future for better compatibility
395+ -- -- or (data.overrideCommand and {
396+ -- -- "/bin/sh",
397+ -- -- "-c",
398+ -- -- "'while sleep 1000; do :; done'",
399+ -- -- })
357400 on_success = function (container_id )
358- on_success (data , image_id , container_id )
401+ if attach then
402+ attach_to_container (data , container_id , function ()
403+ on_success (data , image_id , container_id )
404+ end )
405+ end
359406 end ,
360407 on_fail = function ()
361408 vim .notify (" Running built image (" .. image_id .. " ) failed!" , vim .log .levels .ERROR )
@@ -389,7 +436,7 @@ local function execute_docker_build_and_run(callback, add_neovim)
389436 )
390437 return
391438 end
392- spawn_docker_build_and_run (data , on_success , add_neovim )
439+ spawn_docker_build_and_run (data , on_success , add_neovim , add_neovim )
393440 end )
394441end
395442
421468--- Then it looks for dockerfile
422469--- And last it looks for image
423470--- @param callback function | nil called on success - devcontainer config is passed to the callback
471+ --- @param attach boolean | nil if true , automatically attach after starting
424472--- @usage `require("devcontainer.commands").start_auto()`
425- function M .start_auto (callback )
473+ function M .start_auto (callback , attach )
426474 vim .validate ({
427475 callback = { callback , { " function" , " nil" } },
428476 })
@@ -438,7 +486,11 @@ function M.start_auto(callback)
438486 docker_compose .up (data .dockerComposeFile , {
439487 args = generate_compose_up_command_args (data ),
440488 on_success = function ()
441- on_success (data )
489+ if attach then
490+ attach_to_compose_service (data , on_success )
491+ else
492+ on_success (data )
493+ end
442494 end ,
443495 on_fail = function ()
444496 vim .notify (" Docker compose up failed!" , vim .log .levels .ERROR )
@@ -449,14 +501,14 @@ function M.start_auto(callback)
449501
450502 if data .build .dockerfile then
451503 vim .notify (" Found dockerfile definition. Running docker build and run..." )
452- spawn_docker_build_and_run (data , on_success , false )
504+ spawn_docker_build_and_run (data , on_success , attach , attach )
453505 return
454506 end
455507
456508 if data .image then
457509 vim .notify (" Found image definition. Running docker run..." )
458510 docker .run (data .image , {
459- args = generate_run_command_args (data , false ),
511+ args = generate_run_command_args (data , attach ),
460512 on_success = function (_ )
461513 on_success (data )
462514 end ,
@@ -469,6 +521,48 @@ function M.start_auto(callback)
469521 end )
470522end
471523
524+ --- Parses devcontainer.json and attaches to whatever is defined there
525+ --- Looks for dockerComposeFile first
526+ --- Then it looks for dockerfile
527+ --- And last it looks for image
528+ --- @param callback function | nil called on success - devcontainer config is passed to the callback
529+ --- @usage `require("devcontainer.commands").attach_auto()`
530+ function M .attach_auto (callback )
531+ vim .validate ({
532+ callback = { callback , { " function" , " nil" } },
533+ })
534+
535+ local on_success = callback
536+ or function (config )
537+ vim .notify (" Successfully attached to container from " .. config .metadata .file_path )
538+ end
539+
540+ get_nearest_devcontainer_config (function (data )
541+ if data .dockerComposeFile then
542+ attach_to_compose_service (data , on_success )
543+ return
544+ end
545+
546+ if data .build .dockerfile then
547+ vim .notify (" Found dockerfile definition. Attaching to the container..." )
548+ local container = status .find_container ({ source_dockerfile = data .build .dockerfile })
549+ attach_to_container (data , container .container_id , function ()
550+ on_success (data )
551+ end )
552+ return
553+ end
554+
555+ if data .image then
556+ vim .notify (" Found image definition. Attaaching to the container..." )
557+ local container = status .find_container ({ source_dockerfile = data .build .dockerfile })
558+ attach_to_container (data , container .container_id , function ()
559+ on_success (data )
560+ end )
561+ return
562+ end
563+ end )
564+ end
565+
472566--- Parses devcontainer.json and stops whatever is defined there
473567--- Looks for dockerComposeFile first
474568--- Then it looks for dockerfile
0 commit comments