#fuga: a two-step command-line tool for working with files
##Table of Contents
##Introduction
This Golden Week I had ten days off in a row for the first time in a while and wanted to try something new.
I’d recently started learning Rust, so I decided to use the break to build a command-line tool.
This post introduces that tool, fuga, and describes what it was like to build a CLI in Rust.
##The CLI I built
First, I will introduce the CLI tool that I created this time.
I publish here.
A CLI tool to operate files or directories in 2 steps.
Since we haven't yet provided the executable binary file, you will need to set up a Rust development environment to install it, but we would be very happy if you would like to try it out if you are interested.
If there are any bugs or inconvenient points, please write them in the issue, and we will consider taking action.
###Why I built it
This time, when developing the CLI tool, I decided to create something that could improve the things I had personally found inconvenient in the past.
I don't know how many people understand this, but I've been dissatisfied with file operations on the terminal for a long time.
I will provide some specific examples to explain.
Specifying paths with mv and cp
When you are manipulating files using the command line, it is common to use the move command mv and the copy command cp.
However, when performing file operations with these commands, if you try to specify a relative path, the resulting length can be extremely long.
For example, have you ever experienced typing a long command like the following?
$ mv test.txt ../../../path/to/directory/that/you/want/to/move
I can imagine them pounding the tab and desperately typing in the pass, and just watching it makes my little finger on my left hand hurt (lol).
On the other hand, if you operate using the GUI, right-click the target file and select "Cut or Copy," then move to the target directory and right-click again to "Paste" to operate.
Don't you think this makes the operation simpler by dividing it into two steps?
Copying directories with cp
I also keep getting frustrated with the next example, but when copying a directory using the cp command, you need to use the -r option.
When you hit a long pass like this just now, the irritation level increases even more (lol).
$ cp test_dir ../../../path/to/directory/that/you/want/to/copy
cp: -r not specified; omitting directory 'test_dir'
Since the mv command is unnecessary in the first place, why do you need -r with the cp command?
If you know the reason, please let me know...
Remembering the argument order for ln
You can create a symbolic link using the ln command.
It's not used that often, but sometimes it comes in handy, and at those times you often forget the order of the path to specify in the command.
Correctly speaking,
$ ln -s [対象のファイルorディレクトリ] [作成するシンボリックリンクのパス]
However, because I only use it occasionally, I find it difficult to memorize it with confidence.
###How fuga works
The CLI tool 'fuga' that we developed this time was created to solve the aforementioned problem.
The reading is "Fugue."
Actually, there is a proper source material, but if you have something you think might be possible with this, please feel free to contact us on Twitter or other platforms.
The things that can be done with fuga are the same as the three aforementioned commands mv, cp and ln, which are "file move", "file copy", and "create symbolic link".
The part that is significantly different from these commands is that file operations are performed in two steps: "marking" and "command execution".
(If there is already a command with a similar function, it would be very helpful if you could let me know...)
For example, when moving a file,
- Mark the target file (
fuga mark <target>) - Navigate to the target directory
- Execute file movement (
fuga move)
You can do it in a similar manner.
// Mark files
$ fuga mark test.txt
✅ : 📄 test.txt has marked.
// Change to the destination directory
$ cd ../../..
$ cd path/to/directory/that/you/want/to/move
// Move the marked files
$ fuga move
ℹ️ : Start moving 📄 test.txt from /path/to/source/directory
✅ : 📄 test.txt has moved.
The basic operations for copying and symbolic links are basically the same.
After marking, you can copy or create a symbolic link using the following command.
fuga copyfuga link
For details, please refer to the README in the following repository.
A CLI tool to operate files or directories in 2 steps.
##Writing a CLI in Rust
I have been studying Rust for a long time, but this is the first time I have actually created something.
So, we proceeded with the development while being completely in the dark.
We are not sure to what extent we have come closer to best practice, but we would like to introduce the main libraries we used in this development.
This is a library for developing CLI tools.
You can easily set up subcommands, command line arguments, flags, etc.
It automatically constructs commands like help based on comments out, which was very convenient.
It is a library that allows you to easily create and read/write configuration files.
The directory specified below ~/.config will be automatically created and used to manage the configuration files there.
This time, we are saving the marked paths in this settings file.
This library handles moving and copying files.
There are similar features in the standard Rust library, but this one has a more organized API and was easier to use.
Also, the reason I chose it was because it had a good compatibility with the indicatif mentioned later.
This is a library that displays a progress bar when moving or copying files.
By passing it as a handler to fs_extra, you can visualize the progress of file operations.
##Wrapping up
I spent my Golden Week break building a CLI in Rust.
My original design had quite a few more features. When I realized I wouldn’t finish it over the break, I changed direction and arrived at the smaller tool you see here.
I’d still like to revisit those ideas.