Coder Social home page Coder Social logo

gnat / doit.sh Goto Github PK

View Code? Open in Web Editor NEW
13.0 2.0 0.0 136 KB

๐Ÿดโ€โ˜ ๏ธ Replaces your build system with 1 shell script.

License: MIT License

Shell 100.00%
build bash bash-script run task make shell infra infrastructure runner

doit.sh's Introduction

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.

doit.sh's People

Contributors

gnat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.