Skip to content

Commit

Permalink
git task unset
Browse files Browse the repository at this point in the history
  • Loading branch information
jhspetersson committed Sep 21, 2024
1 parent b6f31bd commit 9de0f8a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 19 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,11 @@ Sets task property:

git task set 1 description "I figured it out all wrong. Fizz Buzz has to be rewritten in Rust!"

Delete property:
### unset

git task set 1 foo bar
git task set 1 foo --delete
Delete a property:

git task unset 1 foo

### edit

Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ impl Task {
self.props.contains_key(prop)
}

pub fn delete_property(&mut self, prop: &str) {
self.props.remove(prop);
pub fn delete_property(&mut self, prop: &str) -> bool {
self.props.remove(prop).is_some()
}

pub fn get_comments(&self) -> &Option<Vec<Comment>> {
Expand Down
17 changes: 11 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::process::ExitCode;

use clap::{Parser, Subcommand};

use crate::operations::{task_clear, task_comment_add, task_comment_delete, task_comment_edit, task_create, task_delete, task_edit, task_export, task_get, task_import, task_list, task_pull, task_push, task_set, task_show, task_stats, task_status};
use crate::operations::{task_clear, task_comment_add, task_comment_delete, task_comment_edit, task_create, task_delete, task_edit, task_export, task_get, task_import, task_list, task_pull, task_push, task_set, task_show, task_stats, task_status, task_unset};
use crate::operations::config::*;

#[derive(Parser)]
Expand Down Expand Up @@ -109,10 +109,7 @@ enum Command {
/// property name
prop_name: String,
/// property value
value: Option<String>,
/// Delete the property
#[arg(short, long, conflicts_with = "value", visible_aliases = ["del", "remove", "rem"])]
delete: bool,
value: String,
/// Also push task to the remote source (e.g., GitHub)
#[arg(short, long)]
push: bool,
Expand All @@ -123,6 +120,13 @@ enum Command {
#[arg(long)]
no_color: bool,
},
/// Delete a property
Unset {
/// space separated task IDs
ids: Vec<String>,
/// property name
prop_name: String,
},
/// Edit a property
Edit {
/// task ID
Expand Down Expand Up @@ -470,7 +474,8 @@ fn main() -> ExitCode {
Some(Command::Create { name, description, no_desc, push, remote }) => task_create(name, description, no_desc, push, &remote),
Some(Command::Status { ids, status, push, remote, no_color }) => task_status(ids, status, push, &remote, no_color),
Some(Command::Get { id, prop_name }) => task_get(id, prop_name),
Some(Command::Set { id, prop_name, value, delete, push, remote, no_color }) => task_set(&id, prop_name, value, delete, push, &remote, no_color),
Some(Command::Set { id, prop_name, value, push, remote, no_color }) => task_set(&id, prop_name, value, push, &remote, no_color),
Some(Command::Unset { ids, prop_name }) => task_unset(ids, prop_name),
Some(Command::Edit { id, prop_name }) => task_edit(id, prop_name),
Some(Command::Comment { subcommand }) => task_comment(subcommand),
Some(Command::Import { ids, format }) => task_import(ids, format),
Expand Down
32 changes: 24 additions & 8 deletions src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub(crate) fn task_status(ids: Vec<String>, status: String, push: bool, remote:
let status = status_manager.get_full_status_name(&status);
let ids = ids.into_iter().expand_range().collect::<Vec<_>>();
for id in &ids {
task_set(id, "status".to_string(), Some(status.clone()), false, push, remote, no_color);
task_set(id, "status".to_string(), status.clone(), push, remote, no_color);
}

if push {
Expand All @@ -82,10 +82,9 @@ pub(crate) fn task_get(id: String, prop_name: String) -> bool {
}
}

pub(crate) fn task_set(id: &String, prop_name: String, value: Option<String>, delete: bool, push: bool, remote: &Option<String>, no_color: bool) -> bool {
pub(crate) fn task_set(id: &String, prop_name: String, value: String, push: bool, remote: &Option<String>, no_color: bool) -> bool {
match prop_name.as_str() {
"id" => {
let value = value.unwrap();
match gittask::update_task_id(&id, &value) {
Ok(_) => {
println!("Task ID {id} -> {value} updated");
Expand All @@ -103,11 +102,7 @@ pub(crate) fn task_set(id: &String, prop_name: String, value: Option<String>, de
_ => {
match gittask::find_task(&id) {
Ok(Some(mut task)) => {
if delete {
task.delete_property(&prop_name);
} else {
task.set_property(prop_name, value.unwrap());
}
task.set_property(prop_name, value);

match gittask::update_task(task) {
Ok(_) => {
Expand All @@ -129,6 +124,27 @@ pub(crate) fn task_set(id: &String, prop_name: String, value: Option<String>, de
}
}

pub(crate) fn task_unset(ids: Vec<String>, prop_name: String) -> bool {
for id in ids {
match gittask::find_task(&id) {
Ok(Some(mut task)) => {
if task.delete_property(&prop_name) {
match gittask::update_task(task) {
Ok(_) => println!("Task ID {id} updated"),
Err(e) => eprintln!("ERROR: {e}")
}
} else {
eprintln!("Task ID {id}: property not found")
}
},
Ok(None) => eprintln!("Task ID {id} not found"),
Err(e) => eprintln!("ERROR: {e}")
}
};

true
}

pub(crate) fn task_edit(id: String, prop_name: String) -> bool {
match gittask::find_task(&id) {
Ok(Some(mut task)) => {
Expand Down

0 comments on commit 9de0f8a

Please sign in to comment.