# This file generated by `dagger_codegen`. Please DO NOT EDIT.
defmodule Dagger.Host do
  @moduledoc """
  Information about the host environment.
  """

  use Dagger.Core.Base, kind: :object, name: "Host"

  alias Dagger.Core.Client
  alias Dagger.Core.QueryBuilder, as: QB

  @derive Dagger.ID

  defstruct [:query_builder, :client]

  @type t() :: %__MODULE__{}

  @doc """
  Accesses a container image on the host.
  """
  @spec container_image(t(), String.t()) :: Dagger.Container.t()
  def container_image(%__MODULE__{} = host, name) do
    query_builder =
      host.query_builder |> QB.select("containerImage") |> QB.put_arg("name", name)

    %Dagger.Container{
      query_builder: query_builder,
      client: host.client
    }
  end

  @doc """
  Accesses a directory on the host.
  """
  @spec directory(t(), String.t(), [
          {:exclude, [String.t()]},
          {:include, [String.t()]},
          {:no_cache, boolean() | nil},
          {:gitignore, boolean() | nil}
        ]) :: Dagger.Directory.t()
  def directory(%__MODULE__{} = host, path, optional_args \\ []) do
    query_builder =
      host.query_builder
      |> QB.select("directory")
      |> QB.put_arg("path", path)
      |> QB.maybe_put_arg("exclude", optional_args[:exclude])
      |> QB.maybe_put_arg("include", optional_args[:include])
      |> QB.maybe_put_arg("noCache", optional_args[:no_cache])
      |> QB.maybe_put_arg("gitignore", optional_args[:gitignore])

    %Dagger.Directory{
      query_builder: query_builder,
      client: host.client
    }
  end

  @doc """
  Accesses a file on the host.
  """
  @spec file(t(), String.t(), [{:no_cache, boolean() | nil}]) :: Dagger.File.t()
  def file(%__MODULE__{} = host, path, optional_args \\ []) do
    query_builder =
      host.query_builder
      |> QB.select("file")
      |> QB.put_arg("path", path)
      |> QB.maybe_put_arg("noCache", optional_args[:no_cache])

    %Dagger.File{
      query_builder: query_builder,
      client: host.client
    }
  end

  @doc """
  Search for a file or directory by walking up the tree from system workdir. Return its relative path. If no match, return null
  """
  @spec find_up(t(), String.t(), [{:no_cache, boolean() | nil}]) ::
          {:ok, String.t() | nil} | {:error, term()}
  def find_up(%__MODULE__{} = host, name, optional_args \\ []) do
    query_builder =
      host.query_builder
      |> QB.select("findUp")
      |> QB.put_arg("name", name)
      |> QB.maybe_put_arg("noCache", optional_args[:no_cache])

    Client.execute(host.client, query_builder)
  end

  @doc """
  A unique identifier for this Host.
  """
  @spec id(t()) :: {:ok, Dagger.HostID.t()} | {:error, term()}
  def id(%__MODULE__{} = host) do
    query_builder =
      host.query_builder |> QB.select("id")

    Client.execute(host.client, query_builder)
  end

  @doc """
  Creates a service that forwards traffic to a specified address via the host.
  """
  @spec service(t(), [Dagger.PortForward.t()], [{:host, String.t() | nil}]) :: Dagger.Service.t()
  def service(%__MODULE__{} = host, ports, optional_args \\ []) do
    query_builder =
      host.query_builder
      |> QB.select("service")
      |> QB.put_arg("ports", ports)
      |> QB.maybe_put_arg("host", optional_args[:host])

    %Dagger.Service{
      query_builder: query_builder,
      client: host.client
    }
  end

  @doc """
  Creates a tunnel that forwards traffic from the host to a service.
  """
  @spec tunnel(t(), Dagger.Service.t(), [
          {:native, boolean() | nil},
          {:ports, [Dagger.PortForward.t()]}
        ]) :: Dagger.Service.t()
  def tunnel(%__MODULE__{} = host, service, optional_args \\ []) do
    query_builder =
      host.query_builder
      |> QB.select("tunnel")
      |> QB.put_arg("service", Dagger.ID.id!(service))
      |> QB.maybe_put_arg("native", optional_args[:native])
      |> QB.maybe_put_arg("ports", optional_args[:ports])

    %Dagger.Service{
      query_builder: query_builder,
      client: host.client
    }
  end

  @doc """
  Accesses a Unix socket on the host.
  """
  @spec unix_socket(t(), String.t()) :: Dagger.Socket.t()
  def unix_socket(%__MODULE__{} = host, path) do
    query_builder =
      host.query_builder |> QB.select("unixSocket") |> QB.put_arg("path", path)

    %Dagger.Socket{
      query_builder: query_builder,
      client: host.client
    }
  end
end

defimpl Jason.Encoder, for: Dagger.Host do
  def encode(host, opts) do
    {:ok, id} = Dagger.Host.id(host)
    Jason.Encode.string(id, opts)
  end
end

defimpl Nestru.Decoder, for: Dagger.Host do
  def decode_fields_hint(_struct, _context, id) do
    {:ok, Dagger.Client.load_host_from_id(Dagger.Global.dag(), id)}
  end
end
