Skip to content

utils

get_api_key()

Get API key from environment or prompt user.

Source code in src/embar/tools/utils.py
42
43
44
45
46
47
48
49
50
51
def get_api_key() -> str:
    """Get API key from environment or prompt user."""
    api_key = os.environ.get("ANTHROPIC_API_KEY")
    if not api_key:
        print("ANTHROPIC_API_KEY environment variable not set.")
        api_key = input("Please enter your Anthropic API key: ").strip()
        if not api_key:
            print("Error: API key is required.")
            sys.exit(1)
    return api_key

load_config(config_path=None)

Load and validate configuration from file.

Source code in src/embar/tools/utils.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def load_config(config_path: str | None = None) -> MigrateConfig:
    """Load and validate configuration from file."""
    if config_path is None:
        config_path = "embar.yml"

    if not os.path.exists(config_path):
        print(f"Error: Config file '{config_path}' not found.")
        sys.exit(1)

    with open(config_path, "r") as f:
        config_data = yaml.safe_load(f)

    try:
        return MigrateConfig(**config_data)
    except TypeError as e:
        print(f"Error: Invalid config format: {e}")
        sys.exit(1)

load_env_file()

Load environment variables from .env file if it exists.

Source code in src/embar/tools/utils.py
 9
10
11
12
13
14
15
16
17
18
19
20
def load_env_file():
    """Load environment variables from .env file if it exists."""
    # Try current directory first, then parent directory
    for env_path in [".env", "../.env"]:
        if os.path.exists(env_path):
            with open(env_path, "r") as f:
                for line in f:
                    line = line.strip()
                    if line and not line.startswith("#") and "=" in line:
                        key, value = line.split("=", 1)
                        os.environ[key.strip()] = value.strip()
            break