/anek

Tool to run commands based on a template

Primary LanguageRustGNU General Public License v3.0GPL-3.0

Introduction

This program can help you run commands that are defined as templates over different inputs.

Everything including the template commands and the input informations are stored inside the .anek directory. The working directory will be the current one unless specified. Although the command can work on other directory given as the path at the end, the autocomplete script will only work for the current directory.

As this is a program made to run different inputs for commands, please keep the complicated logics in different programming languages, and complicated commands in a shell script files. And use the anek commands, or input files to give variable inputs as arguments to those programs/scripts. That way you can run those scripts with manual input even when you don’t have access to anek program, or someone who doesn’t know how to use it can also use those independently. And since anek saves everything as text files you can look up what variables to give as inputs.

Installation

  • Arch Linux: AUR Package or use the PKGBUILD file
  • Others: git clone then cargo build --release

Demo

Demo video with explanation.

./images/video-thumb.png

Following image shows the configurations of the provided .anek demo config, with the repository. You can see how pipelines, or batch files work, as well as the relationship with the inputs and the commands which is indirect with variables. You can only use inputs that have the variables used in a command (if not you can override them from terminal as well).

You can use anek graph command to generate a DOT file of this graph, which you can render using graphviz commands like dot or =neato=.

./images/screenshot.png

Simple Demo

So make a directory, and get inside it,

mkdir testdir
cd testdir

Now make anek directory,

anek new

Then make a input file with your name and age:

echo "name=John Doe" > .anek/inputs/person1
echo "age=35" >> .anek/inputs/person1

Now make a command that prints out the name and age:

echo 'echo "My name is {name}, I am {age}."' > .anek/commands/intro

Now run the command with your input:

anek run --command intro --input person1

The first line shows the rendered command, and then it’s run (which prints the second line here).

That’s about it, you can use it for any commands, and you can have multiple input files, you can process them in batch, loop it, and everything.

Installation

Use cargo to compile the program, and then copy that binary to your path.

cargo compile --release

If you’re using bash and want autocompletion, then you can copy completions/bash-completions.sh to somewhere it loads it (e.g. /usr/share/bash-completion/completions/anek, /etc/bash_completion.d/anek, etc), or load it in .bashrc yourself.

Commands

The commands that are available till now are these. Please feel free to use the help from the command itself to look at the available commands in your version, and their usages.

new

Make a new configuration setup (directories) in current directory, or the path specified.

anek new

Variables

Commands related to variable files. Variable files are not necessary to run any of the commands, as long as their values are provided in the terminal or the favorites files.

Variable files are helpful for using the completion features and to maintain the documentations about the variables. You can write variable files with the short description in the first line, and then full description.

anek variable -l

Scanning variables will make empty files for new variables.

anek variable -s

list

List available things like inputs, batches, commands, pipelines etc. It is mostly used for generating autocomplete. But users can also use it to list the available options for them. You can also filter the list. For example, filtering the commands based on a variable being used in the command.

For example in our sample directory: anek list -i will give us sample1 sample2 sample3 but anek list -i -F skew will give us just sample3. It also works with commands.

edit

Edit or view files inside .anek. It basically just calls your editor, so consider this a shortcut for calling your EDITOR. You shouldn’t specify the full path, but relative path from inside .anek.

All the valid paths can be listed using anek list -a. Completion will help you there by proving them.

Example:

anek edit inputs/sample1

run

Main command to run/print the commands or pipelines. It can also be used to just render the template or to export the variables.

To run something, you need a command, and if the command has some template variables then you need those input variable.

Command

There are 3 ways to give commands to run. Either you give a command template form the terminal, from command file saved, or you can give a pipeline which is a sequence of command files.

Command template

You can give a template, and then it’ll run it. A command template is a shell command with optional input variables. For example, a command template echo Hello World will run by itself without requiring any input variables. While a command template echo Hi {name} would require the value for name variable to run. Refer the Inputs section for how to provide the values for the variables.

In case the variable is not found it’ll error out with a message. If you want to avoid that, and use empty string if the variable is not found then use ? at the end of the name. This is a OR operator, that’ll use the first varible it’ll find. So, {mean?sd} will try to render it with mean if not then sd and if not it’ll fail. while {mean?sd?} will try those two and if not found it’ll render it as empty. Simple {mean?} will render mean empty if it cannot find it.

