Get user and pass from environment during login

This commit is contained in:
Maxime Augier 2024-08-26 15:13:09 +02:00
parent 709e9f5d64
commit 9e39546608
2 changed files with 13 additions and 12 deletions

View File

@ -40,7 +40,7 @@ impl Charger {
pub fn start( pub fn start(
mut ctx: Context, mut ctx: Context,
config: Config, config: Config,
mut chargers: Vec<api::Charger>, chargers: Vec<api::Charger>,
) -> Result<Infallible> { ) -> Result<Infallible> {
let mattermost = mattermost::Context::new(config.mattermost.base, &config.mattermost.token)?; let mattermost = mattermost::Context::new(config.mattermost.base, &config.mattermost.token)?;
let mut stream = observation::Stream::from_context(&mut ctx)?; let mut stream = observation::Stream::from_context(&mut ctx)?;
@ -125,7 +125,7 @@ fn handle_event(
ctx: &mattermost::Context, ctx: &mattermost::Context,
channel: &Channel, channel: &Channel,
) -> Result<()> { ) -> Result<()> {
let send = |msg: &str| ctx.send_to_channel(channel, msg); let send = |msg: &str| ctx.send_to_channel(channel, &format!("{}: {msg}", &charger.inner.id));
match evt.observation { match evt.observation {
Observation::PilotMode(mode) => match mode { Observation::PilotMode(mode) => match mode {

View File

@ -78,20 +78,21 @@ struct CLI {
const SAVED_TOKEN_PATH: &str = ".easee_token"; const SAVED_TOKEN_PATH: &str = ".easee_token";
fn login() -> Result<()> { fn login() -> Result<()> {
use std::io::Write;
let stdin = std::io::stdin(); let stdin = std::io::stdin();
let mut stderr = std::io::stderr(); let mut stderr = std::io::stderr();
let mut username = String::new(); let mut prompt = |key: &str, pr: &str| -> Result<String > {
let mut password = String::new(); use std::io::Write;
if let Ok(var) = std::env::var(key) { return Ok(var) }
let mut buf = String::with_capacity(64);
write!(stderr, "{pr}: ")?;
stdin.read_line(&mut buf)?;
buf.truncate(buf.trim().len());
Ok(buf)
};
write!(stderr, "Username: ")?; let username = prompt("EASEE_USERNAME", "Username")?;
stdin.read_line(&mut username)?; let password = prompt("EASEE_PASSWORD", "Password")?;
write!(stderr, "Password: ")?;
stdin.read_line(&mut password)?;
let username = username.trim();
let password = password.trim();
let mut ctx = easee::api::Context::from_login(&username, &password)?; let mut ctx = easee::api::Context::from_login(&username, &password)?;
info!("Login successful."); info!("Login successful.");