Skip to content

DonTseTse/bash_commons

Repository files navigation

Bash commons (bash_commons)

Library of bash functions organized in several thematic collections; each is one source file.

How to:

  • Clone this repository
  • Source the collection file which contains the desired functions

Example:

commons_path="/path/to/bash_commons"
. "$commons_path/filesystem.sh"
# from here on, all filesystem collection functions are available

It's compulsory to set up $commons_path with the absolute path of the folder with the cloned code (many collections use it to load internal dependencies; for details see the table below). $commons_path may also be set up as global variable, the easiest way to do this it to append an entry to /etc/environment, with the command

echo "commons_path=\"/path/to/bash_commons\"" >> /etc/environment

Then, sourcing a collection simply becomes:

. "$commons_path/filesystem.sh"
# from here on, all filesystem collection functions are available

The tests can be found in /tests.

Collections:

#NameLinksInternal dependency
1FilesystemOverviewDocumentationCode Tests#2, #7
2String handlingOverviewDocumentation CodeTests#7
3LoggingOverviewDocumentation CodeTests#2, #7
4Installer toolsOverviewDocumentation CodeTests#1, #2, #7
5Git handlingOverviewDocumentation CodeTests#1, #2, #7
6InteractionOverviewDocumentation Code-
7HelpersFunction indexDocumentation CodeTests-
8TestingOverviewDocumentationCode #2, #7

Filesystem

The filesystem collection provides:

String handling

The string handling collections includes functions to modify strings and control certain properties as well as sed helpers:

Installer tools

The installer tools module provides a helper called get_executable_status() which checks all details about an executable beyond what which does. The installer tools' handle_dependency() takes in charge the installation of packages if necessary and provides an extensive status return for a precise overview of the situation. This allows an installer to define package lists (by package manager) and every time a command is missing, this list is looked up and it attempts to install the package(s) listed. The handler's behavior may be further customized with callback functions and configurable status message templates.

Git handling

The git handling module provides execute_git_command_in_repository() to execute git commands in a repository context and get_git_repository_remote_url() to get details about a local repository. get_git_repository() is a git clone wrapper with detailed error status codes combined with a parameter which controls the functions' stdout behavior and a verbose mode with configurable message templates using runtime variable injection.

Interaction

The interaction collection provides the basic building blocks for interactive scripts, f.ex. installers: get_user_confirmation() for yes/no type questions, get_user_choice() for multiple choice questions. Both use read_and_validate() internally, which is simply the combination of a read and a regex check.

Logging

The logging collection's functions work together as one module which provides several features:

  • distinct output channels for stdout and file logging, each with their own logging level and message pattern
  • a log message buffer which allows to use log() before the logger is configured. Applications can start logging from the very beginning with logging "disabled" - in fact, messages go into the buffer, nothing is actually logged. Once the configuration is known (usually, when the script parameters were processed - typically to handle that -v flag that enables stdout logging), the application calls launch_logging() to "replay" the buffered messages and log them (or not) according to the configuration in force when launch_logging() is called
  • a utility to shorten and hide secrets before they enter the logs: prepare_secret_for_logging()

Testing

Testing is based on "sessions" which are sequences of test operations. A session begins with initialize_test_session() and ends with conclude_test_session(). Each test follows the scheme:

  1. set the expected result in terms of status code and stdout content with configure_test()
  2. run the command capturing these values with test()

In some cases, f.ex. if the command uses piped input, it's not possible to use test(), the command has to be run in the testing script itself (example). In this case the results shall be evaluated using check_test_results() (test() calls it internally).

For examples of test scripts, check out bash_commons' own tests.

Helpers

Used by the other modules. Function index:

TODO

  • improve handling & clean up if the root user executes the tests, especially for filesystem functions and installer tools
  • transform verbose mode for wrapper functions in filesystem collection: merge the "stdout configuration" and "external message template definition array name" parameters. The logic is:
    • if that new merged parameter is omitted or an empty string, it corresponds to stdout silent mode
    • if it's set to stderr and status, these modes are applied as before
    • if it's set to a non-empty value which is not a variable name, it uses the default message templates (= current verbose mode without external msg def param.)
    • if it's set to an array variable name, it tries to find a custom message template (a element in the array at the index corresponding to the status, = current verbose mode with external msg def param). If no template is found, it falls back to the defaults The only restriction is that an external message definition array variable can't have the names stderr or status. It's implemented this way in the git handling get_git_repository().
  • extend git handling with other git operations
  • extend installer tools to detect/support other package managers

About

Collection of bash functions for common tasks

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages