Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
Signed-off-by: dubo-dubon-duponey <dubodubonduponey+github@pm.me>
  • Loading branch information
dubo-dubon-duponey committed Jan 6, 2020
1 parent af4f6d4 commit 8bc3da5
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 36 deletions.
11 changes: 6 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
language: bash

script:
- ./test.sh "$HOME"
matrix:
include:
- os: osx

addons:
homebrew:
packages:
- shellcheck

matrix:
include:
- os: osx
script:
- rm ~/.profile
- ./test.sh "$HOME"
51 changes: 29 additions & 22 deletions init
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bash
set -o errexit -o errtrace -o functrace -o nounset -o pipefail
# ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★
# Tarmac - a very small piece of shcript to wrap brew installation
# (c) 2019 Dubo Dubon Duponey <dubodubonduponey+github@pm.me>
Expand All @@ -12,12 +13,12 @@
############################
# generic Helpers
############################
TARMAC_COLOR_RED=1
TARMAC_COLOR_GREEN=2
TARMAC_COLOR_YELLOW=3
DP_COLOR_RED=1
DP_COLOR_GREEN=2
DP_COLOR_YELLOW=3

# Prefix a date to a log line and output to stderr
_tarmac_stamp(){
_dp_stamp(){
local color="$1"
local level="$2"
local i
Expand All @@ -30,39 +31,39 @@ _tarmac_stamp(){
[ "$TERM" ] && [ -t 2 ] && >&2 tput op
}

tarmac_logger_info(){
_tarmac_stamp "$TARMAC_COLOR_GREEN" "INFO" "$@"
dp_logger_info(){
_dp_stamp "$DP_COLOR_GREEN" "INFO" "$@"
}

tarmac_logger_warning(){
_tarmac_stamp "$TARMAC_COLOR_YELLOW" "WARNING" "$@"
dp_logger_warning(){
_dp_stamp "$DP_COLOR_YELLOW" "WARNING" "$@"
}

tarmac_logger_error(){
_tarmac_stamp "$TARMAC_COLOR_RED" "ERROR" "$@"
dp_logger_error(){
_dp_stamp "$DP_COLOR_RED" "ERROR" "$@"
}

# Enforce XCode command line tools to be here
while ! git --help 1>/dev/null 2>&1; do
tarmac_logger_warning "You need to install XCode command line tools. You should be prompted to do so now."
tarmac_logger_info "Hit enter when XCode installation completes."
dp_logger_warning "You need to install XCode command line tools. You should be prompted to do so now."
dp_logger_info "Hit enter when XCode installation completes."
read -r
done

tarmac_logger_info "XCode command line tools is properly installed."
dp_logger_info "XCode command line tools is properly installed."

if command -v brew 1>/dev/null; then
if [ ! "$TARMAC_FORCE_INSTALL" ]; then
tarmac_logger_info "Homebrew is installed on this system."
dp_logger_info "Homebrew is installed on this system."
exit
fi
fi

tarmac_logger_info "Selecting with xcode-select now:"
dp_logger_info "Selecting with xcode-select now:"
sudo xcode-select --switch /Library/Developer/CommandLineTools

# Have homebrew if not here
tarmac_logger_warning "You are missing homebrew. Going to install."
dp_logger_warning "You are missing homebrew. Going to install."

[ ! "${POSH_TOKEN+x}" ] && printf "%s" " > Your github api token for homebrew: " && read -r POSH_TOKEN
[ ! "${POSH_TMP+x}" ] && printf "%s" " > Where to hold temporary files for homebrew [default to $HOME/tmp]: " && read -r POSH_TMP
Expand All @@ -86,14 +87,14 @@ if [ ! -d "${POSH_BIN}/homebrew" ] || \
[ ! -d "${POSH_TMP}/log/homebrew" ] || \
[ ! -d "${POSH_CASK}/app" ] || \
[ ! -d "${POSH_CASK}/fonts" ]; then
tarmac_logger_error "Failed to create folders. Check your permissions."
dp_logger_error "Failed to create folders. Check your permissions."
exit 1
fi

curl -s -S -L https://github.com/Homebrew/brew/tarball/master 2>/dev/null | tar xz --strip 1 -C "${POSH_BIN}/homebrew"

if [ ! -f "${POSH_BIN}/homebrew/bin/brew" ]; then
tarmac_logger_error "Failed to retrieve homebrew from github. Network down?"
dp_logger_error "Failed to retrieve homebrew from github. Network down?"
exit 1
fi

Expand All @@ -114,7 +115,6 @@ export HOMEBREW_CACHE="\${POSH_TMP}/cache/homebrew"
export HOMEBREW_TEMP="\${POSH_TMP}/tmp/homebrew"
export HOMEBREW_LOGS="\${POSH_TMP}/log/homebrew"
export HOMEBREW_CASK_OPTS="--appdir=\$HOME/Applications/cask/app --fontdir=\$HOME/Applications/cask/fonts"
# shellcheck source=/dev/null
EOF

# shellcheck source=/dev/null
Expand All @@ -124,15 +124,22 @@ EOF
"${POSH_BIN}/homebrew/bin/brew" install bash-completion

touch "$HOME/.profile"
if ! grep -q posh_brew "$HOME/.profile"; then
if ! grep -q "posh_brew" "$HOME/.profile"; then
cat <<-EOF >> "$HOME/.profile"
# ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★
# ★ tarmac ★
# ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★
# shellcheck source=$HOME/.posh_brew
. "\$HOME/.posh_brew"
EOF
printf "%s" ". \"\$(brew --prefix)/etc/profile.d/bash_completion.sh\"" >> "$HOME/.posh_brew"
fi

tarmac_logger_info "Brew successfully installed. You should now source ~/.profile."
cat <<-EOF >> "$HOME/.posh_brew"
if [ ! "\$POSH_BREW_COMPLETION" ]; then
# shellcheck source=/dev/null
. "\$(brew --prefix)/etc/profile.d/bash_completion.sh"
export POSH_BREW_COMPLETION=true
fi
EOF

dp_logger_info "Brew successfully installed. You should now source ~/.profile."
13 changes: 8 additions & 5 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@ set -o errexit -o errtrace -o functrace -o nounset -o pipefail

# Linting
. tooling/helpers.sh

lint::shell init ./*.sh ./*/*.sh
>&2 printf "Linting successful\n"
logger::info "Linting successful"

# Installation test
expected="$*"

>&2 printf "Testing unattended forced install"
logger::info "Testing unattended forced install"

POSH_CASK="" POSH_TMP="" POSH_BIN="" POSH_TOKEN="" TARMAC_FORCE_INSTALL=true ./init

# Brew completion script will fail with our level of erroring, so, deactivate it.
export POSH_BREW_COMPLETION=true
# shellcheck source=/dev/null
. ~/.profile
. "$HOME"/.posh_brew

if [ "$(command -v brew)" != "$expected/Applications/bin/homebrew/bin/brew" ]; then
>&2 printf "brew installation failed - expected: %s - command -v brew: %s\n" "$expected" "$(command -v brew)"
logger::error "brew installation failed - expected: $expected - command -v brew: $(command -v brew)"
exit 1
fi

>&2 printf "Homebrew installation successful\n"
logger::info "Homebrew installation successful"
38 changes: 34 additions & 4 deletions tooling/helpers.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
#!/usr/bin/env bash
set -o errexit -o errtrace -o functrace -o nounset -o pipefail

COLOR_RED=1
COLOR_GREEN=2
COLOR_YELLOW=3

# Prefix a date to a log line and output to stderr
logger::stamp(){
local color="$1"
local level="$2"
local i
shift
shift
[ "$TERM" ] && [ -t 2 ] && >&2 tput setaf "$color"
for i in "$@"; do
>&2 printf "[%s] [%s] %s\\n" "$(date)" "$level" "$i"
done
[ "$TERM" ] && [ -t 2 ] && >&2 tput op
}

logger::info(){
logger::stamp "$COLOR_GREEN" "INFO" "$@"
}

logger::warning(){
logger::stamp "$COLOR_YELLOW" "WARNING" "$@"
}

logger::error(){
logger::stamp "$COLOR_RED" "ERROR" "$@"
}

lint::dockerfile(){
>&2 printf "Linting dockerfile:\n"
logger::info "Linting dockerfile:\n"
>&2 printf " > %s\n" "$@"
if ! hadolint "$@"; then
>&2 printf "Failed linting Dockerfile\n"
logger::error "Failed linting Dockerfile\n"
exit 1
fi
}

lint::shell(){
>&2 printf "Linting shellscript:\n"
logger::info "Linting shellscript:\n"
>&2 printf " > %s\n" "$@"
if ! shellcheck -a -x "$@"; then
>&2 printf "Failed shellchecking shell script\n"
logger::error "Failed shellchecking shell script\n"
exit 1
fi
}

0 comments on commit 8bc3da5

Please sign in to comment.