shortcommand
Easily run a set of commands quickly using a yaml configuration file
Usage
Create a yaml file named shortcommand.yml or anything you want. Here's an example:
shortcommands: - name: Journals commands: - name: ui description: Pull, build and deploy Web-UI cwd: ~/Apps/Journals/Web-UI do: - git pull - command: npm install skip-if-unchanged: - package.json - package-lock.json - npm run build - name: api description: Pull, build and deploy API cwd: ~/Apps/Journals/API do: - git pull - shards build --release - pm2 restart "Journals API" - pm2 reset "Journals API" - name: QuickNote commands: - name: api description: Pull, build and deploy API cwd: ~/Apps/quick-note/API do: - git pull - pm2 restart "Quick Note API" - pm2 reset "Quick Note API" - name: restart-api description: Restart API in pm2 do: - pm2 restart "Quick Note API" - pm2 reset "Quick Note API"
Then add this in your ~/.bashrc or ~/.zshrc:
export SHORTCOMMAND_CONFIG=/path/to/shortcommand.ymlNow if you run shortcommand without any arguments, you'll see this:
Available commands
Journals
ui Pull, build and deploy Web-UI
api Pull, build and deploy API
QuickNote
api Pull, build and deploy API
restart-api Restart API in pm2Works as a readme for your command sets in case you forget what options are available for you to use.
Now the above defined short commands will be accessible and can be used by running:
shortcommand Journals ui shortcommand Journals api shortcommand QuickNote api shortcommand QuickNote restart-api
Note: Commands mentioned under do: will execute in sequence and will not continue to the next one if the one that's running fails.
This project was created mainly because I have several apps that I run on my server and finding the right set of commands for deploying an app is a hassle. So this basically documents the set of commands for each of my projects, as well as gives me quick access to them.
Features
Conditional Command Execution with Caching
You can skip expensive commands (like npm install, composer install, etc.) if their dependency files haven't changed:
do: - git pull - command: npm install skip-if-unchanged: - package.json - package-lock.json - npm run build
How it works:
- On first run, all commands execute and file hashes are cached
- On subsequent runs, the command is skipped if watched files haven't changed
- Files are tracked using SHA256 hashes stored in your OS cache directory:
- Linux:
~/.cache/shortcommand/ - macOS:
~/Library/Caches/shortcommand/ - Windows:
%LOCALAPPDATA%\shortcommand\cache\
- Linux:
Available flags:
--force- Ignore cache and run all commands--clear-cache- Clear all cached file hashes
Example:
# First run: executes npm install shortcommand Journals ui # Second run: skips npm install (no changes) shortcommand Journals ui ⏭️ Skipping: npm install (no changes detected) # Force execution even if no changes shortcommand --force Journals ui # Clear all cache shortcommand --clear-cache
Installing on Linux (tested on Ubuntu)
wget https://github.com/flawiddsouza/shortcommand/releases/download/v0.1.0/shortcommand_0.1.0_Linux_x86_64.tar.gz
tar -xf shortcommand_0.1.0_Linux_x86_64.tar.gz
mv shortcommand ~/.local/bin
rm shortcommand_0.1.0_Linux_x86_64.tar.gzYou should then be able to use the shortcommand command anywhere you are.
Testing
SHORTCOMMAND_CONFIG=shortcommands-example.yml go run . Test test SHORTCOMMAND_CONFIG=shortcommands-example.yml go run . Test test2 SHORTCOMMAND_CONFIG=shortcommands-example.yml go run . Test test3 SHORTCOMMAND_CONFIG=shortcommands-example.yml go run . Test test-cache
Run the automated test suite:
# With Go installed go test -v # With Docker (if Go not installed) ./test.sh # Run specific test ./test.sh -run TestConditionalCommandExecution # Run with coverage ./test.sh -cover # Run verbose ./test.sh -v
Build Testing
goreleaser build --snapshot --rm-dist
Build for release
go tag vX.X.X git push origin vX.X.X goreleaser release --rm-dist