defmodule Argos.CLI.Config do @moduledoc """ Módulo para manejar la configuración del CLI (shell, variables de entorno, etc.). """ @doc """ Determina si se debe cargar el archivo de configuración del shell. """ @spec should_load_config(keyword()) :: boolean() def should_load_config(opts) do no_config = Keyword.get(opts, :no_config, false) || Keyword.get(opts, :no_login, false) load_config = Keyword.get(opts, :load_config, false) if load_config, do: true, else: not no_config end @doc """ Obtiene el shell configurado o el predeterminado. """ @spec get_shell() :: String.t() def get_shell do Application.get_env(:argos, :shell, "/bin/zsh") end @doc """ Obtiene el ejecutable del shell o el predeterminado si no se encuentra. """ @spec get_shell_executable() :: String.t() def get_shell_executable do shell = get_shell() System.find_executable(shell) || shell end @doc """ Construye las variables de entorno con asdf añadido al PATH. """ @spec build_env_vars_with_asdf() :: [{charlist(), charlist()}] def build_env_vars_with_asdf do home = System.get_env("HOME") || "~" asdf_shims = Path.join(home, ".asdf/shims") asdf_bin = Path.join(home, ".asdf/bin") current_path = System.get_env("PATH") || "" updated_path = add_asdf_to_path(current_path, asdf_shims, asdf_bin) System.get_env() |> Map.put("PATH", updated_path) |> Enum.map(fn {k, v} -> {String.to_charlist(k), String.to_charlist(v)} end) end @doc """ Obtiene el archivo de configuración del shell según el shell usado. """ @spec get_shell_config_file(String.t()) :: String.t() def get_shell_config_file(shell) do home = System.get_env("HOME") || "~" shell_str = to_string(shell) get_default_config_for_shell(shell_str, home) end defp add_asdf_to_path(current_path, asdf_shims, asdf_bin) do if String.contains?(current_path, asdf_shims) do current_path else "#{asdf_shims}:#{asdf_bin}:#{current_path}" end end defp get_default_config_for_shell(shell_str, home) do cond do String.contains?(shell_str, "zsh") -> Path.join(home, ".zshrc") String.contains?(shell_str, "bash") -> bashrc = Path.join(home, ".bashrc") if File.exists?(bashrc), do: bashrc, else: Path.join(home, ".bash_profile") String.contains?(shell_str, "fish") -> Path.join(home, ".config/fish/config.fish") true -> Path.join(home, ".zshrc") end end end