├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── Makefile.toml ├── README-demo.webp ├── README.md ├── src ├── lib.rs └── main.rs └── tests └── main.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /dist/ 3 | /.vscode/ 4 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | version = 3 4 | 5 | [[package]] 6 | name = "aho-corasick" 7 | version = "1.1.2" 8 | source = "registry+https://github.com/rust-lang/crates.io-index" 9 | checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" 10 | dependencies = [ 11 | "memchr", 12 | ] 13 | 14 | [[package]] 15 | name = "anstream" 16 | version = "0.6.4" 17 | source = "registry+https://github.com/rust-lang/crates.io-index" 18 | checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44" 19 | dependencies = [ 20 | "anstyle", 21 | "anstyle-parse", 22 | "anstyle-query", 23 | "anstyle-wincon", 24 | "colorchoice", 25 | "utf8parse", 26 | ] 27 | 28 | [[package]] 29 | name = "anstyle" 30 | version = "1.0.4" 31 | source = "registry+https://github.com/rust-lang/crates.io-index" 32 | checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" 33 | 34 | [[package]] 35 | name = "anstyle-parse" 36 | version = "0.2.2" 37 | source = "registry+https://github.com/rust-lang/crates.io-index" 38 | checksum = "317b9a89c1868f5ea6ff1d9539a69f45dffc21ce321ac1fd1160dfa48c8e2140" 39 | dependencies = [ 40 | "utf8parse", 41 | ] 42 | 43 | [[package]] 44 | name = "anstyle-query" 45 | version = "1.0.0" 46 | source = "registry+https://github.com/rust-lang/crates.io-index" 47 | checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" 48 | dependencies = [ 49 | "windows-sys", 50 | ] 51 | 52 | [[package]] 53 | name = "anstyle-wincon" 54 | version = "3.0.1" 55 | source = "registry+https://github.com/rust-lang/crates.io-index" 56 | checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628" 57 | dependencies = [ 58 | "anstyle", 59 | "windows-sys", 60 | ] 61 | 62 | [[package]] 63 | name = "anyhow" 64 | version = "1.0.75" 65 | source = "registry+https://github.com/rust-lang/crates.io-index" 66 | checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" 67 | 68 | [[package]] 69 | name = "autocfg" 70 | version = "1.1.0" 71 | source = "registry+https://github.com/rust-lang/crates.io-index" 72 | checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" 73 | 74 | [[package]] 75 | name = "bitflags" 76 | version = "1.3.2" 77 | source = "registry+https://github.com/rust-lang/crates.io-index" 78 | checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" 79 | 80 | [[package]] 81 | name = "bitflags" 82 | version = "2.4.1" 83 | source = "registry+https://github.com/rust-lang/crates.io-index" 84 | checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" 85 | 86 | [[package]] 87 | name = "cfg-if" 88 | version = "1.0.0" 89 | source = "registry+https://github.com/rust-lang/crates.io-index" 90 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 91 | 92 | [[package]] 93 | name = "clap" 94 | version = "4.4.7" 95 | source = "registry+https://github.com/rust-lang/crates.io-index" 96 | checksum = "ac495e00dcec98c83465d5ad66c5c4fabd652fd6686e7c6269b117e729a6f17b" 97 | dependencies = [ 98 | "clap_builder", 99 | "clap_derive", 100 | ] 101 | 102 | [[package]] 103 | name = "clap_builder" 104 | version = "4.4.7" 105 | source = "registry+https://github.com/rust-lang/crates.io-index" 106 | checksum = "c77ed9a32a62e6ca27175d00d29d05ca32e396ea1eb5fb01d8256b669cec7663" 107 | dependencies = [ 108 | "anstream", 109 | "anstyle", 110 | "clap_lex", 111 | "strsim", 112 | ] 113 | 114 | [[package]] 115 | name = "clap_derive" 116 | version = "4.4.7" 117 | source = "registry+https://github.com/rust-lang/crates.io-index" 118 | checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" 119 | dependencies = [ 120 | "heck", 121 | "proc-macro2", 122 | "quote", 123 | "syn", 124 | ] 125 | 126 | [[package]] 127 | name = "clap_lex" 128 | version = "0.6.0" 129 | source = "registry+https://github.com/rust-lang/crates.io-index" 130 | checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" 131 | 132 | [[package]] 133 | name = "colorchoice" 134 | version = "1.0.0" 135 | source = "registry+https://github.com/rust-lang/crates.io-index" 136 | checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" 137 | 138 | [[package]] 139 | name = "colored" 140 | version = "2.0.4" 141 | source = "registry+https://github.com/rust-lang/crates.io-index" 142 | checksum = "2674ec482fbc38012cf31e6c42ba0177b431a0cb6f15fe40efa5aab1bda516f6" 143 | dependencies = [ 144 | "is-terminal", 145 | "lazy_static", 146 | "windows-sys", 147 | ] 148 | 149 | [[package]] 150 | name = "dashmap" 151 | version = "5.5.3" 152 | source = "registry+https://github.com/rust-lang/crates.io-index" 153 | checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" 154 | dependencies = [ 155 | "cfg-if", 156 | "hashbrown", 157 | "lock_api", 158 | "once_cell", 159 | "parking_lot_core", 160 | ] 161 | 162 | [[package]] 163 | name = "edit" 164 | version = "0.1.4" 165 | source = "registry+https://github.com/rust-lang/crates.io-index" 166 | checksum = "c562aa71f7bc691fde4c6bf5f93ae5a5298b617c2eb44c76c87832299a17fbb4" 167 | dependencies = [ 168 | "tempfile", 169 | "which", 170 | ] 171 | 172 | [[package]] 173 | name = "either" 174 | version = "1.9.0" 175 | source = "registry+https://github.com/rust-lang/crates.io-index" 176 | checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" 177 | 178 | [[package]] 179 | name = "errno" 180 | version = "0.3.5" 181 | source = "registry+https://github.com/rust-lang/crates.io-index" 182 | checksum = "ac3e13f66a2f95e32a39eaa81f6b95d42878ca0e1db0c7543723dfe12557e860" 183 | dependencies = [ 184 | "libc", 185 | "windows-sys", 186 | ] 187 | 188 | [[package]] 189 | name = "fastrand" 190 | version = "2.0.1" 191 | source = "registry+https://github.com/rust-lang/crates.io-index" 192 | checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" 193 | 194 | [[package]] 195 | name = "fs_extra" 196 | version = "1.3.0" 197 | source = "registry+https://github.com/rust-lang/crates.io-index" 198 | checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c" 199 | 200 | [[package]] 201 | name = "futures" 202 | version = "0.3.29" 203 | source = "registry+https://github.com/rust-lang/crates.io-index" 204 | checksum = "da0290714b38af9b4a7b094b8a37086d1b4e61f2df9122c3cad2577669145335" 205 | dependencies = [ 206 | "futures-channel", 207 | "futures-core", 208 | "futures-executor", 209 | "futures-io", 210 | "futures-sink", 211 | "futures-task", 212 | "futures-util", 213 | ] 214 | 215 | [[package]] 216 | name = "futures-channel" 217 | version = "0.3.29" 218 | source = "registry+https://github.com/rust-lang/crates.io-index" 219 | checksum = "ff4dd66668b557604244583e3e1e1eada8c5c2e96a6d0d6653ede395b78bbacb" 220 | dependencies = [ 221 | "futures-core", 222 | "futures-sink", 223 | ] 224 | 225 | [[package]] 226 | name = "futures-core" 227 | version = "0.3.29" 228 | source = "registry+https://github.com/rust-lang/crates.io-index" 229 | checksum = "eb1d22c66e66d9d72e1758f0bd7d4fd0bee04cad842ee34587d68c07e45d088c" 230 | 231 | [[package]] 232 | name = "futures-executor" 233 | version = "0.3.29" 234 | source = "registry+https://github.com/rust-lang/crates.io-index" 235 | checksum = "0f4fb8693db0cf099eadcca0efe2a5a22e4550f98ed16aba6c48700da29597bc" 236 | dependencies = [ 237 | "futures-core", 238 | "futures-task", 239 | "futures-util", 240 | ] 241 | 242 | [[package]] 243 | name = "futures-io" 244 | version = "0.3.29" 245 | source = "registry+https://github.com/rust-lang/crates.io-index" 246 | checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa" 247 | 248 | [[package]] 249 | name = "futures-sink" 250 | version = "0.3.29" 251 | source = "registry+https://github.com/rust-lang/crates.io-index" 252 | checksum = "e36d3378ee38c2a36ad710c5d30c2911d752cb941c00c72dbabfb786a7970817" 253 | 254 | [[package]] 255 | name = "futures-task" 256 | version = "0.3.29" 257 | source = "registry+https://github.com/rust-lang/crates.io-index" 258 | checksum = "efd193069b0ddadc69c46389b740bbccdd97203899b48d09c5f7969591d6bae2" 259 | 260 | [[package]] 261 | name = "futures-util" 262 | version = "0.3.29" 263 | source = "registry+https://github.com/rust-lang/crates.io-index" 264 | checksum = "a19526d624e703a3179b3d322efec918b6246ea0fa51d41124525f00f1cc8104" 265 | dependencies = [ 266 | "futures-channel", 267 | "futures-core", 268 | "futures-io", 269 | "futures-sink", 270 | "futures-task", 271 | "memchr", 272 | "pin-project-lite", 273 | "pin-utils", 274 | "slab", 275 | ] 276 | 277 | [[package]] 278 | name = "glob" 279 | version = "0.3.1" 280 | source = "registry+https://github.com/rust-lang/crates.io-index" 281 | checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" 282 | 283 | [[package]] 284 | name = "hashbrown" 285 | version = "0.14.2" 286 | source = "registry+https://github.com/rust-lang/crates.io-index" 287 | checksum = "f93e7192158dbcda357bdec5fb5788eebf8bbac027f3f33e719d29135ae84156" 288 | 289 | [[package]] 290 | name = "heck" 291 | version = "0.4.1" 292 | source = "registry+https://github.com/rust-lang/crates.io-index" 293 | checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" 294 | 295 | [[package]] 296 | name = "hermit-abi" 297 | version = "0.3.3" 298 | source = "registry+https://github.com/rust-lang/crates.io-index" 299 | checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" 300 | 301 | [[package]] 302 | name = "home" 303 | version = "0.5.5" 304 | source = "registry+https://github.com/rust-lang/crates.io-index" 305 | checksum = "5444c27eef6923071f7ebcc33e3444508466a76f7a2b93da00ed6e19f30c1ddb" 306 | dependencies = [ 307 | "windows-sys", 308 | ] 309 | 310 | [[package]] 311 | name = "is-terminal" 312 | version = "0.4.9" 313 | source = "registry+https://github.com/rust-lang/crates.io-index" 314 | checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" 315 | dependencies = [ 316 | "hermit-abi", 317 | "rustix", 318 | "windows-sys", 319 | ] 320 | 321 | [[package]] 322 | name = "lazy_static" 323 | version = "1.4.0" 324 | source = "registry+https://github.com/rust-lang/crates.io-index" 325 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" 326 | 327 | [[package]] 328 | name = "libc" 329 | version = "0.2.149" 330 | source = "registry+https://github.com/rust-lang/crates.io-index" 331 | checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b" 332 | 333 | [[package]] 334 | name = "linux-raw-sys" 335 | version = "0.4.10" 336 | source = "registry+https://github.com/rust-lang/crates.io-index" 337 | checksum = "da2479e8c062e40bf0066ffa0bc823de0a9368974af99c9f6df941d2c231e03f" 338 | 339 | [[package]] 340 | name = "lock_api" 341 | version = "0.4.11" 342 | source = "registry+https://github.com/rust-lang/crates.io-index" 343 | checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45" 344 | dependencies = [ 345 | "autocfg", 346 | "scopeguard", 347 | ] 348 | 349 | [[package]] 350 | name = "log" 351 | version = "0.4.20" 352 | source = "registry+https://github.com/rust-lang/crates.io-index" 353 | checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" 354 | 355 | [[package]] 356 | name = "memchr" 357 | version = "2.6.4" 358 | source = "registry+https://github.com/rust-lang/crates.io-index" 359 | checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" 360 | 361 | [[package]] 362 | name = "moove" 363 | version = "0.2.8" 364 | dependencies = [ 365 | "anyhow", 366 | "clap", 367 | "colored", 368 | "edit", 369 | "fs_extra", 370 | "glob", 371 | "natord", 372 | "normpath", 373 | "regex", 374 | "serial_test", 375 | ] 376 | 377 | [[package]] 378 | name = "natord" 379 | version = "1.0.9" 380 | source = "registry+https://github.com/rust-lang/crates.io-index" 381 | checksum = "308d96db8debc727c3fd9744aac51751243420e46edf401010908da7f8d5e57c" 382 | 383 | [[package]] 384 | name = "normpath" 385 | version = "1.1.1" 386 | source = "registry+https://github.com/rust-lang/crates.io-index" 387 | checksum = "ec60c60a693226186f5d6edf073232bfb6464ed97eb22cf3b01c1e8198fd97f5" 388 | dependencies = [ 389 | "windows-sys", 390 | ] 391 | 392 | [[package]] 393 | name = "once_cell" 394 | version = "1.18.0" 395 | source = "registry+https://github.com/rust-lang/crates.io-index" 396 | checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" 397 | 398 | [[package]] 399 | name = "parking_lot" 400 | version = "0.12.1" 401 | source = "registry+https://github.com/rust-lang/crates.io-index" 402 | checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" 403 | dependencies = [ 404 | "lock_api", 405 | "parking_lot_core", 406 | ] 407 | 408 | [[package]] 409 | name = "parking_lot_core" 410 | version = "0.9.9" 411 | source = "registry+https://github.com/rust-lang/crates.io-index" 412 | checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e" 413 | dependencies = [ 414 | "cfg-if", 415 | "libc", 416 | "redox_syscall", 417 | "smallvec", 418 | "windows-targets", 419 | ] 420 | 421 | [[package]] 422 | name = "pin-project-lite" 423 | version = "0.2.13" 424 | source = "registry+https://github.com/rust-lang/crates.io-index" 425 | checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" 426 | 427 | [[package]] 428 | name = "pin-utils" 429 | version = "0.1.0" 430 | source = "registry+https://github.com/rust-lang/crates.io-index" 431 | checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" 432 | 433 | [[package]] 434 | name = "proc-macro2" 435 | version = "1.0.69" 436 | source = "registry+https://github.com/rust-lang/crates.io-index" 437 | checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" 438 | dependencies = [ 439 | "unicode-ident", 440 | ] 441 | 442 | [[package]] 443 | name = "quote" 444 | version = "1.0.33" 445 | source = "registry+https://github.com/rust-lang/crates.io-index" 446 | checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" 447 | dependencies = [ 448 | "proc-macro2", 449 | ] 450 | 451 | [[package]] 452 | name = "redox_syscall" 453 | version = "0.4.1" 454 | source = "registry+https://github.com/rust-lang/crates.io-index" 455 | checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" 456 | dependencies = [ 457 | "bitflags 1.3.2", 458 | ] 459 | 460 | [[package]] 461 | name = "regex" 462 | version = "1.10.2" 463 | source = "registry+https://github.com/rust-lang/crates.io-index" 464 | checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" 465 | dependencies = [ 466 | "aho-corasick", 467 | "memchr", 468 | "regex-automata", 469 | "regex-syntax", 470 | ] 471 | 472 | [[package]] 473 | name = "regex-automata" 474 | version = "0.4.3" 475 | source = "registry+https://github.com/rust-lang/crates.io-index" 476 | checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" 477 | dependencies = [ 478 | "aho-corasick", 479 | "memchr", 480 | "regex-syntax", 481 | ] 482 | 483 | [[package]] 484 | name = "regex-syntax" 485 | version = "0.8.2" 486 | source = "registry+https://github.com/rust-lang/crates.io-index" 487 | checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" 488 | 489 | [[package]] 490 | name = "rustix" 491 | version = "0.38.21" 492 | source = "registry+https://github.com/rust-lang/crates.io-index" 493 | checksum = "2b426b0506e5d50a7d8dafcf2e81471400deb602392c7dd110815afb4eaf02a3" 494 | dependencies = [ 495 | "bitflags 2.4.1", 496 | "errno", 497 | "libc", 498 | "linux-raw-sys", 499 | "windows-sys", 500 | ] 501 | 502 | [[package]] 503 | name = "scopeguard" 504 | version = "1.2.0" 505 | source = "registry+https://github.com/rust-lang/crates.io-index" 506 | checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" 507 | 508 | [[package]] 509 | name = "serial_test" 510 | version = "2.0.0" 511 | source = "registry+https://github.com/rust-lang/crates.io-index" 512 | checksum = "0e56dd856803e253c8f298af3f4d7eb0ae5e23a737252cd90bb4f3b435033b2d" 513 | dependencies = [ 514 | "dashmap", 515 | "futures", 516 | "lazy_static", 517 | "log", 518 | "parking_lot", 519 | "serial_test_derive", 520 | ] 521 | 522 | [[package]] 523 | name = "serial_test_derive" 524 | version = "2.0.0" 525 | source = "registry+https://github.com/rust-lang/crates.io-index" 526 | checksum = "91d129178576168c589c9ec973feedf7d3126c01ac2bf08795109aa35b69fb8f" 527 | dependencies = [ 528 | "proc-macro2", 529 | "quote", 530 | "syn", 531 | ] 532 | 533 | [[package]] 534 | name = "slab" 535 | version = "0.4.9" 536 | source = "registry+https://github.com/rust-lang/crates.io-index" 537 | checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" 538 | dependencies = [ 539 | "autocfg", 540 | ] 541 | 542 | [[package]] 543 | name = "smallvec" 544 | version = "1.11.1" 545 | source = "registry+https://github.com/rust-lang/crates.io-index" 546 | checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a" 547 | 548 | [[package]] 549 | name = "strsim" 550 | version = "0.10.0" 551 | source = "registry+https://github.com/rust-lang/crates.io-index" 552 | checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" 553 | 554 | [[package]] 555 | name = "syn" 556 | version = "2.0.38" 557 | source = "registry+https://github.com/rust-lang/crates.io-index" 558 | checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b" 559 | dependencies = [ 560 | "proc-macro2", 561 | "quote", 562 | "unicode-ident", 563 | ] 564 | 565 | [[package]] 566 | name = "tempfile" 567 | version = "3.8.1" 568 | source = "registry+https://github.com/rust-lang/crates.io-index" 569 | checksum = "7ef1adac450ad7f4b3c28589471ade84f25f731a7a0fe30d71dfa9f60fd808e5" 570 | dependencies = [ 571 | "cfg-if", 572 | "fastrand", 573 | "redox_syscall", 574 | "rustix", 575 | "windows-sys", 576 | ] 577 | 578 | [[package]] 579 | name = "unicode-ident" 580 | version = "1.0.12" 581 | source = "registry+https://github.com/rust-lang/crates.io-index" 582 | checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" 583 | 584 | [[package]] 585 | name = "utf8parse" 586 | version = "0.2.1" 587 | source = "registry+https://github.com/rust-lang/crates.io-index" 588 | checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" 589 | 590 | [[package]] 591 | name = "which" 592 | version = "4.4.2" 593 | source = "registry+https://github.com/rust-lang/crates.io-index" 594 | checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7" 595 | dependencies = [ 596 | "either", 597 | "home", 598 | "once_cell", 599 | "rustix", 600 | ] 601 | 602 | [[package]] 603 | name = "windows-sys" 604 | version = "0.48.0" 605 | source = "registry+https://github.com/rust-lang/crates.io-index" 606 | checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" 607 | dependencies = [ 608 | "windows-targets", 609 | ] 610 | 611 | [[package]] 612 | name = "windows-targets" 613 | version = "0.48.5" 614 | source = "registry+https://github.com/rust-lang/crates.io-index" 615 | checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" 616 | dependencies = [ 617 | "windows_aarch64_gnullvm", 618 | "windows_aarch64_msvc", 619 | "windows_i686_gnu", 620 | "windows_i686_msvc", 621 | "windows_x86_64_gnu", 622 | "windows_x86_64_gnullvm", 623 | "windows_x86_64_msvc", 624 | ] 625 | 626 | [[package]] 627 | name = "windows_aarch64_gnullvm" 628 | version = "0.48.5" 629 | source = "registry+https://github.com/rust-lang/crates.io-index" 630 | checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" 631 | 632 | [[package]] 633 | name = "windows_aarch64_msvc" 634 | version = "0.48.5" 635 | source = "registry+https://github.com/rust-lang/crates.io-index" 636 | checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" 637 | 638 | [[package]] 639 | name = "windows_i686_gnu" 640 | version = "0.48.5" 641 | source = "registry+https://github.com/rust-lang/crates.io-index" 642 | checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" 643 | 644 | [[package]] 645 | name = "windows_i686_msvc" 646 | version = "0.48.5" 647 | source = "registry+https://github.com/rust-lang/crates.io-index" 648 | checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" 649 | 650 | [[package]] 651 | name = "windows_x86_64_gnu" 652 | version = "0.48.5" 653 | source = "registry+https://github.com/rust-lang/crates.io-index" 654 | checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" 655 | 656 | [[package]] 657 | name = "windows_x86_64_gnullvm" 658 | version = "0.48.5" 659 | source = "registry+https://github.com/rust-lang/crates.io-index" 660 | checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" 661 | 662 | [[package]] 663 | name = "windows_x86_64_msvc" 664 | version = "0.48.5" 665 | source = "registry+https://github.com/rust-lang/crates.io-index" 666 | checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" 667 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "moove" 3 | version = "0.2.8" 4 | license = "MIT/Apache-2.0" 5 | edition = "2021" 6 | rust-version = "1.71" 7 | description = "🚚 Manipulate file names and locations" 8 | keywords = ["mv", "rename", "move"] 9 | categories = ["command-line-utilities", "filesystem"] 10 | readme = "README.md" 11 | homepage = "https://github.com/urin/moove" 12 | repository = "https://github.com/urin/moove" 13 | documentation = "https://github.com/urin/moove" 14 | 15 | [dependencies] 16 | anyhow = "1.0.75" 17 | clap = { version = "4.4.7", features = ["derive"] } 18 | colored = "2.0.4" 19 | edit = "0.1.4" 20 | fs_extra = "1.3.0" 21 | glob = "0.3.1" 22 | natord = "1.0.9" 23 | normpath = "1.1.1" 24 | regex = "1.10.2" 25 | 26 | [dev-dependencies] 27 | serial_test = "2.0.0" 28 | 29 | [lib] 30 | doctest = false 31 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2023 Hayato Takenaka 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Hayato Takenaka 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile.toml: -------------------------------------------------------------------------------- 1 | [tasks.default] 2 | alias = "build-all" 3 | 4 | [tasks.build-all] 5 | dependencies = ["doc", "build", "build-platforms"] 6 | run_task = "dist" 7 | 8 | [tasks.doc] 9 | command = "cargo" 10 | args = ["doc", "--bins", "--no-deps"] 11 | 12 | [tasks.build] 13 | command = "cargo" 14 | args = ["build", "--release"] 15 | 16 | [tasks.build-platforms] 17 | command = "cargo" 18 | args = [ 19 | "zigbuild", "--release", 20 | "--target", "aarch64-apple-darwin", 21 | "--target", "aarch64-unknown-linux-musl", 22 | "--target", "x86_64-apple-darwin", 23 | "--target", "x86_64-pc-windows-gnu", 24 | "--target", "x86_64-unknown-linux-musl", 25 | ] 26 | 27 | [tasks.build-linux] 28 | command = "cargo" 29 | args = [ 30 | "zigbuild", "--release", 31 | "--target", "aarch64-unknown-linux-musl", 32 | "--target", "x86_64-unknown-linux-musl", 33 | ] 34 | 35 | [tasks.build-apple] 36 | command = "cargo" 37 | args = [ 38 | "zigbuild", "--release", 39 | "--target", "aarch64-apple-darwin", 40 | "--target", "x86_64-apple-darwin", 41 | ] 42 | 43 | [tasks.build-windows] 44 | command = "cargo" 45 | args = [ 46 | "zigbuild", "--release", 47 | "--target", "x86_64-pc-windows-gnu", 48 | ] 49 | 50 | [tasks.dist] 51 | script_runner = "@shell" 52 | script = ''' 53 | tar caf dist/moove-apple-aarch64.tar.gz -C target/aarch64-apple-darwin/release moove 54 | tar caf dist/moove-linux-aarch64.tar.gz -C target/aarch64-unknown-linux-musl/release moove 55 | tar caf dist/moove-apple-x86_64.tar.gz -C target/x86_64-apple-darwin/release moove 56 | tar caf dist/moove-linux-x86_64.tar.gz -C target/x86_64-unknown-linux-musl/release moove 57 | tar caf dist/moove-windows-x86_64.tar.gz -C target/x86_64-pc-windows-gnu/release moove.exe 58 | ''' 59 | 60 | [tasks.setup] 61 | command = "rustup" 62 | args = [ 63 | "target", "add", 64 | "aarch64-apple-darwin", 65 | "aarch64-unknown-linux-musl", 66 | "x86_64-apple-darwin", 67 | "x86_64-pc-windows-gnu", 68 | "x86_64-unknown-linux-musl" 69 | ] 70 | 71 | [tasks.test] 72 | command = "cargo" 73 | args = ["test"] 74 | dependencies = ["clippy"] 75 | 76 | [tasks.clippy] 77 | command = "cargo" 78 | args = ["clippy"] 79 | 80 | [tasks.update] 81 | dependencies = ["cargo-update", "cargo-upgrade"] 82 | 83 | [tasks.cargo-update] 84 | command = "cargo" 85 | args = ["update"] 86 | 87 | [tasks.cargo-upgrade] 88 | command = "cargo" 89 | args = ["upgrade"] 90 | 91 | [tasks.release] 92 | command = "cargo" 93 | args = ["release"] 94 | 95 | -------------------------------------------------------------------------------- /README-demo.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/urin/moove/3f9f5774d798bf7962d3a7a5b4b6caef9afe9245/README-demo.webp -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # moove - 🚚 Manipulate file names and locations 2 | 3 | [![Version][image-version]][url-version] 4 | [![Downloads from crates.io][image-downloads-crates]][url-crates.io] 5 | [![Downloads from GitHub][image-downloads-github]][url-latest-release] 6 | [![License][image-license]](#license-) 7 | 8 | *moove* is a command line tool for renaming and moving files and directories using a text editor. 9 | 10 | [🎨 Features](#features-) \| 11 | [🚀 Usage](#usage-) \| 12 | [📥 Getting started](#getting-started-) \| 13 | [💙 Contributing](#contributing-) \| 14 | [🌏 License](#license-) 15 | 16 | ![Demo](https://raw.githubusercontent.com/urin/moove/main/README-demo.webp) 17 | 18 | ## Features 🎨 19 | 20 | - Displays file and directory names like [`ls`](https://man7.org/linux/man-pages/man1/ls.1.html) in a text editor, 21 | and renames or moves them exactly as you edit them. 22 | - A pre-compiled single executable without any dependencies. 23 | Thanks 💖 to [Zig](https://ziglang.org/) and [musl libc](https://musl.libc.org/). 24 | - Supports Linux, Mac and Windows. 25 | - Supports wildcard patterns, including Windows. 26 | 27 | ### Caveats ⚠ 28 | 29 | - Given paths have to be convertible to UTF-8. 30 | - Collisions are detected *as much as possible*, but *not perfectly*. 31 | Does not verify all paths such as hard links and symbolic links. 32 | 33 | ## Usage 🚀 34 | 35 | ```txt 36 | Usage: moove [OPTIONS] [PATHS]... 37 | 38 | Arguments: 39 | [PATHS]... Paths or wildcard patterns to move 40 | 41 | Options: 42 | -v, --verbose Verbose output 43 | -s, --sort Sort in the natural order 44 | -a, --absolute Treat as absolute paths 45 | -d, --directory Directories themselves, not their contents 46 | -w, --with-hidden Include hidden files 47 | -e, --exclude-pattern Exclude regular expression pattern 48 | -c, --copy Copy without moving 49 | -u, --dry-run Dry-run 50 | -o, --oops Abort in case of collision (prompt as default) 51 | -q, --quiet No output to stdout/strerr even if error 52 | -h, --help Print help 53 | -V, --version Print version 54 | ``` 55 | 56 | - Displays file and directory names like [`ls`](https://man7.org/linux/man-pages/man1/ls.1.html) in a text editor. 57 | - You can edit the list as you want to operate. The order of lines after editing corresponds to the original one. Empty lines will be ignored. 58 | - Operations are canceled if you close the editor without saving. 59 | - If a line starts with `//`, the file and directory (and its contents) will be removed regardless of modification of the remaining part of the line. 60 | - Destination directories will be created automatically. 61 | - In case of line number change or collision, asks whether to re-edit or abort. Aborts without asking if `--oops` is specified. 62 | 63 | ### Configuration 🎚 64 | 65 | - Default command line options can be specified by the environment variable `MOOVE_OPTIONS`. 66 | - The default editor is searched in the following order. 67 | - environment variable `VISUAL` 68 | - environment variable `EDITOR` 69 | - hardcoded lists 70 | - platform-specific generic file openers 71 | 72 | ## Getting Started 📥 73 | 74 | ### Pre-compiled binaries 75 | 76 | - [moove-apple-aarch64.tar.gz](https://github.com/urin/moove/releases/latest/download/moove-apple-aarch64.tar.gz) 77 | - [moove-apple-x86_64.tar.gz](https://github.com/urin/moove/releases/latest/download/moove-apple-x86_64.tar.gz) 78 | - [moove-linux-aarch64.tar.gz](https://github.com/urin/moove/releases/latest/download/moove-linux-aarch64.tar.gz) 79 | - [moove-linux-x86_64.tar.gz](https://github.com/urin/moove/releases/latest/download/moove-linux-x86_64.tar.gz) 80 | - [moove-windows-x86_64.tar.gz](https://github.com/urin/moove/releases/latest/download/moove-windows-x86_64.tar.gz) 81 | 82 | ### Install by cargo 83 | 84 | ```sh 85 | cargo install moove 86 | ``` 87 | 88 | ## Alternatives 89 | 90 | - [laurent22/massren](https://github.com/laurent22/massren) 91 | - [itchyny/mmv](https://github.com/itchyny/mmv) 92 | 93 | ## Contributing 💙 94 | 95 | Followings are used to build. 96 | 97 | - [cargo-make](https://crates.io/crates/cargo-make/) as the task runner 98 | - [cargo-zigbuild](https://crates.io/crates/cargo-zigbuild) to build for multiple platforms 99 | 100 | ### Setup development environment 🪜 101 | 102 | 1. Install [Zig](https://ziglang.org/) according to [the Zig document](https://ziglang.org/learn/getting-started/#installing-zig). 103 | 2. Run following commands. 104 | ```sh 105 | cargo install cargo-make 106 | cargo make setup 107 | ``` 108 | 109 | ### Testing and Building 🔨 110 | 111 | - To test, 112 | ```txt 113 | cargo make test 114 | ``` 115 | 116 | - To build binaries for release, 117 | ```txt 118 | cargo make 119 | ``` 120 | Pre-compiled binaries will be in the directory `dist`. 121 | 122 | ⚠ Binaries do not have execute permission in case of building on windows. 123 | 124 | ## TODOs ✅ 125 | 126 | - Package for various platforms 127 | - Overwrite option 128 | - Exclude .gitignore option 129 | - Move to trash instead of removing as the default 130 | - Logging 131 | - Recursive option 132 | - Maximum depth option 133 | - Depth option 134 | 135 | ## License 🌏 136 | 137 | Licensed under either of 138 | 139 | - [Apache License, Version 2.0][url-license-apache] or 140 | [https://www.apache.org/licenses/LICENSE-2.0](https://www.apache.org/licenses/LICENSE-2.0) 141 | - [MIT license][url-license-mit] or 142 | [https://opensource.org/licenses/MIT](https://opensource.org/licenses/MIT) 143 | 144 | at your option. 145 | 146 | Unless you explicitly state otherwise, any contribution intentionally submitted 147 | for inclusion in the work by you, as defined in the Apache-2.0 license, shall be 148 | dual licensed as above, without any additional terms or conditions. 149 | 150 | © 2023 [Urin](https://github.com/urin) 151 | 152 | 153 | 154 | [image-license]: https://img.shields.io/badge/license-MIT%2FApache--2.0-lightgrey?style=flat 155 | [image-downloads-crates]: https://img.shields.io/crates/d/moove?label=downloads&style=flat 156 | [image-downloads-github]: https://img.shields.io/github/downloads/urin/moove/total?label=from%20GitHub&style=flat 157 | [image-version]: https://img.shields.io/crates/v/moove.svg?style=flat 158 | 159 | [url-license-mit]: https://github.com/urin/moove/blob/main/LICENSE-MIT 160 | [url-license-apache]: https://github.com/urin/moove/blob/main/LICENSE-APACHE 161 | [url-latest-release]: https://github.com/urin/moove/releases/latest 162 | [url-releases]: https://github.com/urin/moove/releases 163 | [url-version]: https://crates.io/crates/moove/versions 164 | [url-crates.io]: https://crates.io/crates/moove 165 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | use std::fs::Metadata; 2 | use std::io::Write; 3 | use std::path::{Path, PathBuf}; 4 | 5 | use anyhow::{Context, Result}; 6 | use clap::Parser; 7 | use colored::*; 8 | use fs_extra::dir::CopyOptions; 9 | use normpath::PathExt; 10 | use regex::Regex; 11 | 12 | #[derive(Debug, Parser, Default)] 13 | #[command(version)] 14 | pub struct CommandLine { 15 | /// Paths or wildcard patterns to move 16 | #[arg(value_hint = clap::ValueHint::AnyPath)] 17 | pub paths: Vec, 18 | /// Verbose output 19 | #[arg(short, long)] 20 | pub verbose: bool, 21 | /// Sort in natural order 22 | #[arg(short, long)] 23 | pub sort: bool, 24 | /// Treat as absolute paths 25 | #[arg(short, long)] 26 | pub absolute: bool, 27 | /// Directories themselves, not their contents 28 | #[arg(short, long)] 29 | pub directory: bool, 30 | /// Include hidden files 31 | #[arg(short, long)] 32 | pub with_hidden: bool, 33 | /// Exclude regular expression pattern 34 | #[arg(short, long, value_name = "PATTERN")] 35 | pub exclude_pattern: Option, 36 | /// Copy without moving 37 | #[arg(short, long)] 38 | pub copy: bool, 39 | /// Dry-run 40 | #[arg(short = 'u', long)] 41 | pub dry_run: bool, 42 | /// Abort in case of collision (prompt as default) 43 | #[arg(short, long)] 44 | pub oops: bool, 45 | /// No output to stdout/strerr even if error 46 | #[arg(short, long)] 47 | pub quiet: bool, 48 | } 49 | 50 | #[derive(Debug)] 51 | pub struct Operation { 52 | pub kind: OperationKind, 53 | pub src: Source, 54 | pub dst: Destination, 55 | } 56 | 57 | #[derive(Debug)] 58 | pub enum OperationKind { 59 | Move, 60 | Copy, 61 | Remove, 62 | } 63 | 64 | #[derive(Debug, Clone)] 65 | pub struct Source { 66 | pub text: String, 67 | pub path: PathBuf, 68 | pub abs: PathBuf, 69 | pub meta: Metadata, 70 | } 71 | 72 | #[derive(Debug, Clone)] 73 | pub struct Destination { 74 | pub text: String, 75 | pub path: PathBuf, 76 | } 77 | 78 | static SEPARATORS: &[char] = &['/', '\\']; 79 | 80 | trait PathUtilExt { 81 | /// NOTE Can be replaced with `std::path::absolute` in the future. 82 | fn absolute(&self) -> Result; 83 | fn is_hidden(&self) -> Result; 84 | fn is_identical(&self, other: &Path) -> bool; 85 | } 86 | 87 | impl PathUtilExt for Path { 88 | fn is_identical(&self, other: &Path) -> bool { 89 | if cfg!(target_family = "windows") { 90 | self == other 91 | || self.as_os_str().to_ascii_lowercase() == other.as_os_str().to_ascii_lowercase() 92 | } else { 93 | self == other 94 | } 95 | } 96 | 97 | #[cfg(target_family = "windows")] 98 | fn absolute(&self) -> Result { 99 | self.normalize_virtually().with_context(|| { 100 | format!( 101 | "Failed to normalize path. {}", 102 | self.to_string_lossy().yellow().underline() 103 | ) 104 | }) 105 | } 106 | 107 | #[cfg(target_family = "unix")] 108 | fn absolute(&self) -> Result { 109 | self.normalize().with_context(|| { 110 | format!( 111 | "Failed to normalize path. {}", 112 | self.to_string_lossy().yellow().underline() 113 | ) 114 | }) 115 | } 116 | 117 | #[cfg(target_family = "windows")] 118 | fn is_hidden(&self) -> Result { 119 | use std::os::windows::prelude::*; 120 | let metadata = std::fs::metadata(self).with_context(|| { 121 | format!( 122 | "Failed to read metadata of {}", 123 | self.to_string_lossy().yellow().underline() 124 | ) 125 | })?; 126 | Ok((metadata.file_attributes() & 0x2) > 0) 127 | } 128 | 129 | #[cfg(target_family = "unix")] 130 | fn is_hidden(&self) -> Result { 131 | Ok(self 132 | .file_name() 133 | .with_context(|| { 134 | format!( 135 | "Failed to get file name {}", 136 | self.to_string_lossy().yellow().underline() 137 | ) 138 | })? 139 | .to_string_lossy() 140 | .starts_with('.')) 141 | } 142 | } 143 | 144 | pub fn try_main(args: &CommandLine) -> Result { 145 | let sources = &sources_from(args)?; 146 | let operations = &operations_from(sources, args)?; 147 | let mut processed = 0; 148 | for o in operations.iter() { 149 | execute_operation(o, args)?; 150 | if args.dry_run { 151 | continue; 152 | } 153 | processed += 1; 154 | } 155 | Ok(processed) 156 | } 157 | 158 | pub fn sources_from(args: &CommandLine) -> Result> { 159 | let mut sources: Vec = Vec::new(); 160 | let paths = list_files(&args.paths)?; 161 | for p in paths.iter().map(|p| p.trim_end_matches(SEPARATORS)) { 162 | let path = &PathBuf::from(if cfg!(target_family = "windows") { 163 | p.replace('/', "\\") 164 | } else { 165 | p.to_string() 166 | }); 167 | let stat = &path.symlink_metadata().with_context(|| { 168 | format!( 169 | "Failed to access {}", 170 | path.to_string_lossy().yellow().underline() 171 | ) 172 | })?; 173 | if stat.is_file() || stat.is_symlink() || args.directory { 174 | put_source(&mut sources, path, args)?; 175 | } else { 176 | let mut children = Vec::new(); 177 | for entry in std::fs::read_dir(path).with_context(|| { 178 | format!( 179 | "Failed to list files of directory. {}", 180 | path.to_string_lossy().yellow().underline() 181 | ) 182 | })? { 183 | children.push(entry?.path()); 184 | } 185 | children.sort_unstable_by(|a, b| { 186 | natord::compare(&a.to_string_lossy(), &b.to_string_lossy()) 187 | }); 188 | for child in children { 189 | put_source(&mut sources, &child, args)?; 190 | } 191 | if sources.is_empty() { 192 | anyhow::bail!( 193 | "Directory is empty. {}\n\ 194 | Use --directory for the directory itself.", 195 | path.to_string_lossy().yellow().underline() 196 | ); 197 | } 198 | } 199 | } 200 | if args.sort { 201 | sources.sort_unstable_by(|a, b| natord::compare(&a.text, &b.text)); 202 | } 203 | Ok(sources) 204 | } 205 | 206 | pub fn list_files(args: &[String]) -> Result> { 207 | use glob::glob; 208 | let mut paths = Vec::new(); 209 | for arg in args.iter() { 210 | let mut globbed = Vec::new(); 211 | for path in 212 | glob(arg).with_context(|| format!("Invalid pattern {}", arg.yellow().underline()))? 213 | { 214 | globbed 215 | .push(path.with_context(|| format!("Failed to glob {}", arg.yellow().underline()))?) 216 | } 217 | if globbed.is_empty() { 218 | anyhow::bail!("Failed to access {}", arg); 219 | } 220 | globbed.sort_unstable(); 221 | paths.append( 222 | &mut globbed 223 | .iter() 224 | .map(|g| g.to_string_lossy().to_string()) 225 | .collect(), 226 | ); 227 | } 228 | Ok(paths) 229 | } 230 | 231 | pub fn put_source(sources: &mut Vec, path: &Path, args: &CommandLine) -> Result<()> { 232 | let abs = path.absolute()?; 233 | let abs = abs.as_path(); 234 | if abs.parent().is_none() { 235 | anyhow::bail!( 236 | "Source should not be the root directory. {}", 237 | path.to_string_lossy().yellow().underline() 238 | ); 239 | } 240 | if !args.with_hidden && abs.is_hidden()? { 241 | return Ok(()); 242 | } 243 | let new_path = if args.absolute { abs } else { path }; 244 | let new_path_text = new_path 245 | .to_str() 246 | .with_context(|| { 247 | format!( 248 | "Failed to convert path to UTF-8. {}", 249 | path.to_string_lossy().to_string().yellow().underline() 250 | ) 251 | })? 252 | .trim_end_matches(SEPARATORS) 253 | .to_string(); 254 | if let Some(pattern) = &args.exclude_pattern { 255 | if pattern.is_match(&new_path_text) { 256 | return Ok(()); 257 | } 258 | } 259 | let new_src = Source { 260 | text: new_path_text, 261 | path: new_path.to_path_buf(), 262 | abs: abs.to_path_buf(), 263 | meta: new_path.symlink_metadata().with_context(|| { 264 | format!( 265 | "Failed to access {}", 266 | new_path.to_string_lossy().yellow().underline() 267 | ) 268 | })?, 269 | }; 270 | for src in sources.iter() { 271 | if src.abs.is_identical(&new_src.abs) { 272 | anyhow::bail!( 273 | "Duplicated source. {}", 274 | new_src.abs.to_string_lossy().yellow().underline() 275 | ); 276 | } 277 | } 278 | sources.push(new_src); 279 | Ok(()) 280 | } 281 | 282 | pub fn operations_from(sources: &Vec, args: &CommandLine) -> Result> { 283 | let mut operations = Vec::new(); 284 | let mut text = sources 285 | .iter() 286 | .map(|src| { 287 | let mut line = src.text.to_owned(); 288 | if src.path.is_dir() 289 | && !src.path.is_symlink() 290 | && !line.ends_with(std::path::MAIN_SEPARATOR) 291 | { 292 | line.push(std::path::MAIN_SEPARATOR); 293 | } 294 | line 295 | }) 296 | .collect::>() 297 | .join("\n"); 298 | 'redo: loop { 299 | text = edit::edit(&text)?; 300 | let lines = text 301 | .split('\n') 302 | .filter_map(|line| { 303 | let line = line.trim(); 304 | if line.is_empty() { 305 | return None; 306 | } 307 | Some(line.trim_end_matches(SEPARATORS)) 308 | }) 309 | .collect::>(); 310 | if lines.len() != sources.len() { 311 | let message = format!( 312 | "Number of lines {} does not match the original one {}", 313 | lines.len().to_string().yellow(), 314 | sources.len().to_string().yellow() 315 | ); 316 | if !args.oops { 317 | println!("{}", message); 318 | if prompt_redo()? { 319 | continue 'redo; 320 | } 321 | break 'redo; 322 | } 323 | anyhow::bail!(message); 324 | } 325 | for (src, line) in sources.iter().zip(lines.iter()) { 326 | let line = line.to_owned(); 327 | let (kind, line) = if line.starts_with("//") { 328 | (OperationKind::Remove, src.text.as_str()) 329 | } else if args.copy { 330 | (OperationKind::Copy, line) 331 | } else { 332 | (OperationKind::Move, line) 333 | }; 334 | let line = if cfg!(target_family = "windows") { 335 | line.replace('/', "\\") 336 | } else { 337 | line.to_string() 338 | }; 339 | let dst_path = PathBuf::from(&line); 340 | let removing = matches!(kind, OperationKind::Remove); 341 | if !removing && (dst_path == src.path || dst_path == src.abs) { 342 | continue; 343 | } 344 | let new_operation = Operation { 345 | kind, 346 | src: src.to_owned(), 347 | dst: Destination { 348 | text: line.to_owned(), 349 | path: dst_path.to_owned(), 350 | }, 351 | }; 352 | if !removing { 353 | if let Err(message) = is_operational(&operations, &new_operation) { 354 | if !args.oops { 355 | println!("{}", message); 356 | if prompt_redo()? { 357 | continue 'redo; 358 | } 359 | break 'redo; 360 | } 361 | anyhow::bail!(message); 362 | } 363 | } 364 | operations.push(new_operation); 365 | } 366 | break; 367 | } 368 | Ok(operations) 369 | } 370 | 371 | pub fn prompt_redo() -> Result { 372 | loop { 373 | print!( 374 | "{}{} or {}{}? > ", 375 | "E".bold().underline(), 376 | "dit".bold(), 377 | "A".bold().underline(), 378 | "bort".bold() 379 | ); 380 | std::io::stdout().flush()?; 381 | let mut ans = String::new(); 382 | std::io::stdin().read_line(&mut ans)?; 383 | let ans = ans.trim().to_ascii_lowercase(); 384 | if Regex::new(r"^a(bort)?$")?.is_match(&ans) { 385 | return Ok(false); 386 | } 387 | if ans.is_empty() || Regex::new(r"^e(dit)?$")?.is_match(&ans) { 388 | return Ok(true); 389 | } 390 | } 391 | } 392 | 393 | pub fn is_operational(operations: &[Operation], new_operation: &Operation) -> Result<()> { 394 | let src = &new_operation.src; 395 | let dst = &new_operation.dst; 396 | if dst.text.ends_with(std::path::MAIN_SEPARATOR) 397 | && (src.meta.is_file() || src.meta.is_symlink()) 398 | { 399 | anyhow::bail!( 400 | "Missing file name. {} for {}", 401 | dst.text.yellow().underline(), 402 | src.text.underline() 403 | ) 404 | } 405 | if operations 406 | .iter() 407 | .any(|o| o.dst.path.is_identical(&dst.path)) 408 | { 409 | anyhow::bail!("Duplicated destination. {}", dst.text.yellow().underline()); 410 | } 411 | if operations 412 | .iter() 413 | .any(|o| o.dst.path.ancestors().any(|a| a.is_identical(&dst.path))) 414 | { 415 | anyhow::bail!( 416 | "Destination should not be included in other destination. {}", 417 | dst.text.yellow().underline() 418 | ); 419 | } 420 | if dst.path.exists() { 421 | anyhow::bail!("Destination exists. {}", dst.text.yellow().underline()) 422 | } 423 | if dst.path.ancestors().any(|a| { 424 | if !a.exists() { 425 | false 426 | } else if a.is_file() { 427 | true 428 | } else if a.is_symlink() { 429 | if let Ok(p) = a.read_link() { 430 | p.is_file() 431 | } else { 432 | false 433 | } 434 | } else { 435 | false 436 | } 437 | }) { 438 | anyhow::bail!( 439 | "Ancestor of destination should not be a file.\n\ 440 | Destination: {}", 441 | dst.text.yellow().underline() 442 | ); 443 | } 444 | Ok(()) 445 | } 446 | 447 | pub fn execute_operation(o: &Operation, args: &CommandLine) -> Result<()> { 448 | match o.kind { 449 | OperationKind::Move => { 450 | if !args.quiet && (args.verbose || args.dry_run) { 451 | println!( 452 | "{} {}{}{}", 453 | "Move".dimmed(), 454 | o.src.text.dimmed().underline(), 455 | " → ".dimmed(), 456 | o.dst.text.dimmed().underline() 457 | ); 458 | } 459 | if args.dry_run { 460 | return Ok(()); 461 | } 462 | execute_move_or_copy(o, args)?; 463 | if !args.quiet { 464 | println!( 465 | "{} → {}", 466 | o.src.text.green().underline(), 467 | o.dst.text.green().underline() 468 | ); 469 | } 470 | } 471 | OperationKind::Copy => { 472 | if !args.quiet && (args.verbose || args.dry_run) { 473 | println!( 474 | "{} {}{}{}", 475 | "Copy".dimmed(), 476 | o.src.text.dimmed().underline(), 477 | " → ".dimmed(), 478 | o.dst.text.dimmed().underline() 479 | ); 480 | } 481 | if args.dry_run { 482 | return Ok(()); 483 | } 484 | execute_move_or_copy(o, args)?; 485 | if !args.quiet { 486 | println!( 487 | "{} → {}", 488 | o.src.text.green().underline(), 489 | o.dst.text.green().underline() 490 | ); 491 | } 492 | } 493 | OperationKind::Remove => { 494 | if !args.quiet && (args.verbose || args.dry_run) { 495 | println!("{} {}", "Remove".dimmed(), o.src.text.dimmed().underline()); 496 | } 497 | if args.dry_run { 498 | return Ok(()); 499 | } 500 | execute_remove(o, args)?; 501 | if !args.quiet { 502 | println!("Removed {}", o.src.text.green().underline()); 503 | } 504 | } 505 | }; 506 | Ok(()) 507 | } 508 | 509 | pub fn execute_move_or_copy(operation: &Operation, args: &CommandLine) -> Result<()> { 510 | let Operation { kind, src, dst, .. } = operation; 511 | let moving = matches!(kind, OperationKind::Move); 512 | let dst_parent = create_dir(dst, args)?; 513 | if should_relocate(&src.path, &dst_parent) { 514 | if !args.quiet && args.verbose { 515 | println!( 516 | "{} {} {}", 517 | if moving { "Moving" } else { "Copying" }.dimmed(), 518 | src.abs.to_string_lossy().dimmed().underline(), 519 | dst_parent.to_string_lossy().dimmed().underline() 520 | ); 521 | } 522 | if moving { 523 | fs_extra::move_items(&[&src.path], &dst_parent, &CopyOptions::default()).with_context( 524 | || { 525 | format!( 526 | "Failed to move {} to {}", 527 | src.text.yellow().underline(), 528 | dst_parent.to_string_lossy().yellow().underline() 529 | ) 530 | }, 531 | )?; 532 | } else { 533 | fs_extra::copy_items(&[&src.path], &dst_parent, &CopyOptions::default()).with_context( 534 | || { 535 | format!( 536 | "Failed to move {} to {}", 537 | src.text.yellow().underline(), 538 | dst_parent.to_string_lossy().yellow().underline() 539 | ) 540 | }, 541 | )?; 542 | } 543 | } 544 | // Rename if its file name need to be changed. 545 | // NOTE Can be unwrapped safely, `src` and `dst` cannot be root nor `..`. 546 | let src_basename = src.path.file_name().unwrap(); 547 | let dst_basename = dst.path.file_name().unwrap(); 548 | if src_basename != dst_basename { 549 | let from = &dst_parent.join(src_basename); 550 | let to = &dst_parent.join(dst_basename); 551 | if !args.quiet && args.verbose { 552 | println!( 553 | "{} {}{}{}", 554 | "Renaming".dimmed(), 555 | from.to_string_lossy().dimmed().underline(), 556 | " → ".dimmed(), 557 | to.to_string_lossy().dimmed().underline() 558 | ); 559 | } 560 | // Destination is never over-written, ensured when the operation was made. 561 | std::fs::rename(from, to).with_context(|| { 562 | format!( 563 | "Failed to rename {} to {}", 564 | from.to_string_lossy().yellow().underline(), 565 | to.to_string_lossy().yellow().underline() 566 | ) 567 | })?; 568 | } 569 | Ok(()) 570 | } 571 | 572 | /// Create parent directory if missing. 573 | pub fn create_dir(dst: &Destination, args: &CommandLine) -> Result { 574 | let current_dir = std::env::current_dir().context("Failed to get current directory.")?; 575 | let dst_parent = if dst.text.contains(std::path::MAIN_SEPARATOR) { 576 | dst.path.parent().unwrap() 577 | } else { 578 | ¤t_dir 579 | }; 580 | if !dst_parent.exists() { 581 | if !args.quiet && args.verbose { 582 | println!( 583 | "{} {}", 584 | "Creating directory".dimmed(), 585 | dst_parent.to_string_lossy().dimmed().underline() 586 | ); 587 | } 588 | std::fs::create_dir_all(dst_parent).with_context(|| { 589 | format!( 590 | "Failed to create directory. {}", 591 | dst_parent.to_string_lossy().yellow().underline() 592 | ) 593 | })?; 594 | } 595 | Ok(dst_parent.to_path_buf()) 596 | } 597 | 598 | pub fn should_relocate(src: &Path, dst_parent: &Path) -> bool { 599 | // NOTE `Path.parent()` returns `Some("")` in case of simple relative path. 600 | if let Some(src_parent) = src.parent() { 601 | !src_parent.as_os_str().is_empty() && src_parent != dst_parent 602 | } else { 603 | false 604 | } 605 | } 606 | 607 | pub fn execute_remove(operation: &Operation, _args: &CommandLine) -> Result<()> { 608 | let path = &operation.src.abs; 609 | if path.is_dir() { 610 | std::fs::remove_dir_all(path).with_context(|| { 611 | format!( 612 | "Failed to remove {}", 613 | path.to_string_lossy().yellow().underline() 614 | ) 615 | })?; 616 | } else { 617 | std::fs::remove_file(path).with_context(|| { 618 | format!( 619 | "Failed to remove {}", 620 | path.to_string_lossy().yellow().underline() 621 | ) 622 | })?; 623 | } 624 | Ok(()) 625 | } 626 | 627 | #[cfg(test)] 628 | mod lib { 629 | use std::path::PathBuf; 630 | 631 | use anyhow::{Context, Result}; 632 | use colored::*; 633 | use normpath::PathExt; 634 | 635 | use super::*; 636 | 637 | /// Create temporary files before starting tests and removed by RAII. 638 | struct Setup { 639 | sandbox: PathBuf, 640 | args: CommandLine, 641 | } 642 | 643 | impl Setup { 644 | /// 645 | /// Create following tree. 646 | /// 647 | /// ```ignore 648 | /// {sandbox}/ 649 | /// 1/ 650 | /// ├─1.txt 651 | /// ├─11/ 652 | /// │ └─11.txt 653 | /// ├─12/ 654 | /// │ └─12.txt 655 | /// 2/ 656 | /// ├─2.txt 657 | /// ├─21/ 658 | /// │ ├─21.txt 659 | /// │ └─211/ 660 | /// │ └─211.txt 661 | /// └─22 662 | /// └─22.txt 663 | /// ``` 664 | fn init(key: &str) -> Result { 665 | let sandbox = &std::env::temp_dir().join("moove").join("test").join(key); 666 | std::fs::create_dir_all(sandbox)?; 667 | let dirs: Vec = vec!["1", "1/11", "1/12", "2", "2/21", "2/21/211", "2/22"] 668 | .iter() 669 | .map(|d| { 670 | sandbox.join(PathBuf::from(if cfg!(target_family = "windows") { 671 | d.replace('/', "\\") 672 | } else { 673 | d.to_string() 674 | })) 675 | }) 676 | .collect(); 677 | for dir in dirs.iter() { 678 | println!("{} {}", "Creating".dimmed(), dir.to_string_lossy().dimmed()); 679 | std::fs::create_dir_all(dir)?; 680 | } 681 | let files: Vec = dirs 682 | .iter() 683 | .map(|dir| dir.join(dir.file_name().unwrap()).with_extension("txt")) 684 | .collect(); 685 | for file in files.iter() { 686 | println!( 687 | "{} {}", 688 | "Creating".dimmed(), 689 | file.to_string_lossy().dimmed() 690 | ); 691 | std::fs::File::create(file)?; 692 | } 693 | let setup = Setup { 694 | sandbox: sandbox.to_owned(), 695 | args: CommandLine { 696 | verbose: true, 697 | ..CommandLine::default() 698 | }, 699 | }; 700 | Ok(setup) 701 | } 702 | 703 | fn source_from(&self, s: &str) -> Source { 704 | let path = self.sandbox.join(if cfg!(target_family = "windows") { 705 | s.replace('/', "\\") 706 | } else { 707 | s.to_string() 708 | }); 709 | Source { 710 | text: path.to_string_lossy().to_string(), 711 | path: path.to_owned(), 712 | abs: path 713 | .normalize() 714 | .context(format!("Failed to normalize {:?}", path)) 715 | .unwrap() 716 | .into_path_buf(), 717 | meta: path 718 | .metadata() 719 | .context(format!("Failed to get metadata {:?}", path)) 720 | .unwrap(), 721 | } 722 | } 723 | 724 | fn destination_from(&self, s: &str) -> Destination { 725 | let path = self.sandbox.join(if cfg!(target_family = "windows") { 726 | s.replace('/', "\\") 727 | } else { 728 | s.to_string() 729 | }); 730 | Destination { 731 | text: path.to_string_lossy().to_string(), 732 | path: path.to_owned(), 733 | } 734 | } 735 | 736 | fn operation_from(&self, src: &str, dst: &str) -> Operation { 737 | Operation { 738 | kind: OperationKind::Move, 739 | src: self.source_from(src), 740 | dst: self.destination_from(dst), 741 | } 742 | } 743 | } 744 | 745 | impl Drop for Setup { 746 | fn drop(&mut self) { 747 | println!( 748 | "{} {}", 749 | "Removing".dimmed(), 750 | self.sandbox.to_string_lossy().dimmed() 751 | ); 752 | std::fs::remove_dir_all(&self.sandbox).unwrap(); 753 | } 754 | } 755 | 756 | #[test] 757 | fn list_sources_normally() -> Result<()> { 758 | let mut setup = Setup::init("list_sources_normally")?; 759 | setup 760 | .args 761 | .paths 762 | .push(setup.sandbox.join("1").to_string_lossy().to_string()); 763 | let sources = sources_from(&setup.args)?; 764 | assert_eq!(sources[0].path, setup.sandbox.join("1/1.txt")); 765 | assert_eq!(sources[1].path, setup.sandbox.join("1/11")); 766 | assert_eq!(sources[2].path, setup.sandbox.join("1/12")); 767 | setup.args.paths.clear(); 768 | setup.args.paths.push( 769 | setup 770 | .sandbox 771 | .join("..") 772 | .join(&setup.sandbox) 773 | .join("1") 774 | .to_string_lossy() 775 | .to_string(), 776 | ); 777 | let sources = sources_from(&setup.args)?; 778 | assert_eq!(sources[0].path, setup.sandbox.join("1/1.txt")); 779 | assert_eq!(sources[1].path, setup.sandbox.join("1/11")); 780 | assert_eq!(sources[2].path, setup.sandbox.join("1/12")); 781 | Ok(()) 782 | } 783 | 784 | #[test] 785 | fn should_fail_to_list_sources() -> Result<()> { 786 | let mut setup = Setup::init("should_fail_to_list_sources")?; 787 | setup.args.paths.push( 788 | setup 789 | .sandbox 790 | .join("does not exist") 791 | .to_string_lossy() 792 | .to_string(), 793 | ); 794 | assert!(sources_from(&setup.args).is_err()); 795 | setup.args.paths.clear(); 796 | setup.args.paths.push("/".to_owned()); 797 | assert!(sources_from(&setup.args).is_err()); 798 | setup.args.paths.clear(); 799 | setup 800 | .args 801 | .paths 802 | .push(setup.sandbox.join("1").to_string_lossy().to_string()); 803 | setup 804 | .args 805 | .paths 806 | .push(setup.args.paths.last().unwrap().clone()); 807 | assert!(sources_from(&setup.args).is_err()); 808 | Ok(()) 809 | } 810 | 811 | #[test] 812 | fn operate_normally() -> Result<()> { 813 | let setup = &Setup::init("operate_normally")?; 814 | let mut operations = Vec::new(); 815 | let new_operation = setup.operation_from("1/11/11.txt", "1/12/moved-11.txt"); 816 | is_operational(&operations, &new_operation)?; 817 | operations.push(new_operation); 818 | let new_operation = setup.operation_from("1/12/12.txt", "1/11/moved-12.txt"); 819 | is_operational(&operations, &new_operation)?; 820 | operations.push(new_operation); 821 | let new_operation = setup.operation_from("1/1.txt", "1/11/moved-1.txt"); 822 | is_operational(&operations, &new_operation)?; 823 | operations.push(new_operation); 824 | let new_operation = setup.operation_from("2/21/211", "moved-211"); 825 | is_operational(&operations, &new_operation)?; 826 | operations.push(new_operation); 827 | let new_operation = setup.operation_from("2/22", "moved-211/moved-22"); 828 | is_operational(&operations, &new_operation)?; 829 | operations.push(new_operation); 830 | for o in operations.iter() { 831 | execute_operation(o, &setup.args)?; 832 | } 833 | Ok(()) 834 | } 835 | 836 | #[test] 837 | fn should_not_be_operational() -> Result<()> { 838 | let setup = &Setup::init("should_not_be_operational")?; 839 | let operations = vec![ 840 | setup.operation_from("1/11/11.txt", "1/12/moved-11.txt"), 841 | setup.operation_from("1/12/12.txt", "1/11/moved-12.txt"), 842 | setup.operation_from("1/1.txt", "1/11/moved-1.txt"), 843 | setup.operation_from("2/21/211", "moved-211"), 844 | setup.operation_from("2/22", "moved-211/moved-22"), 845 | ]; 846 | [ 847 | ("1/11/11.txt", "1/11/11.txt"), 848 | ("1/11/11.txt", "1/12/12.txt"), 849 | ("1/11", "2/21/211"), 850 | ("1/11", "moved-211"), 851 | ] 852 | .iter() 853 | .for_each(|(src, dst)| { 854 | assert!(is_operational(&operations, &setup.operation_from(src, dst)).is_err()); 855 | }); 856 | Ok(()) 857 | } 858 | 859 | #[test] 860 | fn rename_file() -> Result<()> { 861 | let setup = &Setup::init("rename_file")?; 862 | let operation = &setup.operation_from("1/11/11.txt", "1/11/renamed-11.txt"); 863 | execute_move_or_copy(operation, &setup.args)?; 864 | assert!(operation.dst.path.is_file()); 865 | assert!(!operation.src.path.is_file()); 866 | Ok(()) 867 | } 868 | 869 | #[test] 870 | fn rename_dir() -> Result<()> { 871 | let setup = &Setup::init("rename_dir")?; 872 | let operation = &setup.operation_from("1/11", "1/renamed-11"); 873 | execute_move_or_copy(operation, &setup.args)?; 874 | assert!(operation.dst.path.is_dir()); 875 | assert!(!operation.src.path.is_dir()); 876 | Ok(()) 877 | } 878 | 879 | #[test] 880 | fn rename_dir_with_sub_dirs() -> Result<()> { 881 | let setup = &Setup::init("rename_dir_with_sub_dirs")?; 882 | let operation = &setup.operation_from("1", "renamed-1"); 883 | execute_move_or_copy(operation, &setup.args)?; 884 | assert!(operation.dst.path.is_dir()); 885 | assert!(!operation.src.path.is_dir()); 886 | Ok(()) 887 | } 888 | 889 | #[test] 890 | fn move_and_rename_file() -> Result<()> { 891 | let setup = &Setup::init("move_and_rename_file")?; 892 | let operation = &setup.operation_from("2/21/211/211.txt", "1/renamed-211.txt"); 893 | execute_move_or_copy(operation, &setup.args)?; 894 | assert!(operation.dst.path.is_file()); 895 | assert!(!operation.src.path.is_file()); 896 | Ok(()) 897 | } 898 | 899 | #[test] 900 | fn move_and_rename_directory() -> Result<()> { 901 | let setup = &Setup::init("move_and_rename_directory")?; 902 | let operation = &setup.operation_from("2/22", "1/3"); 903 | execute_move_or_copy(operation, &setup.args)?; 904 | assert!(operation.dst.path.is_dir()); 905 | assert!(!operation.src.path.is_dir()); 906 | Ok(()) 907 | } 908 | 909 | #[test] 910 | fn dry_run() -> Result<()> { 911 | let mut setup = Setup::init("dry_run")?; 912 | setup.args.dry_run = true; 913 | let operation = setup.operation_from("2/22", "1/3"); 914 | execute_operation(&operation, &setup.args)?; 915 | assert!(operation.src.path.is_dir()); 916 | assert!(!operation.dst.path.is_dir()); 917 | Ok(()) 918 | } 919 | } 920 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | #![doc = include_str!("../README.md")] 2 | 3 | use moove::*; 4 | 5 | use clap::Parser; 6 | use colored::*; 7 | use std::io::IsTerminal; 8 | 9 | #[doc(hidden)] 10 | fn main() { 11 | let mut args = CommandLine::parse(); 12 | if let Ok(env) = std::env::var("MOOVE_OPTIONS") { 13 | let env_args = CommandLine::parse_from( 14 | std::env::args() 15 | .take(1) 16 | .chain(env.split_ascii_whitespace().map(|o| o.to_string())), 17 | ); 18 | args.dry_run = args.dry_run || env_args.dry_run; 19 | args.verbose = args.verbose || env_args.verbose; 20 | args.quiet = args.quiet || env_args.quiet; 21 | args.absolute = args.absolute || env_args.absolute; 22 | args.directory = args.directory || env_args.directory; 23 | args.with_hidden = args.with_hidden || env_args.with_hidden; 24 | } 25 | let stdin = std::io::stdin(); 26 | if !stdin.is_terminal() { 27 | args.oops = true; 28 | let mut line = String::new(); 29 | while let Ok(size) = stdin.read_line(&mut line) { 30 | if size == 0 { 31 | break; 32 | } 33 | args.paths 34 | .push(line.trim_end_matches(['\r', '\n']).to_owned()); 35 | line.clear(); 36 | } 37 | } 38 | if args.paths.is_empty() { 39 | args.paths.push(".".to_owned()); 40 | } 41 | match try_main(&args) { 42 | Err(err) => { 43 | if !args.quiet { 44 | eprintln!("{} {:?}", "Error:".bright_red().bold(), err); 45 | } 46 | std::process::exit(2); 47 | } 48 | Ok(processed) => { 49 | if !args.quiet { 50 | if processed == 0 { 51 | println!("{} {}", "Info:".bright_cyan(), "Nothing to do".dimmed()); 52 | } else { 53 | println!( 54 | "{} Processed total {}", 55 | "Success:".green().bold(), 56 | processed.to_string().cyan() 57 | ); 58 | } 59 | } 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /tests/main.rs: -------------------------------------------------------------------------------- 1 | //! This test performs almost the same test as the lib.rs test except for the following: 2 | //! - Input paths are relative path from sandbox. 3 | //! - Change current directory to temporary (sandbox) directory for each test case. 4 | //! - Tests are executed sequencially to get consistent results. 5 | 6 | use moove::*; 7 | 8 | use std::path::PathBuf; 9 | 10 | use anyhow::{Context, Result}; 11 | use colored::*; 12 | use normpath::PathExt; 13 | use serial_test::serial; 14 | 15 | /// Create temporary files before starting tests and removed by RAII. 16 | struct Setup { 17 | sandbox: PathBuf, 18 | args: CommandLine, 19 | } 20 | 21 | impl Setup { 22 | /// 23 | /// Create following tree. 24 | /// 25 | /// ```ignore 26 | /// {sandbox}/ 27 | /// 1/ 28 | /// ├─1.txt 29 | /// ├─11/ 30 | /// │ └─11.txt 31 | /// ├─12/ 32 | /// │ └─12.txt 33 | /// 2/ 34 | /// ├─2.txt 35 | /// ├─21/ 36 | /// │ ├─21.txt 37 | /// │ └─211/ 38 | /// │ └─211.txt 39 | /// └─22 40 | /// └─22.txt 41 | /// ``` 42 | fn init(key: &str) -> Result { 43 | let sandbox = &std::env::temp_dir().join("moove").join("tests").join(key); 44 | std::fs::create_dir_all(sandbox)?; 45 | std::env::set_current_dir(&sandbox)?; 46 | let dirs: Vec = vec!["1", "1/11", "1/12", "2", "2/21", "2/21/211", "2/22"] 47 | .iter() 48 | .map(|d| { 49 | sandbox.join(PathBuf::from(if cfg!(target_family = "windows") { 50 | d.replace('/', "\\") 51 | } else { 52 | d.to_string() 53 | })) 54 | }) 55 | .collect(); 56 | for dir in dirs.iter() { 57 | println!("{} {}", "Creating".dimmed(), dir.to_string_lossy().dimmed()); 58 | std::fs::create_dir_all(dir)?; 59 | } 60 | let files: Vec = dirs 61 | .iter() 62 | .map(|dir| dir.join(dir.file_name().unwrap()).with_extension("txt")) 63 | .collect(); 64 | for file in files.iter() { 65 | println!( 66 | "{} {}", 67 | "Creating".dimmed(), 68 | file.to_string_lossy().dimmed() 69 | ); 70 | std::fs::File::create(file)?; 71 | } 72 | let setup = Setup { 73 | sandbox: sandbox.to_owned(), 74 | args: CommandLine { 75 | verbose: true, 76 | ..CommandLine::default() 77 | }, 78 | }; 79 | Ok(setup) 80 | } 81 | 82 | fn source_from(&self, s: &str) -> Source { 83 | let path = PathBuf::from(if cfg!(target_family = "windows") { 84 | s.replace('/', "\\") 85 | } else { 86 | s.to_string() 87 | }); 88 | Source { 89 | text: path.to_string_lossy().to_string(), 90 | path: path.to_owned(), 91 | abs: path 92 | .normalize() 93 | .context(format!("Failed to normalize {:?}", path)) 94 | .unwrap() 95 | .into_path_buf(), 96 | meta: path 97 | .metadata() 98 | .context(format!("Failed to get metadata {:?}", path)) 99 | .unwrap(), 100 | } 101 | } 102 | 103 | fn destination_from(&self, s: &str) -> Destination { 104 | let path = PathBuf::from(if cfg!(target_family = "windows") { 105 | s.replace('/', "\\") 106 | } else { 107 | s.to_string() 108 | }); 109 | Destination { 110 | text: path.to_string_lossy().to_string(), 111 | path: path.to_owned(), 112 | } 113 | } 114 | 115 | fn operation_from(&self, src: &str, dst: &str) -> Operation { 116 | Operation { 117 | kind: OperationKind::Move, 118 | src: self.source_from(src), 119 | dst: self.destination_from(dst), 120 | } 121 | } 122 | } 123 | 124 | impl Drop for Setup { 125 | fn drop(&mut self) { 126 | if let Err(_) = std::env::set_current_dir("..") { 127 | return println!( 128 | "Failed to change current directory to the parent {}", 129 | "..".yellow().underline() 130 | ); 131 | } 132 | println!( 133 | "{} {}", 134 | "Removing".dimmed(), 135 | self.sandbox.to_string_lossy().dimmed() 136 | ); 137 | std::fs::remove_dir_all(&self.sandbox).unwrap(); 138 | } 139 | } 140 | 141 | #[test] 142 | #[serial] 143 | fn rel_list_sources_normally() -> Result<()> { 144 | let mut setup = Setup::init("list_sources_normally")?; 145 | setup.args.paths.push("1".to_owned()); 146 | let sources = sources_from(&setup.args)?; 147 | assert_eq!(sources[0].path, PathBuf::from("1/1.txt")); 148 | assert_eq!(sources[1].path, PathBuf::from("1/11")); 149 | assert_eq!(sources[2].path, PathBuf::from("1/12")); 150 | Ok(()) 151 | } 152 | 153 | #[test] 154 | #[serial] 155 | fn rel_operate_normally() -> Result<()> { 156 | let setup = &Setup::init("operate_normally")?; 157 | let mut operations = Vec::new(); 158 | let new_operation = setup.operation_from("1/11/11.txt", "1/12/moved-11.txt"); 159 | is_operational(&operations, &new_operation)?; 160 | operations.push(new_operation); 161 | let new_operation = setup.operation_from("1/12/12.txt", "1/11/moved-12.txt"); 162 | is_operational(&operations, &new_operation)?; 163 | operations.push(new_operation); 164 | let new_operation = setup.operation_from("1/1.txt", "1/11/moved-1.txt"); 165 | is_operational(&operations, &new_operation)?; 166 | operations.push(new_operation); 167 | let new_operation = setup.operation_from("2/21/211", "moved-211"); 168 | is_operational(&operations, &new_operation)?; 169 | operations.push(new_operation); 170 | let new_operation = setup.operation_from("2/22", "moved-211/moved-22"); 171 | is_operational(&operations, &new_operation)?; 172 | operations.push(new_operation); 173 | for o in operations.iter() { 174 | execute_operation(o, &setup.args)?; 175 | } 176 | Ok(()) 177 | } 178 | 179 | #[test] 180 | #[serial] 181 | fn rel_rename_file() -> Result<()> { 182 | let setup = &Setup::init("rename_file")?; 183 | let operation = &setup.operation_from("1/11/11.txt", "1/11/renamed-11.txt"); 184 | execute_move_or_copy(operation, &setup.args)?; 185 | assert!(operation.dst.path.is_file()); 186 | assert!(!operation.src.path.is_file()); 187 | Ok(()) 188 | } 189 | 190 | #[test] 191 | #[serial] 192 | fn rel_rename_dir() -> Result<()> { 193 | let setup = &Setup::init("rename_dir")?; 194 | let operation = &setup.operation_from("1/11", "1/renamed-11"); 195 | execute_move_or_copy(operation, &setup.args)?; 196 | assert!(operation.dst.path.is_dir()); 197 | assert!(!operation.src.path.is_dir()); 198 | Ok(()) 199 | } 200 | 201 | #[test] 202 | #[serial] 203 | fn rel_rename_dir_with_sub_dirs() -> Result<()> { 204 | let setup = &Setup::init("rename_dir_with_sub_dirs")?; 205 | let operation = &setup.operation_from("1", "renamed-1"); 206 | execute_move_or_copy(operation, &setup.args)?; 207 | assert!(operation.dst.path.is_dir()); 208 | assert!(!operation.src.path.is_dir()); 209 | Ok(()) 210 | } 211 | 212 | #[test] 213 | #[serial] 214 | fn rel_move_and_rename_file() -> Result<()> { 215 | let setup = &Setup::init("move_and_rename_file")?; 216 | let operation = &setup.operation_from("2/21/211/211.txt", "1/renamed-211.txt"); 217 | execute_move_or_copy(operation, &setup.args)?; 218 | assert!(operation.dst.path.is_file()); 219 | assert!(!operation.src.path.is_file()); 220 | Ok(()) 221 | } 222 | 223 | #[test] 224 | #[serial] 225 | fn rel_move_and_rename_directory() -> Result<()> { 226 | let setup = &Setup::init("move_and_rename_directory")?; 227 | let operation = &setup.operation_from("2/22", "1/3"); 228 | execute_move_or_copy(operation, &setup.args)?; 229 | assert!(operation.dst.path.is_dir()); 230 | assert!(!operation.src.path.is_dir()); 231 | Ok(()) 232 | } 233 | --------------------------------------------------------------------------------