add default to create a new config toml file when none found

This commit is contained in:
Alie 2024-01-29 18:09:58 +01:00
parent d60df6bb31
commit 6bcadd25ac
3 changed files with 60 additions and 11 deletions

2
Cargo.lock generated
View File

@ -850,7 +850,7 @@ dependencies = [
[[package]] [[package]]
name = "mastodon-image-uploader-bot" name = "mastodon-image-uploader-bot"
version = "0.3.0" version = "0.3.1"
dependencies = [ dependencies = [
"async-std", "async-std",
"log", "log",

39
compose.yaml Normal file
View File

@ -0,0 +1,39 @@
version: "3"
services:
mongodb:
image: mongo:bionic
container_name: mongodb
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: password
MONGO_INITDB_DATABASE: bot
volumes:
- mongodb_data:/data/db
bot-api:
image: git.fai.st/fedi-image-bot/bot-api:v1.0.0
container_name: bot-api
ports:
- "8080:8080"
depends_on:
- mongodb
environment:
MONGODB_URI: "mongodb://mongodb:27017/bot"
MONGODB_USER: "root"
MONGODB_PASS: "password"
JWTSECRET: "cooljwtsecret"
bot:
image: rust
container_name: bot
working_dir: /app
depends_on:
- bot-api
volumes:
- ./:/app:rw
volumes:
mongodb_data:

View File

@ -7,7 +7,7 @@ use std::io::{Cursor, Write};
use std::process::exit; use std::process::exit;
use std::time::Duration; use std::time::Duration;
#[derive(Debug, Serialize)] #[derive(Serialize)]
struct AccountUpdate { struct AccountUpdate {
note: String, note: String,
} }
@ -16,28 +16,28 @@ const CONFIG_FILENAME: &str = "config.toml";
type DynResult<T> = Result<T, Box<dyn std::error::Error>>; type DynResult<T> = Result<T, Box<dyn std::error::Error>>;
#[derive(Deserialize)] #[derive(Deserialize, Serialize, Default)]
struct Config { struct Config {
bot: Bot, bot: Bot,
files: Files, files: Files,
errors: Errors, errors: Errors,
} }
#[derive(Deserialize)] #[derive(Deserialize, Serialize, Default)]
struct Bot { struct Bot {
name: String, name: String,
instance: String, instance: String,
bio: String, bio: String,
} }
#[derive(Deserialize)] #[derive(Deserialize, Serialize, Default)]
struct Files { struct Files {
posted: String, posted: String,
urls: String, urls: String,
tempfile: String, tempfile: String,
} }
#[derive(Deserialize)] #[derive(Deserialize, Serialize, Default)]
struct Errors { struct Errors {
maintainers: String, maintainers: String,
out_of_images: String, out_of_images: String,
@ -129,11 +129,26 @@ fn get_config() -> Config {
Ok(config) => config, Ok(config) => config,
Err(err) => { Err(err) => {
log::error!("Config file parsing unsuccesful: {}", err); log::error!("Config file parsing unsuccesful: {}", err);
generate_config().unwrap();
log::info!("Please check the working directory to find a new config file");
exit(1); exit(1);
} }
} }
} }
fn generate_config() -> DynResult<()> {
let config = Config::default();
let config = toml::to_string(&config)?;
std::fs::write(CONFIG_FILENAME, &config)?;
Ok(())
}
/// Parses the given filename to a config struct
fn parse_config(filename: &str) -> DynResult<Config> {
let toml_file = std::fs::read_to_string(filename)?; //.expect("No config file, consider getting the original one and modifing it");
Ok(toml::from_str(&toml_file)?) //("Malformed config file, check the original one for reference")
}
async fn get_account(config: &Config) -> Mastodon { async fn get_account(config: &Config) -> Mastodon {
if let Ok(data) = masto_toml::from_file("mastodon-data.toml") { if let Ok(data) = masto_toml::from_file("mastodon-data.toml") {
Mastodon::from(data) Mastodon::from(data)
@ -148,11 +163,6 @@ async fn get_account(config: &Config) -> Mastodon {
} }
} }
/// Parses the given filename to a config struct
fn parse_config(filename: &str) -> DynResult<Config> {
let toml_file = std::fs::read_to_string(filename)?; //.expect("No config file, consider getting the original one and modifing it");
Ok(toml::from_str(&toml_file)?) //("Malformed config file, check the original one for reference")
}
fn get_next_url(config: &Config) -> DynResult<Option<String>> { fn get_next_url(config: &Config) -> DynResult<Option<String>> {
let binding = std::fs::read_to_string(&config.files.posted)?; //.expect("Posted file not found"); let binding = std::fs::read_to_string(&config.files.posted)?; //.expect("Posted file not found");