Implement the add command

This commit is contained in:
Thom Dickson 2021-12-16 20:50:30 -05:00
parent ee411a6f9b
commit f572d6d8b2
Signed by: boots
GPG Key ID: 40BE2AF8EBF8D2BB
2 changed files with 39 additions and 9 deletions

View File

@ -7,16 +7,28 @@ use std::path::Path;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
/// The internal representation of the configuration file. /// The internal representation of the configuration file.
pub struct Config { pub struct Config {
#[serde(skip)] // Skip serializing the configration file location
config_location: String,
pub source_dir: String, pub source_dir: String,
repositories: Vec<Repository>, pub repositories: Vec<Repository>,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
struct Repository { /// The internal representation of a single repository
pub struct Repository {
name: String, name: String,
url: String, url: String,
} }
impl Repository {
pub fn new(url: String) -> Repository {
Repository {
name: url.clone(),
url,
}
}
}
impl Config { impl Config {
/// Create new Config struct with default values /// Create new Config struct with default values
/// ///
@ -25,12 +37,13 @@ impl Config {
Config { Config {
source_dir: format!("{}/sources", env::var("HOME").unwrap()), source_dir: format!("{}/sources", env::var("HOME").unwrap()),
repositories: vec![], repositories: vec![],
config_location: String::new(),
} }
} }
/// Read TOML file and load values into a Config struct /// Read TOML file and load values into a Config struct
/// ///
/// If the filename doesn't exist, read_config() will write the current struct to the given /// If the filename doesn't exist, `read_config()` will write the current struct to the given
/// config file. /// config file.
pub fn read_config(&mut self, filename: &str) -> Result<(), Box<dyn Error>> { pub fn read_config(&mut self, filename: &str) -> Result<(), Box<dyn Error>> {
// Load raw yaml file. If the file doesn't exist, create it // Load raw yaml file. If the file doesn't exist, create it
@ -51,6 +64,15 @@ impl Config {
// Write default Config struct to file // Write default Config struct to file
fs::write(filename, config).expect("Couldn't write file"); fs::write(filename, config).expect("Couldn't write file");
} }
self.config_location = filename.to_string();
Ok(())
}
/// Save the current configuration to a file.
///
/// This has to be called after `read_config()`.
pub fn save_config(&self) -> Result<(), Box<dyn Error>> {
fs::write(&self.config_location, toml::to_string(&self)?)?;
Ok(()) Ok(())
} }

View File

@ -1,5 +1,5 @@
pub mod config; pub mod config;
use config::Config; use config::{Config, Repository};
use std::error::Error; use std::error::Error;
use std::env; use std::env;
@ -17,7 +17,7 @@ pub struct State {
#[derive(Debug)] #[derive(Debug)]
/// Used to signal which main command is going to be run /// Used to signal which main command is going to be run
enum Cmd { enum Cmd {
Fetch, Add,
Help, Help,
DumpConfig, DumpConfig,
} }
@ -68,13 +68,13 @@ impl State {
} }
} }
} }
// Fetch command // add command
else if x == "fetch" { else if x == "add" {
let url = args.next(); let url = args.next();
match url { match url {
Some(x) => match state.cmd { Some(x) => match state.cmd {
None => { None => {
state.cmd = Some(Cmd::Fetch); state.cmd = Some(Cmd::Add);
state.url = Some(x); state.url = Some(x);
} }
_ => {} _ => {}
@ -96,7 +96,8 @@ impl State {
} }
} }
pub fn run(state: State, config: Config) -> Result<(), Box<dyn Error>> { #[allow(unreachable_patterns)]
pub fn run(state: State, mut config: Config) -> Result<(), Box<dyn Error>> {
match state.cmd { match state.cmd {
Some(Cmd::Help) => { Some(Cmd::Help) => {
println!("{}", help_msg()); println!("{}", help_msg());
@ -104,6 +105,13 @@ pub fn run(state: State, config: Config) -> Result<(), Box<dyn Error>> {
Some(Cmd::DumpConfig) => { Some(Cmd::DumpConfig) => {
println!("{}", config.to_string().unwrap()); println!("{}", config.to_string().unwrap());
} }
Some(Cmd::Add) => {
config.repositories.push(Repository::new(match state.url {
Some(x) => x,
None => String::new(),
}));
config.save_config()?;
}
Some(x) => { Some(x) => {
println!("{:?} hasn't been implemented yet!", x) println!("{:?} hasn't been implemented yet!", x)
} }