Skip to content

Commit

Permalink
version 0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mckernant1 committed Aug 27, 2019
1 parent bd9530d commit 9848d1d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ subcommands:
- PATH:
required: true
help: The directory of the git repo to add
- delete:
- rm:
about: remove the repo from the list maintained by global git
args:
- remove-dir:
Expand Down
5 changes: 2 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ fn call_subcommands(matches: ArgMatches, settings_file: SettingsFile) {
list(matches, settings_file)
} else if matches.is_present("add") {
add(matches, settings_file)
} else if matches.is_present("rm") {
delete(matches, settings_file)
}
// else if matches.is_present("delete") {
// delete(matches, settings_file)
// }
}

45 changes: 43 additions & 2 deletions src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl SettingsFile {
}

pub fn add_repo(self, repo_path: &str, repo_name: &str) {
let repos = get_settings_json(
let settings_json = get_settings_json(
&mut File::open(
self.settings_file_path.clone()
).unwrap()
Expand All @@ -38,7 +38,7 @@ impl SettingsFile {
"path" => repo_path
};

let mut repos_mut = repos.clone();
let mut repos_mut = settings_json.clone();

if !repos_mut["repos"].is_array() {
repos_mut["repos"] = array![];
Expand All @@ -54,6 +54,33 @@ impl SettingsFile {
).unwrap();
}

pub fn delete_repo(self, repo_name: &str) {
let settings_json = get_settings_json(
&mut File::open(
self.settings_file_path.clone()
).unwrap()
);
let mut settings_mut = settings_json.clone();

let members = settings_mut["repos"].members();
let mut new_repos = array!();

for member in members {
if member["name"].as_str().unwrap() != repo_name {
new_repos.push(member.clone());
}
}

settings_mut["repos"] = new_repos;

let settings_string = json::stringify(settings_mut);

fs::write(
self.settings_file_path.clone(),
settings_string.as_bytes(),
).unwrap();
}

pub fn list_repos(self) -> JsonValue {
let repos =
get_settings_json(
Expand All @@ -68,6 +95,20 @@ impl SettingsFile {
let mut file = File::open(self.settings_file_path).unwrap();
get_settings_json(&mut file)
}

pub fn get_repo_by_name(self, repo_name: &str) -> JsonValue {
let settings_json = get_settings_json(
&mut File::open(
self.settings_file_path.clone()
).unwrap()
);

let repo = settings_json["repos"].members().find(
|r| {
r["name"].as_str().unwrap() == repo_name
}).unwrap();
return (*repo).clone();
}
}

fn create_settings_file(settings_file_path: String) -> File {
Expand Down
15 changes: 13 additions & 2 deletions src/subcommands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,29 @@ pub fn add(matches: ArgMatches, settings_file: SettingsFile) {
let repo_name =
path_string.chars()
.rev()
.take_while(|c| { c != &'/' } )
.take_while(|c| { c != &'/' })
.collect::<String>()
.chars()
.rev()
.collect::<String>();

settings_file.add_repo(
path_string,
repo_name.as_str()
repo_name.as_str(),
);
}

pub fn delete(matches: ArgMatches, settings_file: SettingsFile) {
let repo_name = matches
.subcommand_matches("rm").unwrap()
.value_of("NAME").unwrap();

if matches.subcommand_matches("rm").unwrap()
.is_present("remove-dir") {
let repo = settings_file.clone().get_repo_by_name(repo_name);
let repo_path = repo["path"].as_str().unwrap();
fs::remove_dir_all(repo_path).unwrap();
}

settings_file.clone().delete_repo(repo_name)
}

0 comments on commit 9848d1d

Please sign in to comment.