Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fixes for Darwin w/Homebrew #198

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
.PHONY : build, test, shellcheck, tag
SHELL=/bin/bash
TMP_DIR:=$(shell mktemp -d)
TMP_DIR?="/tmp/hypriot-flash"

default: build

.PHONY: build
build:
docker build -t flash .

TMP_DIR ::= $(shell mktemp -d)
TMP_DIR ?= "/tmp/hypriot-flash"

.PHONY: test
test: build
mkdir -p $(TMP_DIR)
docker run --privileged -ti -v $(shell pwd):/code -v $(TMP_DIR):/tmp -e CIRCLE_TAG flash npm test
rm -rf $(TMP_DIR)

.PHONY: install
install:
( \
VERSION=$(shell curl -s https://api.github.com/repos/hypriot/flash/releases/latest | grep tag_name | cut -d\" -f4); \
curl -L https://github.com/hypriot/flash/releases/download/$$VERSION/flash >flash-$$VERSION; \
chmod +x flash-$$VERSION; \
sudo mv flash-$$VERSION /usr/local/bin/flash; \
)

.PHONY: shellcheck
shellcheck:
docker run --rm -ti -v $(shell pwd):/mnt -w /mnt koalaman/shellcheck -s bash flash

.PHONY: tag
tag:
git tag ${TAG}
git push origin ${TAG}
44 changes: 32 additions & 12 deletions flash
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
# Linux initial version by Matt Williams - [email protected]
# MIT License

# Allow user control of which program is used for writing
# SD card image.
DD=${DD:-/bin/dd}

set -eo pipefail

error()
Expand Down Expand Up @@ -142,7 +146,26 @@ fi
case "${OSTYPE}" in
darwin*)
size_opt="-f %z"
bs_size=1m
# 'dd' may be one of several; just try the options for size
# to see which one is first in PATH.
if "${DD}" bs=1m count=1 if=/dev/zero of=/dev/null 2>/dev/null; then
bs_size=1m
elif "${DD}" bs=1M count=1 if=/dev/zero of=/dev/null 2>/dev/null; then
bs_size=1M
else
echo "could not determine dd blocksize: is dd installed?"
echo "(hint: export environment variable 'DD' with path to 'dd' program)"
exit 1
fi

# Show in the standard output the devices that are a likely
# destination for the tool to write an image to.
show_devices() {
diskutil list | grep --color=never FDisk_partition_scheme | awk 'NF>1{print $NF}'
}
get_first_device() {
show_devices | head -n 1
}

# Check that the system has all the needed binaries/requirements in place
check_requirements() {
Expand Down Expand Up @@ -171,24 +194,19 @@ case "${OSTYPE}" in
# return _RET: the name of the device to use
autodetect_device() {
set +e
_RET=/dev/$(diskutil list | grep --color=never FDisk_partition_scheme | awk 'NF>1{print $NF}')
# Handle situation where multiple devices are found by taking the first one.
_RET=/dev/$(get_first_device)

if [ "${_RET}" == "" ] || [ "${_RET}" == "/dev/" ]; then
echo "No SD card found. Please insert SD card, I'll wait for it..."
while [ "${_RET}" == "" ] || [ "${_RET}" == "/dev/" ]; do
sleep 1
_RET=/dev/$(diskutil list | grep --color=never FDisk_partition_scheme | awk 'NF>1{print $NF}')
_RET=/dev/$(get_first_device)
done
fi
set -e
}

# Show in the standard output the devices that are a likely
# destination for the tool to write an image to.
show_devices() {
diskutil list | grep --color=never FDisk_partition_scheme | awk 'NF>1{print $NF}'
}

# Check that the target device can be written. It will return 0 in
# this case and 1 if it is not writable
#
Expand All @@ -200,7 +218,7 @@ case "${OSTYPE}" in
_RET=1
return
fi
readonlymedia=$(diskutil info "$disk" | grep --color=never "Read-Only Media\|Media Read-Only" | awk 'NF>1{print $NF}')
readonlymedia=$(diskutil info "$disk" | grep --color=never "Read-Only Media\|Media Read-Only" | awk 'NF>1 {print $NF; exit 0}')
if [[ $readonlymedia == "No" ]] ; then
_RET=1
else
Expand Down Expand Up @@ -669,11 +687,11 @@ if [[ -z $CONFIGURE_ONLY ]] ; then
if [[ -x $(command -v pv) ]]; then
sudo_prompt
size=$(/usr/bin/stat "$size_opt" "${image}")
pv -s "${size}" < "${image}" | sudo dd bs=$bs_size "of=${rawdisk}"
pv -s "${size}" < "${image}" | sudo "${DD}" bs=$bs_size "of=${rawdisk}"
else
echo "No 'pv' command found, so no progress available."
echo "Press CTRL+T if you want to see the current info of dd command."
sudo dd bs=$bs_size "if=${image}" "of=${rawdisk}"
sudo "${DD}" bs=$bs_size "if=${image}" "of=${rawdisk}"
fi

wait_for_disk "${disk}"
Expand Down Expand Up @@ -795,3 +813,5 @@ else
play_warn
echo "Something went wrong."
fi

# vim: set ts=2 sw=2 tw=0 et :