No more os detection (#41)

This commit is contained in:
Kevin van Zonneveld 2016-06-24 13:12:41 +02:00 committed by GitHub
parent 8bbba18f7e
commit 35b51072b3
9 changed files with 34 additions and 39 deletions

View File

@ -4,6 +4,7 @@
Released: Unreleased. [Commit log](https://github.com/kvz/bash3boilerplate/compare/v2.0.0...master)
- Remove OS detection altogether (#38, thx @zbeekman)
- Offer the main template for download as http://bash3boilerplate.sh/main.sh
- Better OS detection (#38, thx @moviuro)
- Improve README copy (#34, thx galaktos)

29
FAQ.md
View File

@ -63,7 +63,7 @@ __temp_file_name="${arg_t}"
## What is a magic variable?
The [magic variables](https://github.com/kvz/bash3boilerplate/blob/master/main.sh#L63) in `main.sh` are special in that they have a different value, depending on your environment. You can use `${__file}` to get a reference to your current script, `${__dir}` to get a reference to the directory it lives in. This is not to be confused with the location of the calling script that might be sourcing the `${__file}`, which is accessible via `${0}`, and the current directory of the administrator running the script, accessible via `$(pwd)`. Other magic variables are for instance `${__os}` which currently is limited to telling you wether you are on `OSX` and otherwise defaults to `Linux`.
The [magic variables](https://github.com/kvz/bash3boilerplate/blob/master/main.sh#L63) in `main.sh` are special in that they have a different value, depending on your environment. You can use `${__file}` to get a reference to your current script, `${__dir}` to get a reference to the directory it lives in. This is not to be confused with the location of the calling script that might be sourcing the `${__file}`, which is accessible via `${0}`, and the current directory of the administrator running the script, accessible via `$(pwd)`.
## How do I submit an issue report?
@ -118,3 +118,30 @@ We run automated tests to make sure that it will, here's proof for the following
This portability however does not mean we try to be compatible with
KornShell, Zsh, posh, yash, dash or other shells. We allow syntax that would explode if
you pasted it in anything but Bash 3 and up.
## How do I do Operating System detection?
We used to offer a magic `__os` variable, but quickly [discovered](https://github.com/kvz/bash3boilerplate/issues/38) that it would be hard
to create a satisfactory abstraction that is correct, covers enough use-cases,
and still has a relatively small footprint in `main.sh`.
For simple OS detection, we recommend using the `${OSTYPE}` variable available in Bash as
is demoed in [this stackoverflow post](http://stackoverflow.com/a/8597411/151666):
```bash
if [[ "${OSTYPE}" = "linux-gnu" ]]; then
echo "GNU Linux"
elif [[ "${OSTYPE}" = "darwin"* ]]; then
echo "Mac OSX"
elif [[ "${OSTYPE}" = "cygwin" ]]; then
echo "POSIX compatibility layer and Linux environment emulation for Windows"
elif [[ "${OSTYPE}" = "msys" ]]; then
echo "Lightweight shell and GNU utilities compiled for Windows (part of MinGW)"
elif [[ "${OSTYPE}" = "win32" ]]; then
echo "I'm not sure this can happen."
elif [[ "${OSTYPE}" = "freebsd"* ]]; then
echo "..."
else
echo "Unknown."
fi
```

View File

@ -44,7 +44,7 @@ dependency.
- Safe by default (break on error, pipefail, etc)
- Configuration by environment variables
- Simple command-line argument parsing that requires no external dependencies. Definitions are parsed from help info, so there is no duplication
- Helpful magic variables like `__file`, `__dir`, and `__os`
- Helpful magic variables like `__file` and `__dir`
- Logging that supports colors and is compatible with [Syslog Severity levels](http://en.wikipedia.org/wiki/Syslog#Severity_levels) as well as the [twelve-factor](http://12factor.net/) guidelines
## Who uses b3bp?

17
main.sh
View File

@ -27,21 +27,6 @@ __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
__base="$(basename ${__file} .sh)"
# The __os magic variable that b3bp offers is a dumbed down version of OSTYPE, aimed at
# simple use cases. For more specific os information use ${OSTYPE}, or uname directly.
if [[ "${OSTYPE:-}" == "linux"* ]]; then
__os="Linux"
elif [[ "${OSTYPE:-}" == "darwin"* ]]; then
__os="OSX"
elif [[ "${OSTYPE:-}" == "msys" ]]; then
# This could accomodate Git Bash but we're welcoming more input at https://github.com/kvz/bash3boilerplate/issues/32
__os="Windows"
elif [[ "${OSTYPE:-}" == *"bsd"* ]]; then
__os="BSD"
else
__os="b3bp_unsupported"
fi
# Define the environment variables (and their defaults) that this script depends on
LOG_LEVEL="${LOG_LEVEL:-6}" # 7 = debug -> 0 = emergency
NO_COLOR="${NO_COLOR:-}" # true = disable color. otherwise autodetected
@ -245,7 +230,7 @@ fi
info "__file: ${__file}"
info "__dir: ${__dir}"
info "__base: ${__base}"
info "__os: ${__os}"
info "OSTYPE: ${OSTYPE}"
info "arg_f: ${arg_f}"
info "arg_d: ${arg_d}"

View File

@ -29,11 +29,6 @@ __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
__base="$(basename ${__file} .sh)"
__root="$(cd "$(dirname "${__dir}")" && pwd)"
__os="Linux"
if [[ "${OSTYPE:-}" == "darwin"* ]]; then
__os="OSX"
fi
scenarios="${1:-$(ls ${__dir}/scenario/|egrep -v ^prepare$)}"
@ -55,11 +50,6 @@ else
fi
__node="$(which node)"
__os="linux"
if [[ "${OSTYPE}" == "darwin"* ]]; then
__os="darwin"
fi
__arch="amd64"
@ -96,8 +86,8 @@ for scenario in $(echo ${scenarios}); do
-e "s@{root}/node_modules/\.bin/node@{node}@g" "${curFile}" \
-e "s@{home}/build/{user}/fre{node}@{node}@g" "${curFile}" \
-e "s@${HOSTNAME}@{hostname}@g" "${curFile}" \
-e "s@${__os}@{os}@g" "${curFile}" \
-e "s@${__arch}@{arch}@g" "${curFile}" \
-e "s@${OSTYPE}@{OSTYPE}@g" "${curFile}" \
-e "s@OSX@{os}@g" "${curFile}" \
-e "s@Linux@{os}@g" "${curFile}" \
|| false

View File

@ -3,7 +3,7 @@ ACCPTST:STDIO_REPLACE_DATETIMES
{datetime} UTC [ info] __file: {root}/main.sh
{datetime} UTC [ info] __dir: {root}
{datetime} UTC [ info] __base: main
{datetime} UTC [ info] __os: {os}
{datetime} UTC [ info] OSTYPE: {OSTYPE}
{datetime} UTC [ info] arg_f: {tmpdir}/x
{datetime} UTC [ info] arg_d: 0
{datetime} UTC [ info] arg_v: 0

View File

@ -3,7 +3,7 @@ ACCPTST:STDIO_REPLACE_DATETIMES
{datetime} UTC [ info] __file: {root}/main.sh
{datetime} UTC [ info] __dir: {root}
{datetime} UTC [ info] __base: main
{datetime} UTC [ info] __os: {os}
{datetime} UTC [ info] OSTYPE: {OSTYPE}
{datetime} UTC [ info] arg_f: {tmpdir}/x
{datetime} UTC [ info] arg_d: 0
{datetime} UTC [ info] arg_v: 0

View File

@ -29,10 +29,6 @@ set -o pipefail
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
__base="$(basename ${__file} .sh)"
__os="Linux"
if [[ "${OSTYPE:-}" == "darwin"* ]]; then
__os="OSX"
fi
ghpages_repo=${GHPAGES_REPO:-"kvz/bash3boilerplate"}
ghpages_branch=${GHPAGES_BRANCH:-"gh-pages"}

View File

@ -27,10 +27,6 @@ set -o pipefail
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
__base="$(basename ${__file} .sh)"
__os="Linux"
if [[ "${OSTYPE:-}" == "darwin"* ]]; then
__os="OSX"
fi
# Offer the main template for download as http://bash3boilerplate.sh/main.sh
cp -v main.sh website/