Implement the add command
This commit is contained in:
parent
ee411a6f9b
commit
f572d6d8b2
|
@ -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<Repository>,
|
||||
pub repositories: Vec<Repository>,
|
||||
}
|
||||
|
||||
#[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<dyn Error>> {
|
||||
// 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<dyn Error>> {
|
||||
fs::write(&self.config_location, toml::to_string(&self)?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
20
src/lib.rs
20
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<dyn Error>> {
|
||||
#[allow(unreachable_patterns)]
|
||||
pub fn run(state: State, mut config: Config) -> Result<(), Box<dyn Error>> {
|
||||
match state.cmd {
|
||||
Some(Cmd::Help) => {
|
||||
println!("{}", help_msg());
|
||||
|
@ -104,6 +105,13 @@ pub fn run(state: State, config: Config) -> Result<(), Box<dyn Error>> {
|
|||
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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue