@@ -13,6 +13,27 @@ pub struct Location {
13
13
location : String ,
14
14
}
15
15
16
+ fn unmark_path ( mut connection : Connection , path : & str ) -> Result < ( ) , GeneralError > {
17
+ let dir = PathBuf :: from ( path. trim ( ) ) ;
18
+ if !Path :: new ( & dir) . exists ( ) {
19
+ panic ! ( "Path does not exist" ) ;
20
+ }
21
+
22
+ let absolute_path = fs:: canonicalize ( & dir) ?. to_string_lossy ( ) . to_string ( ) ;
23
+
24
+ // Use a transaction in-case we fail to insert a tag at some point.
25
+ let tx = connection. transaction ( ) ?;
26
+
27
+ tx. execute (
28
+ "DELETE FROM tagged WHERE location = ?1;" ,
29
+ & [ & absolute_path] ,
30
+ ) ?;
31
+
32
+ tx. commit ( ) ?;
33
+
34
+ Ok ( ( ) )
35
+ }
36
+
16
37
fn mark_path ( mut connection : Connection , path : & str , tags : & str ) -> Result < ( ) , GeneralError > {
17
38
let dir = PathBuf :: from ( path. trim ( ) ) ;
18
39
if !Path :: new ( & dir) . exists ( ) {
@@ -82,7 +103,7 @@ fn main() -> Result<(), GeneralError> {
82
103
}
83
104
}
84
105
85
- let connection = Connection :: open ( dir) ?;
106
+ let mut connection = Connection :: open ( dir) ?;
86
107
87
108
connection. execute (
88
109
"CREATE TABLE IF NOT EXISTS tagged (
@@ -98,6 +119,11 @@ fn main() -> Result<(), GeneralError> {
98
119
. propagate_version ( true )
99
120
. subcommand_required ( true )
100
121
. arg_required_else_help ( true )
122
+ . subcommand (
123
+ Command :: new ( "unmark" )
124
+ . about ( "Remove all tags from a specified path" )
125
+ . arg ( clap:: arg!( [ PATH ] ) )
126
+ )
101
127
. subcommand (
102
128
Command :: new ( "mark" )
103
129
. about ( "Give a path specified tags" )
@@ -108,6 +134,11 @@ fn main() -> Result<(), GeneralError> {
108
134
clap:: arg!( -c --"in-cwd" [ IN_CWD ] "filters by paths in the current working directory" ) ,
109
135
clap:: arg!( [ TAGS ] ) ,
110
136
] ) )
137
+ . subcommand (
138
+ Command :: new ( "deltag" )
139
+ . about ( "Remove specified tags from all paths" )
140
+ . arg ( clap:: arg!( [ TAGS ] ) )
141
+ )
111
142
. subcommand ( Command :: new ( "tags" ) . about ( "Get a list of all tags" ) )
112
143
. get_matches ( ) ;
113
144
@@ -117,6 +148,10 @@ fn main() -> Result<(), GeneralError> {
117
148
let tags: String = sub_matches. value_of_t_or_exit ( "TAGS" ) ;
118
149
mark_path ( connection, & path, & tags) ?;
119
150
}
151
+ Some ( ( "unmark" , sub_matches) ) => {
152
+ let path: String = sub_matches. value_of_t_or_exit ( "PATH" ) ;
153
+ unmark_path ( connection, & path) ?;
154
+ }
120
155
Some ( ( "find" , sub_matches) ) => {
121
156
let tags: String = sub_matches. value_of_t_or_exit ( "TAGS" ) ;
122
157
let in_cwd: bool = sub_matches
@@ -132,6 +167,19 @@ fn main() -> Result<(), GeneralError> {
132
167
println ! ( "{}" , row?) ;
133
168
}
134
169
}
170
+ Some ( ( "deltag" , sub_matches) ) => {
171
+ let tags: String = sub_matches. value_of_t_or_exit ( "TAGS" ) ;
172
+ let tx = connection. transaction ( ) ?;
173
+
174
+ for tag in tags. split ( ',' ) {
175
+ tx. execute (
176
+ "DELETE FROM tagged WHERE tag = ?1;" ,
177
+ & [ & tag] ,
178
+ ) ?;
179
+ }
180
+
181
+ tx. commit ( ) ?;
182
+ }
135
183
_ => unreachable ! ( "Exhausted list of subcommands and subcommand_required prevents `None`" ) ,
136
184
} ;
137
185
0 commit comments