diff options
| author | Olcan <[email protected]> | 2025-04-22 13:51:50 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-04-22 13:51:50 -0700 |
| commit | 5e34d9e2764da1251eb777f4f32e5cf1804ee949 (patch) | |
| tree | a6882af2cbf32315044a65f68acf437d8492efbd /scripts/sandbox_command.sh | |
| parent | 60fc979332572a8ae2f7097cc634e26cb4836269 (diff) | |
Refactor_sandbox_command (#121)
Diffstat (limited to 'scripts/sandbox_command.sh')
| -rwxr-xr-x | scripts/sandbox_command.sh | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/scripts/sandbox_command.sh b/scripts/sandbox_command.sh new file mode 100755 index 00000000..372d0d50 --- /dev/null +++ b/scripts/sandbox_command.sh @@ -0,0 +1,63 @@ +#!/bin/bash +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# usage: scripts/sandbox_command.sh [-q] +# -q: quiet mode (do not print command, just exit w/ code 0 or 1) + +set -euo pipefail + +# parse flags +QUIET=false +while getopts ":q" opt; do + case ${opt} in + q ) QUIET=true ;; + \? ) echo "Usage: $0 [-q]" + exit 1 + ;; + esac +done +shift $((OPTIND - 1)) + + +# if GEMINI_CODE_SANDBOX is not set, try to source .env in case set there +if [ -z "${GEMINI_CODE_SANDBOX:-}" ]; then source .env; fi + +# if GEMINI_CODE_SANDBOX is still not set, then exit immediately w/ code 1 +if [ -z "${GEMINI_CODE_SANDBOX:-}" ]; then exit 1; fi + +# lowercase GEMINI_CODE_SANDBOX +GEMINI_CODE_SANDBOX=$(echo "${GEMINI_CODE_SANDBOX:-}" | tr '[:upper:]' '[:lower:]') + +# if GEMINI_CODE_SANDBOX is set to 1 or true, then try to use docker or podman +if [[ "${GEMINI_CODE_SANDBOX:-}" =~ ^(1|true)$ ]]; then + if command -v docker &> /dev/null; then + if [ "$QUIET" = false ]; then echo "docker"; fi + exit 0 + elif command -v podman &> /dev/null; then + if [ "$QUIET" = false ]; then echo "podman"; fi + exit 0 + else + echo "ERROR: install docker or podman or specify command in GEMINI_CODE_SANDBOX" >&2 + exit 1 + fi +fi + +if ! command -v "$GEMINI_CODE_SANDBOX" &> /dev/null; then + echo "ERROR: missing sandbox command '$GEMINI_CODE_SANDBOX' (from GEMINI_CODE_SANDBOX)" >&2 + exit 1 +fi + +if [ "$QUIET" = false ]; then echo "$GEMINI_CODE_SANDBOX"; fi +exit 0 |
