Skip to content

TangoMan Shoe Shell Microframework : A Lightweight script boilerplate for Posix Systems.

License

Notifications You must be signed in to change notification settings

TangoMan75/shoe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

GH language GH release GH license GH stars Bash CI Shellcheck CI Visitors

πŸ‘  TangoMan Shoe Shell Microframework

A Lightweight script boilerplate for Posix Systems

Simplify your shell scripting workflow with self-documenting shell scripts with robust argument validation. Just prefix a comment with two pound signs (##) on the line above any function or variable to generate helpful documentation using the "help" command.

shoe

πŸ€” Why not simply use Make ?

While GNU Make is a powerful build tool, it's not primarily designed for general-purpose scripting :

  1. Its specialized syntax for defining dependencies and build rules can be less intuitive and flexible compared to plain Shell scripting.
  2. Additionally, GNU Make may not be universally available across all Linux distributions, particularly in Android-based systems or minimal Docker containers like Alpine or Busybox.
  3. Also, a lack of dedicated linting and unit testing tools for Makefiles can hinder code quality and maintainability.
  4. Finally Makefile's parameter handling approach is inconvenient, often requiring the use of environment variables.

TangoMan Shoe Shell Microframework fills this gap by providing a lightweight, Posix-compatible alternative that's easy to set up, lint, test and use.

🎯 Features

TangoMan Shoe Shell Microframework provides the following features:

  1. ⚑ Shell Script Boilerplate:

Use TangoMan Shoe Shell Microframework as a starting point for your own shell scripts, benefiting from its modular structure and best practices.

  1. ⚑ Semantic Output Coloring:

TangoMan Shoe Shell Microframework includes a suite of visually appealing and semantically meaningful color functions. You can print primary, secondary, success, danger, warning, info, light and dark text with indentation and padding in any POSIX terminal.

  1. ⚑ Easy Annotation:

TangoMan Shoe Shell Microframework provides an intuitive annotation system that enables parameter validation and automatic documentation generation.

  1. ⚑ Self-Documenting Shell Scripts:

Prefix a comment with two pound signs (##) on the line above any function or variable, the help command will generate documentation on the fly.

  1. ⚑ Automatic Function-to-Command Conversion:

Define your functions as usual, they are automatically available as commands at runtime, making your scripting more intuitive.

  1. ⚑ Automatic Variable-to-Option Conversion:

Define your global variables as usual, they are converted as options at runtime.

  1. ⚑ Runtime Parameter Validation:

Append a regular expression (eg: /^a-zA-Z$/) at the end of any option's annotation, it will provide automatic validation at runtime.

  1. ⚑ Automatic Boolean-to-Flag Conversion:

Set a global variable to false, it creates a flag for simple on/off settings.

  1. ⚑ Automatic Shorthand:

Shorthand for commands, options and flags are automatically created.

  1. ⚑ POSIX Shell Compatibility:

TangoMan Shoe Shell Microframework is 100% POSIX compliant, run your scripts in virtually any environment, including containers and android based systems.

  1. ⚑ MacOS Compatibility Layer:

TangoMan Shoe Shell Microframework provides optional compatibility functions, since some shell commands syntax can vary between Mac and Linux (eg: sed).

  1. ⚑ Global Installation With Autocomplete:

Install and use your custom command across your system, TangoMan Shoe Shell Microframework will dump and load the appropriate comspec in your sh, bash or zsh terminal.

  1. ⚑ Extract Markdown Documentation Directly From Script Source:

Automate the generation of comprehensive Markdown documentation from a single source of truth. TangoMan Shoe Shell Microframework extracts information from the script annotations to generate documentation, it will never become outdated !

  1. ⚑ Automate Makefile Creation:

By extracting information from your shell scripts, it automatically generates Makefiles, complete with build rules and help messages.

  1. ⚑ Shellcheck Validation:

TangoMan Shoe Shell Microframework codebase is 100% shellcheck validated, it will not trigger any warnings or errors in your CI/CD pipeline.

  1. ⚑ Unit Test Coverage:

TangoMan Shoe Shell Microframework codebase is thoroughly covered with bashunit tests, ensuring code quality and reliability.

  1. ⚑ Command Grouping:

Prefix a comment with three pound signs (###) to organize related commands into categories for better help output.

  1. ⚑ Shoemaker Developement Tool:

Use shoemaker.sh tool to split a script file into its components, build a script from a "build.lst" file, generate Makefile or Markdown documentation from a shoe script.

  1. ⚑ Code Snippets:

In addition, TangoMan Shoe Shell Microframework includes a library of reusable code snippets that cover common use cases, saving you time and effort.

πŸ“‘ Documentation

  1. Full Shoe documentation can be found here: ./doc/shoe.md
  2. Documentation for shoemaker.sh can be found here: ./doc/shoemaker.md
  3. Documentation for entrypoint.sh can be found here: ./doc/entrypoint.md

πŸ“ Annotations

Prefix a comment with two pound signs (##) on the line above any function or variable to generate helpful documentation using the help command.

help prints commands in the order they appear in the script.

πŸ“ Sections

Group commands by categories by dividing them with a comment prefixed with three pound signs (###).

πŸ“ Docbloc

Change displayed title and description by editing docblock infos at the top of the actual file.

#/**
# * Script Title
# *
# * Place here script description.
# *
# * @author       "Your Name" <your@email.com>
# * @version      0.1.0
# * @link         https://your.website.com
# * @update-link  https://raw.githubusercontent.com/<USERNAME>/<REPOSITORY>/refs/heads/main/<SCRIPT>.sh
# */

πŸ”₯ Script Execution

πŸ”₯ Shorthands

TangoMan Shoe Shell Microframework allows you to use short versions (shorthands) for functions, flags, or options with a specific alphabetial character. Only the first occurrence of each letter can be used as a shorthand. These shorthands work based on the order they appear in the script.

$ sh shoe.sh h
# will return the same result as
$ sh shoe.sh hello
# but it will NOT execute "help"
$ sh shoe.sh hello -f
# is the same as
$ sh shoe.sh hello --foobar
# and
$ sh shoe.sh hello -w TangoMan
# is the same as
$ sh shoe.sh hello --who TangoMan

πŸ”₯ Constants

Any uppercase variable is considered a constant withing the framework, they cannot be directly modified from the command line but will be displayed in the help output.

πŸ”₯ Private Elements

Elements prefixed with an underscore are considered private. This means they are not visible in the help output and cannot be accessed or invoked from the command line.

#--------------------------------------------------
# Place your options after this line
#--------------------------------------------------

## Greeting recipient name /^[a-zA-Z-]+$/
who="World"

#--------------------------------------------------
# Place your flags after this line
#--------------------------------------------------

# (any variable set to "false" will convert to a flag at runtime)
## Example Flag
flag=false

#--------------------------------------------------
# Place your private constants after this line
#--------------------------------------------------

## This constant is private (it will not de displayed in the "help" output)
_PRIVATE="I'm a private constant"

#--------------------------------------------------
# Place your global variables after this line
#--------------------------------------------------

## This variable is private (it will not de displayed in the "help" output)
_global_variable="I'm a global variable"

#--------------------------------------------------
# Place your functions after this line
#--------------------------------------------------

## Greets a specified recipient a given number of times
hello() {
    # the recipient's name is provided with the "--who" option
    # the repetition count is provided with the "--count" option

    loop=0
    while [ "${loop}" -lt "${count}" ]; do
        echo "Hello ${who} !"
        loop=$((loop+1))
    done
}

## Prints flag status
flag_test() {
    if [ "${flag}" = true ]; then
        echo "Flag is on"

        return 0
    fi

    echo "Flag is off"
}

#--------------------------------------------------
# Place your private functions after this line
#--------------------------------------------------

# Private functions are prefixed with underscore, they cannot be called from the command line and will not appear in the help section.
_private() {
    echo "I'm private"
}

The example above will generate this result:

example

πŸ”Ž Validation

⚠️ Errors Messages

If you provide an unrecognized command, the script will display an error message like this: error_command

Similarly, an invalid option will trigger an error message: error_option

Illegal arguments will also result in an error message: error_validation

πŸ› Limitations of awk Regular Expressions

Due to inherent limitations in awk's regular expression capabilities, interval syntax (e.g., {1,3}) is not supported.

For instance, the following regular expression, will not function as expected:

## Local IP address /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/
ip=127.0.0.1

However, the following alternative, which uses the + quantifier to match one or more digits, will work correctly:

## Local IP address /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/
ip=127.0.0.1

πŸ› Limitations

πŸ› Function names

⚠️ Function names should not not contain dashes.

πŸ› Repetition

⚠️ Function will execute as many times as parameter is given, for example the command below will execute the hello function thee times.

$ sh shoe.sh hello hello hello

πŸ› Flags

⚠️ Last flag or option will overwrite variable value.

$ sh shoe.sh hello --who=foo --who=bar

The command above will return :

Hello bar !

πŸ“š Coding Conventions

πŸ“œ Style Guide

TangoMan Shoe Shell Microframework adheres to the Google Shell Style Guide, promoting consistent and readable scripts. You can find the guide here: https://google.github.io/styleguide/shellguide.html

🌐 Variable scope

POSIX shell has no variable scope limitations. To mitigate potential name collisions, variables in kernel use dunder syntax (double underscores). Ideally, to avoid variable-related issues entirely within Shoe private functions, it's recommended to use positional arguments. This involves assigning values to positional parameters using the set command (eg: set -- "$1" "$2" "$3").

πŸ”’ Positional arguments

To maintain a consistent developper experience, required arguments, particularly file paths, should be specified first. Optional arguments can be added after these.

πŸ’» Dependencies

TangoMan Shoe Shell Microframework requires the following dependencies:

  • Awk
  • Sed
  • Shell (ash supported)
  • Docker (dev)
  • bash_unit (dev)
  • Shellcheck (dev)

🀝 Contributing

Thank you for your interrest in contributing to TangoMan Shoe Shell Microframework.

Please review the code of conduct and contribution guidelines before starting to work on any features.

If you want to open an issue, please check first if it was not reported already before creating a new one.

πŸ“œ License

Copyrights (c) 2024 "Matthias Morin" <mat@tangoman.io>

License Distributed under the MIT license.

If you like TangoMan Shoe Shell Microframework please star, follow or tweet:

GitHub stars GitHub followers Twitter

... And check my other cool projects.

πŸ™ Acknowledgements

  • This script was inspired by Peter Forret bashew
  • bash_unit: bash unit testing enterprise edition framework (used for CI/CD)
  • shellcheck: a static analysis tool for shell scripts (used for CI/CD)

Releases

No releases published