-
Notifications
You must be signed in to change notification settings - Fork 2
/
localenv
executable file
·93 lines (77 loc) · 2.12 KB
/
localenv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/bash
set -e
# Local Containerized Environment (localenv)
# Function to display usage information
usage() {
cat <<EOF
Usage: $0 <language> <script_path>
Options:
--help Display this help message and exit
Arguments:
<language> The programming language environment to use (e.g., python)
<script_path> The path to the script to be executed within the Docker container
EOF
exit 0
}
# Function to display an error message and usage information
error_usage() {
echo "Error: $1"
usage
}
# Function to check if Docker is installed
check_docker_installed() {
if ! command -v docker &>/dev/null; then
echo "Error: Docker is not installed. Please install Docker to use this script."
exit 1
fi
}
# Function to check if a file exists
check_file_exists() {
local file_to_check=$1
if [ ! -f "$file_to_check" ]; then
echo "Error: YAML file '$file_to_check' not found."
exit 1
fi
}
# Function to validate input arguments
check_input() {
local language=$1
local script_path=$2
if [[ -z "$language" || -z "$script_path" ]]; then
error_usage "Both language and script_path are required."
fi
}
# Variables
CONFIG_FILE_PATH="./config.yaml"
language=$1
script_path=$2
# Check for --help argument
if [[ "$language" == "--help" || "$script_path" == "--help" ]]; then
usage
fi
echo "Pre-flight checking"
# Validate input arguments
check_input "$language" "$script_path"
# Ensure Docker is installed
check_docker_installed
# Ensure the config file exists
check_file_exists "$CONFIG_FILE_PATH"
# Load and parse the YAML file using yq
image=$(yq e ".$language.image" "$CONFIG_FILE_PATH")
container_name=$(yq e ".$language.default_container_name" "$CONFIG_FILE_PATH")
base_cmd=$(yq e ".$language.base_command" "$CONFIG_FILE_PATH")
echo "Pre-flight checking - OK"
# Command to execute
echo "Execute $language script in Docker..."
docker run --rm \
--name "$container_name" \
-v "$(pwd)":/tmp/ \
-w /tmp/ \
"$image" $base_cmd $script_path
# Check the exit status of the Docker run command
if [ $? -eq 0 ]; then
echo "Script executed successfully."
else
echo "Script execution failed."
exit 1
fi