Skip to content

gnat/doit.sh

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Build without bullshit.

party-palpatine

πŸ΄β€β˜ οΈ Anti-features

  • No installation
  • No dependencies
  • No overhead

Replace your convoluted build system with vanilla bash.

πŸ‘οΈ What does it look like?

#!/bin/bash
set -euo pipefail # Error handling: -e stops on errors. -u stops on unset variables. -o pipefail stops pipelines on fail: https://mobile.twitter.com/b0rk/status/1314345978963648524

build() {
  echo "I am ${FUNCNAME[0]}ing" # ▢️ doit build πŸ‘οΈ I am building
}

deploy() {
  echo "I am ${FUNCNAME[0]}ing with args $1 $2 $3" # ▢️ doit deploy a b c πŸ‘οΈ I am deploying with args a b c
}

clean() { echo "I am ${FUNCNAME[0]}ing in just one line." ;}

required() {
  which docker || { echo "Error: Docker is not installed"; exit 1 ;}
}

all() {
  required && clean && build && deploy a b c # Continues chain on success.
}

[ "$#" -gt 0 ] || echo "Usage: doit task [options]" && "$@" # 🟒 DO IT!

Save as doit.sh use chmod +x ./doit.sh

Do task: ./doit.sh build

♻️ Alias setup

  • echo "alias doit='./doit.sh'" >> ~/.bashrc
  • Open new shell.
  • You can now use doit

βœ‚οΈ Snippets

Show help

[ "$#" -gt 0 ] && { "$@"; } || echo -e "Usage: $0 task [options]\nTasks:"; printf "\t%s\n" $(compgen -A function) # 🟒 DO IT!

Show fancy help with comments

help() { # Show help message.
  echo -e "Usage: $0 task [options]\nTasks:"
  for func in $(compgen -A function | grep -E '^_' -v); do printf "\t$func \t \e[92m $(grep "^$func()" $0 | grep -oP '(?<=# ).*')" ; printf " \e[0m \n"; done | column -t -s $'\t'
}

[ "$#" -gt 0 ] && { "$@"; } || help;  # 🟒 DO IT!

Local script include

. $(dirname $0)/helpers.sh

Online scripts

Run script from URL, including public or private github repositories!

required() {
  which docker || { echo "Error: Docker is not installed"; exit 1 ;}
  $0 docker/install_check # Easily run an online script.
}

online() {
  echo "🌐 Find online? (y/n)"; read CHOICE && [[ $CHOICE = [yY] ]] || (echo "Cancelled"; exit 1)
  { curl -fsSL https://raw.githubusercontent.com/gnat/doit/main/extra/$1.sh | bash --login -s -- ${@:2}; } && exit 1 || echo "Not found: '$1'"
}

[ "$#" -gt 0 ] || echo -e "Usage: $0 command [options]" && { "$@" || online "$@"; } # 🟒 DO IT!

βœ‚οΈ Online Snippets

# Run online script.
curl -fsSL https://raw.githubusercontent.com/gnat/doit/main/extra/helpers.sh | bash

# Import online script.
. <(curl -fsSL https://raw.githubusercontent.com/gnat/doit/main/extra/helpers.sh)

Use private github

online() {
  URL="https://YOUR_PRIVATE_GITHUB/main/$1.sh"
  echo "🌐 Find online? (y/n) ($URL) "; read CHOICE && [[ $CHOICE = [yY] ]] || (echo "Cancelled"; exit 1)
  { curl -fsSL "$URL -H 'Authorization: Token YOUR_PRIVATE_ACCESS_CODE'" | bash --login -s -- ${@:2}; } ||
  echo "Not found: '$1'"
}

Use private github, with fallbacks

online() {
  URLS=(
    "https://YOUR_PRIVATE_GITHUB/main/$1.sh -H 'Authorization: Token YOUR_PRIVATE_ACCESS_CODE'"
    "https://raw.githubusercontent.com/gnat/doit/main/extra/$1.sh"
    "https://raw.githubusercontent.com/gnat/doit_again/main/extra/$1.sh"
  )
  for URL in "${URLS[@]}"; do
    echo "🌐 Find online? (y/n) (${URL%% *}) "; read CHOICE && [[ $CHOICE = [yY] ]] || { echo "Skipping"; continue; }
    { curl -fsSL "$URL" | bash --login -s -- ${@:2}; } && exit # Success
  done
  echo "Not found: '$1'"
}

πŸ“š Helpful references

πŸ” Technical FAQ

For online scripts, why are read prompts not working ?

  • curl https://URL/script.sh | bash breaks some user input prompts such as read. For workarounds, see examples/choices. If you do not want to use a different convention for calling online scripts, you may consider passing script arguments only.

For online scripts, why bash --login ?

  • This simulates a user session, and is required to install certain apps such as Rootless Docker.

✨ Special thanks

πŸ‘€ Related tools

  • Task a task runner / simpler Make alternative written in Go
  • Robo Simple Go / YAML-based task runner for the team.
  • godo godo is a task runner and file watcher for golang in the spirit of rake, gulp. It has kind of same name.
  • just A better make in rust.