use single config file

This commit is contained in:
Antoine Martin 2021-02-02 06:04:06 +01:00
parent e6e41b9ad2
commit ab08f6e0f3
3 changed files with 35 additions and 35 deletions

2
Cargo.lock generated
View file

@ -124,6 +124,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"matrix-sdk", "matrix-sdk",
"serde",
"serde_json", "serde_json",
"tokio", "tokio",
"tracing-subscriber", "tracing-subscriber",
@ -2218,6 +2219,7 @@ dependencies = [
"idna", "idna",
"matches", "matches",
"percent-encoding", "percent-encoding",
"serde",
] ]
[[package]] [[package]]

View file

@ -10,8 +10,9 @@ edition = "2018"
clap = "3.0.0-beta.2" clap = "3.0.0-beta.2"
tokio = { version = "1", features = [ "full" ] } tokio = { version = "1", features = [ "full" ] }
tracing-subscriber = "0.2" tracing-subscriber = "0.2"
url = "2.2" url = { version = "2.2", features = [ "serde" ] }
serde_json = "1" serde_json = "1"
serde = "1.0"
[dependencies.matrix-sdk] [dependencies.matrix-sdk]
git = "https://github.com/matrix-org/matrix-rust-sdk" git = "https://github.com/matrix-org/matrix-rust-sdk"

View file

@ -21,29 +21,7 @@ use matrix_sdk::{
Client, ClientConfig, EventEmitter, RoomState, Session, SyncSettings, Client, ClientConfig, EventEmitter, RoomState, Session, SyncSettings,
}; };
#[derive(Clap)] use serde::Deserialize;
#[clap(version = "0.1", author = "Antoine Martin")]
struct Opts {
/// Username to use for bot account
#[clap(long)]
username: String,
/// Password for bot account
#[clap(long)]
password: String,
/// Homeserver to connect to
#[clap(long)]
homeserver: String,
/// Folder to store the client state into
#[clap(long, parse(from_os_str))]
store_path: PathBuf,
/// File where session information will be saved
#[clap(long, parse(from_os_str))]
session: PathBuf,
}
struct AutoJoinBot { struct AutoJoinBot {
client: Client, client: Client,
@ -152,18 +130,16 @@ async fn load_or_init_session(
} }
async fn login_and_sync( async fn login_and_sync(
homeserver_url: String, homeserver_url: Url,
username: &str, username: &str,
password: &str, password: &str,
store_path: PathBuf, state_dir: PathBuf,
session_file: PathBuf,
) -> Result<(), matrix_sdk::Error> { ) -> Result<(), matrix_sdk::Error> {
let client_config = ClientConfig::new().store_path(store_path); let client_config = ClientConfig::new().store_path(state_dir.join("store"));
let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL");
let client = Client::new_with_config(homeserver_url, client_config).unwrap(); let client = Client::new_with_config(homeserver_url, client_config).unwrap();
load_or_init_session(&client, session_file, username, password).await; load_or_init_session(&client, state_dir.join("session.json"), username, password).await;
client client
.add_event_emitter(Box::new(AutoJoinBot::new(client.clone()))) .add_event_emitter(Box::new(AutoJoinBot::new(client.clone())))
@ -174,17 +150,38 @@ async fn login_and_sync(
Ok(()) Ok(())
} }
#[derive(Clap)]
#[clap(version = "0.1", author = "Antoine Martin")]
struct Opts {
/// File where session information will be saved
#[clap(short, long, parse(from_os_str))]
config: PathBuf,
}
#[derive(Deserialize)]
struct Config {
homeserver: Url,
username: String,
password: String,
state_dir: PathBuf,
}
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), matrix_sdk::Error> { async fn main() -> Result<(), matrix_sdk::Error> {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
let opts = Opts::parse(); let opts = Opts::parse();
let config_file = opts.config;
let config: Config =
serde_json::from_reader(BufReader::new(File::open(config_file).unwrap())).unwrap();
login_and_sync( login_and_sync(
opts.homeserver, config.homeserver,
&opts.username, &config.username,
&opts.password, &config.password,
opts.store_path, config.state_dir,
opts.session,
) )
.await .await
} }