add --config and --no-config

pull/14/head
chris west 4 years ago
parent bbf0d4b9dd
commit be12941ce3

@ -39,6 +39,9 @@ the gophersphere.
-p, --print Print rendered Gopher response only
-l, --local Connect to 127.0.0.1:7070
-c, --config FILE Use instead of ~/.config/phetch/phetch.conf
-C, --no-config Don't use any config file
-h, --help Show this screen
-v, --version Show phetch version

@ -1,6 +1,7 @@
use crate::phetchdir;
use std::{
collections::HashMap,
fs::OpenOptions,
io::{Read, Result},
};
@ -10,10 +11,8 @@ const CONFIG_FILE: &str = "phetch.conf";
/// Default start page.
const DEFAULT_START: &str = "gopher://phetch/1/home";
/// Default config. Currently only used internally.
#[allow(dead_code)]
const DEFAULT_CONFIG: &str = "
## config file for the phetch gopher client
/// Default config
pub const DEFAULT_CONFIG: &str = "## default config file for the phetch gopher client
## gopher://phkt.io/1/phetch
# Page to load when launched with no URL argument.
@ -61,7 +60,7 @@ pub fn default() -> Config {
Default::default()
}
/// Attempt to load config from disk, specifically `CONFIG_FILE`.
/// Attempt to load ~/.config/phetch/phetch.conf from disk.
pub fn load() -> Result<Config> {
let mut reader = phetchdir::load(CONFIG_FILE)?;
let mut file = String::new();
@ -69,6 +68,14 @@ pub fn load() -> Result<Config> {
parse(&file)
}
/// Attempt to load a config from disk.
pub fn load_file(path: &str) -> Result<Config> {
let mut reader = OpenOptions::new().read(true).open(&path)?;
let mut file = String::new();
reader.read_to_string(&mut file)?;
parse(&file)
}
/// Does the config file exist?
pub fn exists() -> bool {
phetchdir::exists(CONFIG_FILE)

@ -33,6 +33,8 @@ fn run() -> i32 {
let mut set_notls = false;
let mut set_tor = false;
let mut set_notor = false;
let mut set_cfg = false;
let mut set_nocfg = false;
while let Some(arg) = iter.next() {
match arg.as_ref() {
"-v" | "--version" | "-version" => {
@ -55,6 +57,53 @@ fn run() -> i32 {
mode = Mode::Print;
}
"-l" | "--local" | "-local" => cfg.start = "gopher://127.0.0.1:7070".into(),
"-C" => {
if set_cfg {
eprintln!("can't mix --config and --no-config");
return 1;
}
set_nocfg = true;
cfg = config::default();
}
"-c" | "--config" | "-config" => {
if set_nocfg {
eprintln!("can't mix --config and --no-config");
return 1;
}
set_cfg = true;
if let Some(arg) = iter.next() {
cfg = match config::load_file(arg) {
Ok(c) => c,
Err(e) => {
eprintln!("error loading config: {}", e);
return 1;
}
};
} else {
eprintln!("need a config file");
return 1;
}
}
arg if arg.starts_with("--config=") || arg.starts_with("-config=") => {
if set_nocfg {
eprintln!("can't mix --config and --no-config");
return 1;
}
set_cfg = true;
let mut parts = arg.splitn(2, '=');
if let Some(file) = parts.nth(1) {
cfg = match config::load_file(file) {
Ok(c) => c,
Err(e) => {
eprintln!("error loading config: {}", e);
return 1;
}
};
} else {
eprintln!("need a config file");
return 1;
}
}
"-s" | "--tls" | "-tls" => {
if set_notls {
eprintln!("can't set both --tls and --no-tls");
@ -178,6 +227,9 @@ Options:
-p, --print Print rendered Gopher response only
-l, --local Connect to 127.0.0.1:7070
-c, --config FILE Use instead of ~/.config/phetch/phetch.conf
-C, --no-config Don't use any config file
-h, --help Show this screen
-v, --version Show phetch version

Loading…
Cancel
Save