Implement fetch command
This commit is contained in:
parent
50ade80ab9
commit
9da42c44c3
43
src/lib.rs
43
src/lib.rs
|
@ -2,6 +2,8 @@ pub mod config;
|
||||||
use config::{Config, Repository};
|
use config::{Config, Repository};
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
use std::fs;
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
/// Holds the current state of the application.
|
/// Holds the current state of the application.
|
||||||
|
@ -35,6 +37,9 @@ impl State {
|
||||||
/// - `--long-flag` type arguments
|
/// - `--long-flag` type arguments
|
||||||
/// - `-mcsf` multi character short flag type arguments
|
/// - `-mcsf` multi character short flag type arguments
|
||||||
/// - `command` type arguments
|
/// - `command` type arguments
|
||||||
|
///
|
||||||
|
/// The command argument is deterministic in the sense that it doesn't change once it's set. If
|
||||||
|
/// no command has been given by the end of the parsing loop, it defaults to the help command
|
||||||
pub fn new(mut args: std::env::Args) -> State {
|
pub fn new(mut args: std::env::Args) -> State {
|
||||||
let mut state = State {
|
let mut state = State {
|
||||||
cmd: None,
|
cmd: None,
|
||||||
|
@ -124,6 +129,9 @@ impl State {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unreachable_patterns)]
|
#[allow(unreachable_patterns)]
|
||||||
|
/// This is the main run function for the program.
|
||||||
|
///
|
||||||
|
/// It starts by interpreting the state of the program as set by the argument parsing.
|
||||||
pub fn run(state: State, mut config: Config) -> Result<(), Box<dyn Error>> {
|
pub fn run(state: State, mut config: Config) -> Result<(), Box<dyn Error>> {
|
||||||
match state.cmd {
|
match state.cmd {
|
||||||
Some(Cmd::Help) => {
|
Some(Cmd::Help) => {
|
||||||
|
@ -166,6 +174,9 @@ pub fn run(state: State, mut config: Config) -> Result<(), Box<dyn Error>> {
|
||||||
println!("{:width$} {}", repo.name, repo.url, width = width);
|
println!("{:width$} {}", repo.name, repo.url, width = width);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Some(Cmd::Fetch) => {
|
||||||
|
fetch_repos(config);
|
||||||
|
}
|
||||||
Some(x) => {
|
Some(x) => {
|
||||||
println!("{:?} hasn't been implemented yet!", x)
|
println!("{:?} hasn't been implemented yet!", x)
|
||||||
}
|
}
|
||||||
|
@ -174,6 +185,38 @@ pub fn run(state: State, mut config: Config) -> Result<(), Box<dyn Error>> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn fetch_repos(config: Config) {
|
||||||
|
// Make sure sources directory exists
|
||||||
|
match fs::create_dir_all(&config.source_dir) {
|
||||||
|
Err(e) => eprintln!("{}", e),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loop through the repositories
|
||||||
|
for repo in config.repositories.iter() {
|
||||||
|
// Check to see if the folder is already cloned, if so, just fetch
|
||||||
|
if std::path::Path::new(&format!("{}/{}", config.source_dir, repo.name)).is_dir() {
|
||||||
|
println!("=== FETCHING {} ===", repo.name);
|
||||||
|
Command::new("git")
|
||||||
|
.args([
|
||||||
|
"-C",
|
||||||
|
&format!("{}/{}", config.source_dir, repo.name),
|
||||||
|
"fetch",
|
||||||
|
"--all",
|
||||||
|
])
|
||||||
|
.status()
|
||||||
|
.unwrap();
|
||||||
|
} else {
|
||||||
|
println!("=== {} doesn't exist locally ===", repo.name);
|
||||||
|
println!("=== CLONING {} ===", repo.name);
|
||||||
|
Command::new("git")
|
||||||
|
.args(["-C", &config.source_dir, "clone", &repo.url, &repo.name])
|
||||||
|
.status()
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn help_msg() -> String {
|
fn help_msg() -> String {
|
||||||
format!(
|
format!(
|
||||||
"Git repository manager
|
"Git repository manager
|
||||||
|
|
Loading…
Reference in New Issue