From 9de93b98d529cc77a09bb7c6eae2985ce5890e4d Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Mon, 10 Jun 2019 18:29:49 +0300 Subject: [PATCH] Update sample-config, and generate it if missing on startup --- sample-config | 54 ++++++++++++++++++++++++++++++++++++++++--------- ui/src/conf.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 94 insertions(+), 15 deletions(-) diff --git a/sample-config b/sample-config index fe0c1f3a6..8e130858c 100644 --- a/sample-config +++ b/sample-config @@ -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 " +#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' diff --git a/ui/src/conf.rs b/ui/src/conf.rs index 5fa6e23e2..f6e90e62c 100644 --- a/ui/src/conf.rs +++ b/ui/src/conf.rs @@ -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, + #[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 = HashMap::new(); for (id, x) in fs.accounts {