src: simplify file reading
This commit is contained in:
parent
60f1978eed
commit
c3fe4c7230
|
@ -3,11 +3,8 @@ use json::{object, JsonValue};
|
|||
use rand::rngs::OsRng;
|
||||
use regex::Regex;
|
||||
|
||||
use std::fs::File;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use std::io::prelude::*;
|
||||
|
||||
pub trait SSBKeypair {
|
||||
fn to_json(&self) -> JsonValue;
|
||||
fn from_json(obj: JsonValue) -> Self;
|
||||
|
@ -55,9 +52,7 @@ impl SSBKeypair for Keypair {
|
|||
|
||||
fn read_or_generate(path: PathBuf) -> Self {
|
||||
if path.exists() {
|
||||
let mut secret_file = File::open(path).unwrap();
|
||||
let mut secret = String::new();
|
||||
secret_file.read_to_string(&mut secret).unwrap();
|
||||
let secret = std::fs::read_to_string(path).unwrap();
|
||||
let re = Regex::new(r"\s*#[^\n]*").unwrap();
|
||||
let secret = re.replace_all(secret.as_str(), "");
|
||||
SSBKeypair::from_json(json::parse(&secret).unwrap())
|
||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -1,24 +1,14 @@
|
|||
use clap::{load_yaml, App};
|
||||
use ed25519_dalek::Keypair;
|
||||
|
||||
use std::fs::File;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use std::io::prelude::*;
|
||||
|
||||
mod keypair;
|
||||
|
||||
use keypair::SSBKeypair;
|
||||
|
||||
type Config = toml::map::Map<String, toml::Value>;
|
||||
|
||||
fn read_config(path: PathBuf) -> Config {
|
||||
let mut file = File::open(path).unwrap();
|
||||
let mut content = String::new();
|
||||
file.read_to_string(&mut content).unwrap();
|
||||
toml::from_str(content.as_str()).unwrap()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let options = load_yaml!("options.yaml");
|
||||
let options = App::from(options).get_matches();
|
||||
|
@ -30,7 +20,8 @@ fn main() {
|
|||
.join("cosmoline")
|
||||
.join("config.toml"),
|
||||
};
|
||||
let config = read_config(config_file);
|
||||
let config = std::fs::read_to_string(config_file).unwrap();
|
||||
let config: Config = toml::from_str(config.as_str()).unwrap();
|
||||
|
||||
let path = match options.value_of("path") {
|
||||
Some(path) => PathBuf::from(path),
|
||||
|
|
Loading…
Reference in New Issue