summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManos Pitsidianakis <el13635@mail.ntua.gr>2019-06-10 18:29:49 +0300
committerManos Pitsidianakis <el13635@mail.ntua.gr>2019-06-10 19:40:51 +0300
commit9de93b98d529cc77a09bb7c6eae2985ce5890e4d (patch)
treebbd43174b6ede99a9539ed13ad4b6ab74b19da0a
parentf1c72588c30f27a412dc1b927997fcdf78109baf (diff)
Update sample-config, and generate it if missing on startup
-rw-r--r--sample-config56
-rw-r--r--ui/src/conf.rs55
2 files changed, 95 insertions, 16 deletions
diff --git a/sample-config b/sample-config
index fe0c1f3a..8e130858 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
-
- [accounts.account-name.folders]
- "Inbox" = "inbox"
- "Sent" = "sent"
- "Drafts" = "drafts"
- "foobar-devel" = "devbar-fooel"
+# 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"
+
+# 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 5fa6e23e..f6e90e62 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<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 {