1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use std::env::VarError;

use anyhow::bail;
use libdav::auth::{Auth, Password};

pub fn auth_from_env() -> anyhow::Result<Auth> {
    match std::env::var("DAVCLI_USERNAME") {
        Ok(username) => {
            let password = match std::env::var("DAVCLI_PASSWORD").map(Password::from) {
                Ok(password) => Some(password),
                Err(VarError::NotPresent) => None,
                Err(VarError::NotUnicode(_)) => {
                    bail!("password must be valid unicode");
                }
            };
            Ok(Auth::Basic { username, password })
        }
        Err(VarError::NotPresent) => Ok(Auth::None),
        Err(VarError::NotUnicode(_)) => {
            bail!("username must be valid unicode");
        }
    }
}