You can also use a literal string as a replacement character if the variable is not available, for example {mean?"mean"} will first search for mean variable, if it cannot find it, then it’ll render the literal string mean.

Command

You can give the name of the command file saved inside anek commands and it’ll run it. Since they’re just a simple template that’ll be filled and run, and the file itself is saved inside the hidden directory .anek, keep the commands simple. If you need some complicated logic (more than a single line), just write those in a shell script in the working directory and use the command file to pass the arguments that are the variables to the script.

Pipeline

You give the name of the pipeline and it’ll run the commands one by one. It is useful when you need to execute commands one after another, or just some commands all in the same inputs. A single command or a command template is considered a pipeline with one task.

Inputs

If your command has no inputs needed, it can run as it is. But if it does, then you can specify them in these 4 ways.

Input File

You can save values of variables as input files, and then pass that as input, it’ll use them to fill the template and run it.

You can give a input file, or directory. If file, then it’ll read those variables. If directory then it’ll use all the files inside that directory recursively and make a hashmap, so if you have repeated variables it’ll be replaced.

Given a directory, it is supposed to read the files alphabetically in one depth, and then recursively inside. Might be more meaningful to have the variable from file outside overwrite those inside directories but I haven’t implemented that.

Overwrite

This command can be used to overwrite the values provided with other commands, but you can also use it standalone and use them as inputs without having to load them from any files.

Overwrite has a extra feature not available to others, that is the presence of metavariables. You can use the variables from the input to be used in the overwrite process. For example: if you have input values of mean=10 in input sample1, then you can use -o mean:20,sd={mean} which’ll replace the value of mean by 20, while keep the old mean’s value as sd. The metavariables used in the overwrite only has access to the variables’ values from input files (and batch files as they use input files one after another) and are useless in case of loop, or other variables provided with overwrite flag itself.

Even without the metavariables, overwrite is powerful, it can be used in any situation, for input files it’ll overwrite those (or add those) provided variables. For batch it does the same for each input files, and for loops it’ll overwrite the variable which can mean not looping for a variable if originally it had multiple values. You cannot give multiple values to loop using the overwrite.

Batch

You can make a batch file with list in input files, the command will run in each one of the input files separately.

This is just a convinient feature to process multiple input files in a batch. if you have only one command, or a pipeline that doesn’t depend on the commands executing one after another, you can run it in parallel.

Loop

You can make a loop directory, inside it a file for each input you want. And the files should have the values for that variable. You need at least one value for all variable, anything more than that, then it’ll be looped with one value at a time. You can have multiple input files with multiple values to have a different combinations of the inputs in the loop (2 values of input1 and 2 values of input2, will run it 4 times).

Other options

Other options that are related to commands are pipable and demo. Demo will only print the generated commands and not run it. Pipable will also print the commands but not anything else. So you can pipe those commands to other programs. For example you can pipe it to bash when you want to just run them without any other info, or you can pipe it to gnu parallel if you want to run those commands in parallel.

Since pipable doesn’t run the commands, you can also use it to print/list out some of the inputs, and then pipe it to awk/files/grep or anything.

Rendering only options

These options are `-r`, `-R`, and `-e`. Look into help menu for more details.

The inputs can be used in the same way to just render something or to export the rendering.

They are useful when you just want to look at some variables, or to generate a report based on a template file. Or make csv or json of the variables for different inputs.

If you want to export the whole set of variables you can generate that with some bash-fu.

For example this command here will make a template with optional rendering of all the variables in the config, run it in a batch mode for all inputs and save it in a csv.

anek run -e $(printf "%s?," `anek list -v`) -b all > /tmp/data.csv

completions

Prints completion for shells. It’s for internal use for now, though you can get completions for you from this.

Since the clap_complete doesn’t have the features to generate the completions using a shell commmand from inside rust yet. I’m editing the output from this manually and providing it separately for bash. I haven’t tried it for other shells.

Look at installation instructions for how to install the completions on bash.

I’ll probably make it specify shells so that people can generate the rudimentary completion for other shells.

Limitations

  • The variable names are not tested with lots of different characters, so please use the characters you use for variable names in any sane programming language (a-z A-Z 0-9 and _).
  • The --overwrite flag in run command only overwrites from the values in input files. and doesn’t work with --loop
  • The command files are assumed to be single shell command, multiline commands with complex logic are not tested. But it does work with pipes and such, so just put what you’d type in your terminal.