A new tool for aliasing and executing single or multiple commands

Levent Ogut
3 min readJan 14, 2024

In this article, I’d like to introduce a new command execution tool called Xec. I recently published the code and await feedback, suggestions, and contributions.

Xec is a simple tool for executing pre-defined commands with their arguments. Xec reads its configuration file from the current working directory and the user’s home directory; this way, you can have aliases per-directory (e.g., per project, per domain) and per user.

xec
Simple command (task) executor.

Usage:
xec <flags> <alias> -- [additional-arguments] [flags]

Available Commands:
completion Generate the autocompletion script for the specified shell
help Help about any command
ls list all files, long format, size units
lsenv tasklist for ls and env
lszenv tasklist for ls and env errors
version Print the version number

Flags:
...

Once Xec reads the configuration, it generates its cli options based on it. On the above output, all aliases configured are listed.

Task

The task keyword defines a single alias, command, arguments, and other configuration options.

A configuration snapshot of one of the tasks defined as follows:

...
tasks:
- alias: myls
description: execute custom ls.
cmd: ls
args:
- -al

You run the alias as follows:

xec myls

This will run ls -al.

You can also run the alias with additional parameters to be passed to the command:

xec myls -- -Z

This will run ls -al -Z.

Task list

Xec has a task list concept where you can group several tasks under an alias. Configured as follows:

...
taskLists:
- alias: lsenv
description: "tasklist for ls and env"
taskNames:
- ls
- printenv
- alias: lszenv
description: "tasklist for ls and env errors"
taskNames:
- lsz
- printenv
ignoreError: true

This first task list alias would be executed as:

xec lsenv

In the second task list, the referred lsz alias has an invalid argument for ls; this is to demonstrate the ignore error functionality. Even though thelszalias will fail,printenv` alias will still be executed, this control flow can be useful on pipelines.

Environment

Xec also provides some security for passed environment values. The environment values that should be passed to the command are customizable by adding new values and regex-based filters for accepting or rejecting; by default, all environment values are passed as you would have written the command on the shell.

A sample environment configuration of a task.

...
environment:
passOn: true # Pass the environment key/values that Xec receives to the process or not.
values: # Additional environment values to pass to the process.
key: value
environment: prod
acceptFilterRegex: # Include the environment values matches the regex.
- "XEC_*"
rejectFilterRegex: # Don't include the environment values matches the regex.
- "SECRET*"
- "AWS*"
...

Exit codes

Xec tracks the tasks’ exit code and exits with it when there is a single task; this behavior can be customizable so that command exit-code is ignored and zero exit-code is returned.

Where Xec executes multiple commands in one go, you can configure the whole list or individual tasks return-code strategy.

Dependencies

Xec has two dependencies:

Usage

The primary function I had in mind when writing Xec was running complex multiple-argument commands where I could still add new arguments on demand. However, while developing, I also saw its potential in CI/CD pipelines.

License and Contribution

Xec is open-source and distributed with an Apache license.

Contributions code or no-code(suggestions, feedback, issues, etc.) are most welcome.

Releases

Xec is only available on Linux and Darwin at the time of writing; I hope Windows support will be added in the near future.

Xec is distributed as a binary initial release, found here.

--

--