Skip to content

Helpers

Jérémie Campari edited this page Jan 8, 2019 · 7 revisions

Helpers

Generic helpers

Pulsion help you to develop your bash script faster. It provides a list of functions to make generic actions in your command process. This functions, available by default in Pulsion, are called helpers.

  • print : Echo or not depending on the verbose level (1=very high importance only, 5= very low importance)
print 3 "Echo only if level of verbose is <= 3"
p 3 "p is an alias of print. It works the same way"
pulsion hello -v 3 # Verbose level
  • has_param : Return true (0) if the param is present
if has_param --name "$@"; then
	...
  • get_param_value : Return the value of a parameter
myname="$(get_param_value --name "$@")"
  • is_command_exist : Return true (0) if the shell command exists
if is_command_exist echo; then
	echo "Echo is a valid shell command"
fi
  • is_function_exist : Return true (0) if the function exists
if is_function_exist echo; then
	...
  • is_root : Return true (0) if the user have root access
if is_root; then
	...
  • require_root : Exit if the user don't have root access
require_root
  • get_platform : Return current platform #{osx, linux} (windows not detected for the moment)
if [[ $(get_platform) != "osx" ]]; then
	...
  • get_index_in_array : Search a value in an array and return the index where value is found
index="$(get_index_in_array "$value" "${array[@]}")"
# Return -1 if nothing is found

Stream helpers (alpha)

Easily manage stdin and stdout streams with filters, duplicators, actions...

Create a basic stream

In this example, we capture the output of ls command, keep only directories with testin the name, then remove it.

stream::action rm \
  stream::filter *"test"* \
    ls

Note : The Pulsion stream must be read from bottom to top.

Duplicate a stream

Sometimes, you want to apply multiples pipelines on the same stream. So, you need to duplicate the stream and apply different pipeline on each copy.

In the below example, the ls output is duplicated. We apply to pipeline on it.

In the first pipeline, we keep only directories containing test and then, removes it.
In the second pipeline, we keep only directories containing my-text and we store the output of "cat" command in the file with name "my-file".

  stream::duplicator::new

  # Configure first pipeline
  stream::duplicator::add \
    stream::action rm =\
    stream::filter *"test"*

  # Configure second pipeline
  stream::duplicator::add \
    stream::to-file my-file =\
    stream::action cat =\
    stream::filter *"my-text"*

  # Execute ls
  stream::duplicator::forward ls

All streams functions available

  • stream::print-stderr : Print the stream only if it comes from stderr (must be use as first-level function)
  • stream::print-stdout : Print the stream only if it comes from stdout (must be use as first-level function)
  • stream::print 2 : Print the stream if verbose >= 2
  • stream::filter *"regex"* : Forward the stream line only if line match regex
  • stream::action command : Execute a shell command or a bash function with stream line as argument
  • stream::to-file : Write the stream lines in a file

Contributions to helpers

If you think that an helper can be added, create an issue and talk us about it or push a pull request.