Shell

Reusable shell functions for infrastructure automation and developer experience

  • Installation
  • Installation instructions

  • Download
  • Secure and reliable data retrieval from remote servers

  • File
  • Utilities for local file operations

  • Software
  • Tooling setup for local development and continuous integration environments

  • Log
  • Generic console and logging and helpers

  • Text
  • Constants for console text formatting

  • CI
  • Utilities for various CI/CD systems

  • Terraform
  • Wrappers and helpers for Terraform

  • Python
  • Wrappers and helpers for Python

All functions are tested on the following distributions

  • Amazon Linux 2
  • Debian 10
  • Debian 11
  • Ubuntu 20.04
  • Ubuntu 22.04

Kitchen Sink Usage Example



#!/usr/bin/env bash

set -eu -o pipefail

DIR="$(cd "$(dirname "$0")" ; pwd -P)"

SOLIDBLOCKS_SHELL_VERSION="v0.2.5"
SOLIDBLOCKS_SHELL_CHECKSUM="d07eb3250f83ae545236fdd915feca602bdb9b683140f2db8782eab29c9b2c48"

# self contained function for initial Solidblocks bootstrapping
function bootstrap_solidblocks() {
  local default_dir="$(cd "$(dirname "$0")" ; pwd -P)"
  local install_dir="${1:-${default_dir}/.solidblocks-shell}"

  local temp_file="$(mktemp)"

  curl -v -L "${SOLIDBLOCKS_BASE_URL:-https://github.com}/pellepelster/solidblocks/releases/download/${SOLIDBLOCKS_SHELL_VERSION}/solidblocks-shell-${SOLIDBLOCKS_SHELL_VERSION}.zip" > "${temp_file}"
  echo "${SOLIDBLOCKS_SHELL_CHECKSUM}  ${temp_file}" | sha256sum -c

  mkdir -p "${install_dir}" || true
  (
      cd "${install_dir}"
      unzip -o -j "${temp_file}" -d "${install_dir}"
      rm -f "${temp_file}"
  )
}

# makes sure all needed shell functions functions are available and all bootstrapped software is on the $PATH
function ensure_environment() {

  if [[ ! -d "${DIR}/.solidblocks-shell" ]]; then
    echo "environment is not bootstrapped, please run ./do bootstrap first"
    exit 1
  fi

  # included needed shell functions
  source "${DIR}/.solidblocks-shell/log.sh"
  source "${DIR}/.solidblocks-shell/text.sh"
  source "${DIR}/.solidblocks-shell/software.sh"

  # ensure $PATH contains all software downloaded via the `software_ensure_*` functions
  software_set_export_path
}

# bootstrap Solidblocks, and all other software needed using the software installer helpers from https://pellepelster.github.io/solidblocks/shell/software/
function task_bootstrap() {
  bootstrap_solidblocks
  ensure_environment
  software_ensure_terraform
}

# run the downloaded terraform version, ensure_environment ensures the downloaded versions takes precedence over any system binaries
function task_terraform {
  terraform -version
}

function task_log {
    log_info "info message"
    log_success "success message"
    log_warning "warning message"
    log_debug "debug message"
    log_error "error message"
}

function task_text {
    echo "${FORMAT_DIM}Dim${FORMAT_RESET}"
    echo "${FORMAT_UNDERLINE}Underline${FORMAT_RESET}"
    echo "${FORMAT_BOLD}Bold${FORMAT_RESET}"
    echo "${COLOR_RED}Red${COLOR_RESET}"
    echo "${COLOR_GREEN}Green${COLOR_RESET}"
    echo "${COLOR_YELLOW}Yellow${COLOR_RESET}"
    echo "${COLOR_BLACK}Black${COLOR_RESET}"
    echo "${COLOR_BLUE}Blue${COLOR_RESET}"
    echo "${COLOR_MAGENTA}Magenta${COLOR_RESET}"
    echo "${COLOR_CYAN}Cyan${COLOR_RESET}"
    echo "${COLOR_WHITE}White${COLOR_RESET}"
}

# provide some meaningful help using shell formatting from https://pellepelster.github.io/solidblocks/shell/text/
function task_usage {
  cat <<EOF
Usage: $0

  bootstrap             initialize the development environment
  terraform             run terraform
  log                   log some stuff
  text                  print soe fancy text formats
EOF
  exit 1
}

ARG=${1:-}
shift || true

# if we see the bootstrap command assume Solidshell is not yet initialized and skip environment setup
case "${ARG}" in
  bootstrap) ;;
  *) ensure_environment ;;
esac

case ${ARG} in
  bootstrap) task_bootstrap "$@" ;;
  terraform) task_terraform "$@" ;;
  log)       task_log "$@" ;;
  text)      task_text "$@" ;;
  *) task_usage ;;
esac