Skip to content

Commit

Permalink
Running installer with option -s makes it use sudo (#20)
Browse files Browse the repository at this point in the history
* Running installer with option -s makes it use sudo
* Add an example of using sudo to README
  • Loading branch information
bttk authored Feb 21, 2020
1 parent 9214bdd commit 77eedd2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ Think: `make install`. Someday: `bazel run install`.
bazel run //src/path/to/pkg:install_foo -c opt ~/bin
```

If you need to use `sudo` to install a file in a system directory:

* **Do not** run `sudo bazel`.
* Instead pass flag `-s` to the installer.

```shell
bazel run //src/path/to/pkg:install_foo -c opt -- -s /usr/local/bin
```

## See also

* [Examples](examples/README.md)
16 changes: 11 additions & 5 deletions installer/installer.bash.template
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#
# If you don't want --compilation_mode=opt, run:
# bazel run //path/to/your:installer_target -- -g /usr/local/bin
#
# To use sudo, run:
# bazel run //path/to/your:installer_target -- -s /usr/local/bin

set -o pipefail -o errexit -o nounset

Expand All @@ -48,7 +51,7 @@ function error() {
}

function usage() {
echo >&2 "Usage: bazel run ${INSTALLER_LABEL} [-c opt] [--] [-g] [--] /INSTALL_PREFIX"
echo >&2 "Usage: bazel run ${INSTALLER_LABEL} [-c opt] [--] [-g] [-s] [--] /INSTALL_PREFIX"
}

# Checks that all template variables have been substituted.
Expand Down Expand Up @@ -78,6 +81,7 @@ function check_sources() {
function install_file() {
local prefix="$1"
local i="$2"
local sudo="$3"
local source="${SOURCE_FILES[${i}]}"
local target="${TARGET_NAMES[${i}]}"
local target_dir
Expand All @@ -92,19 +96,21 @@ function install_file() {
fi

[[ -d "${target_dir}" ]] || \
mkdir --parents -- "${target_dir}"
$sudo mkdir --parents -- "${target_dir}"

install --mode="${target_mode}" \
$sudo install --mode="${target_mode}" \
-T -- "${source}" "${target_dir}/${target_name}"
}

function main() {
verify_templates

local g_flag=''
while getopts ':gh' flag; do
local s_flag=''
while getopts ':ghs' flag; do
case "${flag}" in
g) g_flag='true' ;; # Accept debug builds
s) s_flag='sudo' ;; # Run with sudo
h) usage; exit 0 ;;
*) error "Unexpected option '-${OPTARG}'" ;;
esac
Expand All @@ -125,7 +131,7 @@ function main() {
check_sources
local i
for ((i=0; i<${N_FILES}; i++)); do
install_file "${prefix}" "${i}"
install_file "${prefix}" "${i}" "${s_flag}"
done
}

Expand Down

0 comments on commit 77eedd2

Please sign in to comment.