Update sample-config, and generate it if missing on startup

embed
Manos Pitsidianakis 2019-06-10 18:29:49 +03:00
parent f1c72588c3
commit 9de93b98d5
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
2 changed files with 94 additions and 15 deletions

View File

@ -1,15 +1,49 @@
[accounts.account-name]
root_folder = "/path/to/root/folder"
sent_folder = "/path/to/sent/folder" # optional
format = "Maildir"
threaded = true
# Setting up a Maildir account
#[accounts.account-name]
#root_folder = "/path/to/root/folder"
#sent_folder = "/path/to/sent/folder" # optional
#draft_folder = "Drafts"
#format = "Maildir"
#index = "Compact" # [plain, threaded, compact]
#identity="Name <email@address.tld>"
#display_name = "Name"
[accounts.account-name.folders]
"Inbox" = "inbox"
"Sent" = "sent"
"Drafts" = "drafts"
"foobar-devel" = "devbar-fooel"
# Set folder-specific settings
# [accounts.account-name.folders]
# "INBOX" = { rename="Inbox" }
# "drafts" = { rename="Drafts" }
# "foobar-devel" = { ignore = true } # don't show notifications for this folder
#[pager]
#pager_ratio = 80
#filter = "/usr/bin/pygmentize"
#[notifications]
#script = "notify-send"
#[shortcuts]
# Compact mail listing defaults:
#open_thread = '\n'
#exit_thread = 'i'
#create_contact = 'c'
#edit_contact = 'e'
# Mail listing defaults
#prev_page = PageUp,
#next_page = PageDown,
#prev_folder = 'K',
#next_folder = 'J',
#prev_account = 'l',
#next_account = 'h',
#new_mail = 'm',
# Pager defaults
#scroll_up = 'k',
#scroll_down = 'j',
#page_up = PageUp,
#page_down = PageDown,
#[mailer]
# required for sending e-mail
#mailer_cmd = 'msmtp --read-recipients --read-envelope-from'

View File

@ -44,6 +44,8 @@ use pager::PagerSettings;
use self::serde::{de, Deserialize, Deserializer};
use std::collections::HashMap;
use std::env;
use std::fs::OpenOptions;
use std::io::{self, BufRead, Write};
use std::path::PathBuf;
#[macro_export]
@ -180,8 +182,11 @@ impl FileAccount {
#[derive(Debug, Clone, Default, Deserialize)]
struct FileSettings {
accounts: HashMap<String, FileAccount>,
#[serde(default)]
pager: PagerSettings,
#[serde(default)]
notifications: NotificationsSettings,
#[serde(default)]
shortcuts: Shortcuts,
mailer: MailerSettings,
}
@ -226,14 +231,51 @@ impl FileSettings {
}
};
if !config_path.exists() {
panic!(
"Config file path `{}` doesn't exist or can't be created.",
println!(
"No configuration found. Would you like to generate one in {}? [Y/n]",
config_path.display()
);
let mut buffer = String::new();
let stdin = io::stdin();
let mut handle = stdin.lock();
loop {
buffer.clear();
handle
.read_line(&mut buffer)
.expect("Could not read from stdin.");
match buffer.trim() {
"Y" | "y" | "yes" | "YES" | "Yes" => {
let mut file = OpenOptions::new()
.write(true)
.create_new(true)
.open(config_path.as_path())
.expect("Could not create config file.");
file.write_all(include_str!("../../sample-config").as_bytes())
.expect("Could not write to config file.");
println!("Written config to {}", config_path.display());
std::process::exit(1);
}
"n" | "N" | "no" | "No" | "NO" => {
std::process::exit(1);
}
_ => {
println!(
"No configuration found. Would you like to generate one in {}? [Y/n]",
config_path.display()
);
}
}
}
}
let mut s = Config::new();
s.merge(File::new(config_path.to_str().unwrap(), FileFormat::Toml))
.unwrap();
if s.merge(File::new(config_path.to_str().unwrap(), FileFormat::Toml))
.is_err()
{
println!("Config file contains errors.");
std::process::exit(1);
}
/* No point in returning without a config file. */
match s.try_into() {
@ -245,7 +287,10 @@ impl FileSettings {
impl Settings {
pub fn new() -> Settings {
let fs = FileSettings::new().unwrap_or_else(|e| panic!(format!("{}", e)));
let fs = FileSettings::new().unwrap_or_else(|e| {
println!("Configuration error: {}", e);
std::process::exit(1);
});
let mut s: HashMap<String, AccountConf> = HashMap::new();
for (id, x) in fs.accounts {