Add category feature

This commit is contained in:
Thom Dickson 2021-12-21 20:35:40 -05:00
parent 789b5fb6b9
commit d1592e83a5
Signed by: boots
GPG Key ID: 40BE2AF8EBF8D2BB
2 changed files with 63 additions and 13 deletions

View File

@ -18,13 +18,15 @@ pub struct Config {
pub struct Repository { pub struct Repository {
pub name: String, pub name: String,
pub url: String, pub url: String,
pub category: Option<String>,
} }
impl Repository { impl Repository {
pub fn new(name: String, url: String) -> Repository { pub fn new(name: String, url: String, category: Option<String>) -> Repository {
Repository { Repository {
name, name,
url, url,
category,
} }
} }
} }

View File

@ -14,6 +14,7 @@ pub struct State {
cmd: Option<Cmd>, cmd: Option<Cmd>,
url: Option<String>, url: Option<String>,
name: Option<String>, name: Option<String>,
category: Option<String>,
pub config: String, pub config: String,
} }
@ -45,6 +46,7 @@ impl State {
cmd: None, cmd: None,
url: None, url: None,
name: None, name: None,
category: None,
config: format!("{}/.config/shepherd/config.toml", env::var("HOME").unwrap()), config: format!("{}/.config/shepherd/config.toml", env::var("HOME").unwrap()),
}; };
@ -79,7 +81,19 @@ impl State {
} else if let None = state.cmd { } else if let None = state.cmd {
// add command // add command
if x == "add" { if x == "add" {
let name = args.next(); let next = args.next();
let name: Option<String>;
match next {
Some(ref x) => match &x[..] {
"--category" => {
state.category = args.next();
name = args.next();
}
_ => name = next,
},
None => name = None,
}
eprintln!("Category: {:?}", name);
match name { match name {
Some(x) => { Some(x) => {
state.name = Some(x); state.name = Some(x);
@ -150,6 +164,10 @@ pub fn run(state: State, mut config: Config) -> Result<(), Box<dyn Error>> {
Some(x) => x, Some(x) => x,
_ => String::new(), _ => String::new(),
}, },
match state.category {
Some(x) => Some(x),
_ => None,
},
); );
if let None = config.repositories.iter().find(|x| **x == repo) { if let None = config.repositories.iter().find(|x| **x == repo) {
config.repositories.push(repo); config.repositories.push(repo);
@ -161,7 +179,7 @@ pub fn run(state: State, mut config: Config) -> Result<(), Box<dyn Error>> {
} }
Some(Cmd::List) => { Some(Cmd::List) => {
// Get the largest entry by name // Get the largest entry by name
let width: usize = match config let name_width: usize = match config
.repositories .repositories
.iter() .iter()
.max_by(|x, y| x.name.len().cmp(&y.name.len())) .max_by(|x, y| x.name.len().cmp(&y.name.len()))
@ -169,9 +187,33 @@ pub fn run(state: State, mut config: Config) -> Result<(), Box<dyn Error>> {
Some(x) => x.name.len(), Some(x) => x.name.len(),
None => 0, None => 0,
}; };
println!("{:width$} URL", "Name", width = width); let url_width: usize = match config
.repositories
.iter()
.max_by(|x, y| x.url.len().cmp(&y.url.len()))
{
Some(x) => x.url.len(),
None => 0,
};
println!(
"{:name$} {:url$} Category",
"Name",
"URL",
name = name_width,
url = url_width
);
for repo in config.repositories.iter() { for repo in config.repositories.iter() {
println!("{:width$} {}", repo.name, repo.url, width = width); println!(
"{name:name_w$} {url:url_w$} {cat}",
name = repo.name,
name_w = name_width,
url = repo.url,
url_w = url_width,
cat = match &repo.category {
Some(x) => x,
None => "",
},
);
} }
} }
Some(Cmd::Fetch) => { Some(Cmd::Fetch) => {
@ -194,23 +236,29 @@ fn fetch_repos(config: Config) {
// Loop through the repositories // Loop through the repositories
for repo in config.repositories.iter() { for repo in config.repositories.iter() {
// Get the full path to the repository
let path: String = match &repo.category {
Some(x) => format!("{}/{}/", config.source_dir, x),
None => config.source_dir.clone(),
};
let fullpath: String = format!("{}/{}", path, repo.name);
// Check to see if the folder is already cloned, if so, just fetch // 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() { if std::path::Path::new(&fullpath).is_dir() {
println!("=== FETCHING {} ===", repo.name); println!("=== FETCHING {} ===", repo.name);
Command::new("git") Command::new("git")
.args([ .args(["-C", &fullpath, "fetch", "--all"])
"-C",
&format!("{}/{}", config.source_dir, repo.name),
"fetch",
"--all",
])
.status() .status()
.unwrap(); .unwrap();
} else { } else {
println!("=== {} doesn't exist locally ===", repo.name); println!("=== {} doesn't exist locally ===", repo.name);
println!("=== CLONING {} ===", repo.name); println!("=== CLONING {} ===", repo.name);
match fs::create_dir_all(&path) {
Err(e) => eprintln!("{}", e),
_ => {}
}
println!("{}", path);
Command::new("git") Command::new("git")
.args(["-C", &config.source_dir, "clone", &repo.url, &repo.name]) .args(["-C", &path, "clone", &repo.url, &repo.name])
.status() .status()
.unwrap(); .unwrap();
} }