From f572d6d8b2cf2d5d1e2ae335c46506ec6ae6dd54 Mon Sep 17 00:00:00 2001 From: Thom Dickson Date: Thu, 16 Dec 2021 20:50:30 -0500 Subject: [PATCH] Implement the add command --- src/config.rs | 28 +++++++++++++++++++++++++--- src/lib.rs | 20 ++++++++++++++------ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/src/config.rs b/src/config.rs index e8bb711..fc8a4e9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -7,16 +7,28 @@ use std::path::Path; #[derive(Debug, Serialize, Deserialize)] /// The internal representation of the configuration file. pub struct Config { + #[serde(skip)] // Skip serializing the configration file location + config_location: String, pub source_dir: String, - repositories: Vec, + pub repositories: Vec, } #[derive(Debug, Serialize, Deserialize)] -struct Repository { +/// The internal representation of a single repository +pub struct Repository { name: String, url: String, } +impl Repository { + pub fn new(url: String) -> Repository { + Repository { + name: url.clone(), + url, + } + } +} + impl Config { /// Create new Config struct with default values /// @@ -25,12 +37,13 @@ impl Config { Config { source_dir: format!("{}/sources", env::var("HOME").unwrap()), repositories: vec![], + config_location: String::new(), } } /// 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. pub fn read_config(&mut self, filename: &str) -> Result<(), Box> { // Load raw yaml file. If the file doesn't exist, create it @@ -51,6 +64,15 @@ impl Config { // Write default Config struct to 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> { + fs::write(&self.config_location, toml::to_string(&self)?)?; Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 922de8c..c7217e2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ pub mod config; -use config::Config; +use config::{Config, Repository}; use std::error::Error; use std::env; @@ -17,7 +17,7 @@ pub struct State { #[derive(Debug)] /// Used to signal which main command is going to be run enum Cmd { - Fetch, + Add, Help, DumpConfig, } @@ -68,13 +68,13 @@ impl State { } } } - // Fetch command - else if x == "fetch" { + // add command + else if x == "add" { let url = args.next(); match url { Some(x) => match state.cmd { None => { - state.cmd = Some(Cmd::Fetch); + state.cmd = Some(Cmd::Add); state.url = Some(x); } _ => {} @@ -96,7 +96,8 @@ impl State { } } -pub fn run(state: State, config: Config) -> Result<(), Box> { +#[allow(unreachable_patterns)] +pub fn run(state: State, mut config: Config) -> Result<(), Box> { match state.cmd { Some(Cmd::Help) => { println!("{}", help_msg()); @@ -104,6 +105,13 @@ pub fn run(state: State, config: Config) -> Result<(), Box> { Some(Cmd::DumpConfig) => { 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) => { println!("{:?} hasn't been implemented yet!", x) }