Using `in` to run a command in multiple directories
Recently, I found a cool Go utility named in
that allows you to run a command in a directory you specify. When you run the in
command with a directory that doesn't exist it first creates the directory and runs the command.
So this command is cool because it allows you to run a command in a specific directory on the command-line without first navigating to it (via cd
).
The idea behind the tool is to allow you to do this:
$ in build cmake ..
Instead of
$ mkdir -p build
$ cd build
$ cmake ..
Adding a feature to in
When I found this tool I knew I had to add another feature to it to accomplish something I have needed for a while; a command-line tool to run the same command in multiple project directories at once.
I sent in a Pull-Request to add glob support to enable in
to be used to run a command in multiple directories. I was pleased to see the PR merged ๐ So this functionality is available to anyone else that comes across in
.
So, let's see what we can do with this new feature:
Compile all maven projects
$ in "./**/*pom.xml" mvn clean compile
View last N commits for all projects in a directory
This works if the directory has subdirectories that are Git repositories.
$ in "./*" git log --oneline -n 3
Pull refs for all git repos in a directory
Like the previous example, this works if the directory has subdirectories that are Git repositories.
$ in "./*" git pull --all
Run go fmt on all directories containing Go files
$ in "./**/*.go" go fmt
Creating GitHub actions workflow directories in multiple directories
With Powershell
$ in ".\oss\*" powershell "mkdir .github/workflow"
With Bash
$ in "./oss/*" mkdir -p .github/workflow
Installing and using in
In order to use in
you will currently need to build it locally. Fortunately, the command does not have external dependencies, at the time of writing, so all you will need is the Go toolchain. Download Go here
$ git clone https://github.com/xyproto/in
$ cd in
$ go build
Personally, I build the binary as indir
as I think that makes more sense despite being more characters and also works much better with Powershell :). If you want to use that similar approach you can build it like so:
$ go build -o indir main.go
Or like this on Windows
$ go build -o indir.exe main.go
When you build it like that you can place the command on your $PATH
and call the command as follows:
$ indir "./**/*pom.xml" mvn clean compile
Conclusion
in
is a great tool which has saved me the trouble of writing multiple single-use case commands. Open-Source software is awesome and contributing to something that I needed and use feels good.
Moving on, I would like to improve the documentation to make sure people understand the behavior of the tool especially with the glob feature and add some notes on using the tool in a Windows environment.