allan2 / dotenvy

A well-maintained fork of the Rust dotenv crate
MIT License
625 stars 39 forks source link

Failing to read env field #91

Closed djkato closed 5 months ago

djkato commented 5 months ago

env file: .env

REQUIRED_SALEOR_VERSION=">=3.11.7<4"
SALEOR_APP_ID="dummy-saleor-app-rs"
APP_API_BASE_URL="http://localhost:8000/graphql/"
APL="redis"
APL_URL="redis://redis:6379/2"
LOG_LEVEL="DEBUG"

running: config.rs


use crate::saleor::AplType;
use tracing::Level;

#[derive(Debug, Deserialize)]
#[serde(remote = "Level")]
pub enum LocalTracingLevel {
    TRACE,
    DEBUG,
    INFO,
    WARN,
    ERROR,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub struct Config {
    pub required_saleor_version: String,
    pub saleor_app_id: String,
    pub app_api_base_url: String,
    pub apl: AplType,
    pub apl_url: String,
    #[serde(with = "LocalTracingLevel")]
    pub log_level: tracing::Level,
}

impl Config {
    pub fn load() -> Result<Self, envy::Error> {
        dotenvy::dotenv().unwrap();

        for (key, value) in std::env::vars() {
            println!("{key}: {value}");
        }
        let env = envy::from_env::<Config>();
        dbg!(&env);
        env
    }
}

Error/stdout:

REQUIRED_SALEOR_VERSION: >=3.11.7<4
SALEOR_APP_ID: dummy-saleor-app-rs
APP_API_BASE_URL: http://localhost:8000/graphql/
APL: redis
APL_URL: redis://redis:6379/2
LOG_LEVEL: DEBUG
[src/config.rs:36:9] &env = Err(
    MissingValue(
        "REQUIRED_SALEOR_VERSION",
    ),
)
Error: missing value for field REQUIRED_SALEOR_VERSION
allan2 commented 5 months ago
let env = envy::from_env::<Config>();

This repo is for dotenvy, not envy. It looks like the env vars were loaded correctly.

djkato commented 5 months ago

Oh yeah ups, sorry I posted into the wrong repo. This lib works just fine :)