2 releases

0.1.1 Sep 17, 2024
0.1.0 Aug 7, 2024

#561 in Development tools

MIT license

40KB
914 lines

CXD

Command Executor per Directory (cxd) provides a simple interface to save and execute commands within specific directories.

Background

While working in large codebases with several executables, it was common for me to add custom scripts and Makefiles to each directory. This allowed me to not have to rely on bash history to remember my specific commands. However, after one too many git-merge events overwriting my temporary Makefiles, I wrote cxd.

Dependencies

  • libsqlite3

Installation

cargo install cxd

Bash Completions

Clone the files under pkg/completions/bash/ to the first of the following directories.

  • /usr/share/bash-completion/completions/
  • (deprecated) /etc/bash_completion.d/

Zsh Completions

Clone the files under pkg/completions/zsh/ to any directory listed by echo $fpath. For example, on Debian 12 this includes /usr/share/zsh/vendor-completions by default.

You may need to logout and login for these changes to take effect.

Use

cxd uses a sqlite database to save commands and their respective directories to execute at a later time. Database cache files can be saved and reused across devices. However, note that you should NEVER trust a cache file from an outside source, as cxd will execute arbitrary commands from the database.

Selecting Cache File

By default, cxd will attempt to store the cache file in the following locations, and fail if unable to construct any.

  1. Contents of -f FILE or --file FILE
  2. $CXD_CACHE_DIR/cxd.cache
  3. $XDG_CACHE_HOME/cxd.cache
  4. $HOME/.cache/cxd.cache

Adding a Command

To add a command to the database, use cxd --add <NAME> <CMD> [ARG]....

cxd --add hello echo Hi there!

This will register a command named hello that prints out a greeting.

If you'd like the command to swap directories back to your current $CWD before invoking the command, pass the --cwd flag.

cxd --add --cwd build cargo build

Similarly, you can set a specific working directory with --dir <DIR>.

cxd --add --dir /src/cxd build cargo build

If specific environment variables must be set, use --env <KEY>=<VALUE>.

cxd --add --env SOME_ENV=hi hello printenv SOME_ENV

Executing a command

To execute a command from the database, use cxd <CMD>.

cxd hello

Removing a command

To remove a command from the database, use cxd --remove <CMD>.

cxd --remove hello

Listing

To list all commands in the database, use cxd --list.

cxd --list

Clearing

To clear all commands in the database, use cxd --clear.

cxd --clear

Tips

Using multiple cache files

It can be useful to segment cache files for specific commands. A simple and ergonomic way to do this is to set aliases.

For example, imagine you want two separate cache files, one for playing media and one for building executables.

Then you can add the following to your .bashrc, .zshrc, or equivalent.

alias build="cxd -f .cache/cxd.build.cache"
alias play="cxd -f .cache/cxd.play.cache"

This allows you to run cxd with each cache file as if it were two different commands, build and play.

Dependencies

~23MB
~444K SLoC