-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleax_cli
More file actions
executable file
·151 lines (129 loc) · 3.81 KB
/
leax_cli
File metadata and controls
executable file
·151 lines (129 loc) · 3.81 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
CONFIG_FILE="$HOME/.leaxrc"
OS="$(uname -s)"
# ANSI color codes
GREEN="\033[38;5;49m"
YELLOW="\033[38;5;214m"
BLUE="\033[38;5;31m"
RED="\033[38;5;196m"
RESET="\033[0m"
# Handle 'leax configure' command
if [ "$1" = "configure" ]; then
echo ""
read -p "$(echo -e "${YELLOW} - ${RESET}Enter your Mistral API key: ")" api_key
if [ -z "$api_key" ]; then
echo -e " ${RED}×${RESET} Error: API key cannot be empty"
exit 1
fi
echo "MISTRAL_API_KEY=$api_key" > "$CONFIG_FILE"
chmod 600 "$CONFIG_FILE"
echo ""
echo -e "${GREEN} ✓ ${RESET} Configuration saved to $CONFIG_FILE"
echo -e "${GREEN} ✓ ${RESET} You can now use leax from anywhere!"
echo -e "${GREEN} ✓ ${RESET} Usage: leax ./<executable> [args...]"
echo ""
exit 0
fi
# Normal leax usage
if [ $# -lt 1 ]; then
echo "Usage: leax <executable> [args...]"
echo " leax configure"
echo ""
echo "Examples:"
echo " leax ./my_program"
echo " leax ./my_program arg1 arg2"
echo " leax configure # Configure Mistral API key"
exit 1
fi
EXECUTABLE="$1"
shift
ARGS=("$@")
# Verify executable exists
if [ ! -f "$EXECUTABLE" ]; then
echo "Error: Executable '$EXECUTABLE' not found"
exit 1
fi
if [ ! -x "$EXECUTABLE" ]; then
echo "Warning: '$EXECUTABLE' is not executable, attempting to make it executable..."
chmod +x "$EXECUTABLE"
fi
# Get absolute path of current directory
CURRENT_DIR=$(pwd)
# Find .env file by traversing up the directory tree (fallback)
find_env_file() {
local dir="$1"
local levels=0
while [ "$levels" -lt 5 ]; do
if [ -f "$dir/.env" ]; then
if grep -q "MISTRAL_API_KEY" "$dir/.env" 2>/dev/null; then
echo "$dir/.env"
return 0
fi
fi
local parent=$(dirname "$dir")
if [ "$parent" = "$dir" ]; then
break
fi
dir="$parent"
levels=$((levels + 1))
done
return 1
}
# Load API key from config
load_api_key() {
if [ -f "$CONFIG_FILE" ]; then
export $(grep -v '^#' "$CONFIG_FILE" | xargs)
else
local env_file
env_file=$(find_env_file "$CURRENT_DIR")
if [ -n "$env_file" ]; then
export $(grep -v '^#' "$env_file" | grep 'MISTRAL_API_KEY' | xargs)
else
echo "Error: No Mistral API key configured"
echo ""
echo "Please run: leax configure"
echo ""
echo "Or create a .env file with:"
echo " MISTRAL_API_KEY=your_api_key"
echo ""
exit 1
fi
fi
}
if [ "$OS" = "Linux" ]; then
# ── Linux: run Python directly ──
LEAX_SRCS="$HOME/.local/share/leax/srcs"
if [ ! -f "$LEAX_SRCS/vex.py" ]; then
echo "Error: Leax sources not found at $LEAX_SRCS"
echo "Please run 'make install' from the Leax repository"
exit 1
fi
load_api_key
python3 "$LEAX_SRCS/vex.py" "$EXECUTABLE" "${ARGS[@]}"
else
# ── macOS: run via Docker ──
# Determine env source for --env-file
if [ -f "$CONFIG_FILE" ]; then
ENV_SOURCE="$CONFIG_FILE"
else
ENV_FILE=$(find_env_file "$CURRENT_DIR")
if [ -n "$ENV_FILE" ]; then
ENV_SOURCE="$ENV_FILE"
else
echo "Error: No Mistral API key configured"
echo ""
echo "Please run: leax configure"
echo ""
echo "Or create a .env file with:"
echo " MISTRAL_API_KEY=your_api_key"
echo ""
exit 1
fi
fi
docker run -it --rm \
--cap-add=SYS_PTRACE --security-opt seccomp=unconfined \
--env-file "$ENV_SOURCE" \
-v "$CURRENT_DIR:/work" \
-w /work \
leax /app/srcs/vex.py "$EXECUTABLE" "${ARGS[@]}" 2>/dev/null
fi