diff --git a/src/docker-osx-dev b/src/docker-osx-dev index 3154247..a78a06a 100755 --- a/src/docker-osx-dev +++ b/src/docker-osx-dev @@ -101,6 +101,28 @@ function assert_non_empty { fi } +# +# Usage: assert_mutually_exclusive VAR_NAME1 VAR_NAME2 ... VAR_NAMEn +# +# Asserts that at most one of $VAR_NAME1, $VAR_NAME2, ..., $VAR_NAMEn is defined +# +function assert_mutually_exclusive { + local found + while [[ $# > 0 ]]; do + local readonly var_name="$1" + if test -n "${!var_name+x}" ; then + if test -n "$found" ; then + log_error "$var_name conflicts with $found" + instructions + exit 1 + else + found=$var_name + fi + fi + shift + done +} + # # Usage: index_of VALUE ARRAY # @@ -871,6 +893,8 @@ function instructions { echo -e " -e, --exclude-path PATH\t\tExclude PATH while syncing. Behaves identically to rsync's --exclude parameter. May be specified multiple times. Default: $DEFAULT_EXCLUDES" echo -e " -c, --compose-file COMPOSE_FILE\tRead in this docker-compose file and sync any volumes specified in it. Default: $DEFAULT_COMPOSE_FILE" echo -e " -i, --ignore-file IGNORE_FILE\t\tRead in this ignore file and exclude any paths within it while syncing (see --exclude). Default: $DEFAULT_IGNORE_FILE" + echo -e " --only-dependencies\t\t\tDuring install, only install the homebrew dependencies. Useful if homebrew is needs to be run as a different user." + echo -e " --skip-dependencies\t\t\tDuring install, don't install the homebrew dependencies. Useful if homebrew is needs to be run as a different user." echo -e " -l, --log-level LOG_LEVEL\t\tSpecify the logging level. One of: $LOG_LEVELS. Default: ${DEFAULT_LOG_LEVEL}" echo -e " -h, --help\t\t\t\tPrint this help text and exit." echo -e @@ -1094,12 +1118,16 @@ function sync { # function install { log_info "Starting install of docker-osx-dev" - install_dependencies - init_docker_host - install_rsync_on_docker_host - add_docker_host - add_environment_variables - print_next_steps + if test -z "$SKIP_DEPENDENCIES" ; then + install_dependencies + fi + if test -z "$ONLY_DEPENDENCIES" ; then + init_docker_host + install_rsync_on_docker_host + add_docker_host + add_environment_variables + print_next_steps + fi } # @@ -1208,6 +1236,12 @@ function handle_command { DOCKER_MACHINE_NAME="$2" shift ;; + --only-dependencies) + ONLY_DEPENDENCIES=true + ;; + --skip-dependencies) + SKIP_DEPENDENCIES=true + ;; -h|--help) instructions exit 0 @@ -1222,6 +1256,8 @@ function handle_command { shift done + assert_mutually_exclusive "ONLY_DEPENDENCIES" "SKIP_DEPENDENCIES" + case "$cmd" in "$SYNC_COMMAND" | "$SYNC_ONLY_COMMAND" | "$WATCH_ONLY_COMMAND") configure_log_level "$log_level" diff --git a/test/docker-osx-dev.bats b/test/docker-osx-dev.bats index 13b7503..68d944f 100755 --- a/test/docker-osx-dev.bats +++ b/test/docker-osx-dev.bats @@ -303,6 +303,37 @@ load test_helper assert_success } +@test "assert_mutually_exclusive exits on conflicting variables" { + FOO=1 + BAR=2 + run assert_mutually_exclusive "FOO" "BAR" + assert_failure +} + +@test "assert_mutually_exclusive exits on conflicting but empty variables" { + FOO= + BAR= + run assert_mutually_exclusive "FOO" "BAR" + assert_failure +} + +@test "assert_mutually_exclusive doesn't exit without any variables" { + run assert_mutually_exclusive "FOO" "BAR" + assert_success +} + +@test "assert_mutually_exclusive doesn't exit with only the first variable" { + FOO=1 + run assert_mutually_exclusive "FOO" "BAR" + assert_success +} + +@test "assert_mutually_exclusive doesn't exit with only the last variable" { + BAR=2 + run assert_mutually_exclusive "FOO" "BAR" + assert_success +} + @test "env_is_defined returns true for USER variable being defined" { run env_is_defined "USER" assert_success