module Kamal::Commands::App::Execution
  def execute_in_existing_container(*command, interactive: false, env:)
    docker :exec,
      (docker_interactive_args if interactive),
      *argumentize("--env", env),
      container_name,
      *command
  end

  def execute_in_new_container(*command, interactive: false, detach: false, env:)
    docker :run,
      (docker_interactive_args if interactive),
      ("--detach" if detach),
      ("--rm" unless detach),
      "--name", container_name_for_exec,
      "--network", "kamal",
      *role&.env_args(host),
      *argumentize("--env", env),
      *role.logging_args,
      *config.volume_args,
      *role&.option_args,
      config.absolute_image,
      *command
  end

  def execute_in_existing_container_over_ssh(*command, env:)
    run_over_ssh execute_in_existing_container(*command, interactive: true, env: env), host: host
  end

  def execute_in_new_container_over_ssh(*command, env:)
    run_over_ssh execute_in_new_container(*command, interactive: true, env: env), host: host
  end

  private
    def container_name_for_exec
      [ role.container_prefix, "exec", config.version, SecureRandom.hex(3) ].compact.join("-")
    end
end
