Repository navigation
Expand file tree
/
Copy pathcode-snatch
More file actions
executable file
Β·289 lines (249 loc) Β· 8.27 KB
/
Copy pathcode-snatch
File metadata and controls
executable file
Β·289 lines (249 loc) Β· 8.27 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/bin/bash
# code-snatch - Semantic code search CLI
# Author: manusiele254@gmail.com
set -e
# Configuration
GITHUB_USER="manusiele"
REPO_NAME="semantic-code-index"
DB_URL="https://raw.githubusercontent.com/${GITHUB_USER}/${REPO_NAME}/main/data/snippets_db.json"
CACHE_DIR="$HOME/.semantic-code-cli"
CACHE_FILE="$CACHE_DIR/snippets_db.json"
CACHE_AGE_HOURS=24
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m'
# Helper functions
error() {
echo -e "${RED}β Error: $1${NC}" >&2
exit 1
}
success() {
echo -e "${GREEN}β $1${NC}"
}
info() {
echo -e "${CYAN}βΉ $1${NC}"
}
warning() {
echo -e "${YELLOW}β $1${NC}"
}
print_header() {
echo -e "${BOLD}${BLUE}ββββββββββββββββββββββββββββββββββββββββββββββββββββββ${NC}"
echo -e "${BOLD}${BLUE}$1${NC}"
echo -e "${BOLD}${BLUE}ββββββββββββββββββββββββββββββββββββββββββββββββββββββ${NC}"
}
# Check dependencies
check_dependencies() {
local missing=()
for cmd in curl jq; do
if ! command -v $cmd &> /dev/null; then
missing+=($cmd)
fi
done
if [ ${#missing[@]} -gt 0 ]; then
error "Missing dependencies: ${missing[*]}\n Install: sudo apt install ${missing[*]} (Ubuntu) or brew install ${missing[*]} (macOS)"
fi
}
# Check if cache is fresh
is_cache_fresh() {
if [ ! -f "$CACHE_FILE" ]; then
return 1
fi
if [[ "$OSTYPE" == "darwin"* ]]; then
cache_time=$(stat -f %m "$CACHE_FILE")
else
cache_time=$(stat -c %Y "$CACHE_FILE")
fi
current_time=$(date +%s)
age=$((current_time - cache_time))
max_age=$((CACHE_AGE_HOURS * 3600))
[ $age -lt $max_age ]
}
# Download database
download_database() {
info "Downloading database from GitHub..."
echo " Source: $DB_URL"
mkdir -p "$CACHE_DIR"
if curl -sS -f -L "$DB_URL" -o "$CACHE_FILE.tmp"; then
mv "$CACHE_FILE.tmp" "$CACHE_FILE"
success "Database downloaded and cached"
local count=$(jq '. | length' "$CACHE_FILE" 2>/dev/null || echo "unknown")
echo " π¦ Total snippets: $count"
return 0
else
rm -f "$CACHE_FILE.tmp"
error "Failed to download database. Check your internet connection and repo setup."
return 1
fi
}
# Update cache
update_cache() {
local force=$1
if [ "$force" = "true" ] || ! is_cache_fresh; then
download_database
else
success "Using cached database (fresh)"
fi
}
# Search snippets
search_snippets() {
local query="$1"
if [ ! -f "$CACHE_FILE" ]; then
error "No cache found. Run: code-snatch --update"
fi
info "Searching for: \"$query\""
echo ""
local results=$(jq -c --arg query "$query" '
[
.[] |
select(
(.docstring // "" | ascii_downcase | contains($query | ascii_downcase)) or
(.function_name // "" | ascii_downcase | contains($query | ascii_downcase))
)
] | .[0:5]
' "$CACHE_FILE")
local count=$(echo "$results" | jq '. | length')
if [ "$count" -eq 0 ]; then
warning "No results found for \"$query\""
echo ""
echo "This might be because:"
echo " β’ Query is about a non-Python language (only Python is indexed)"
echo " β’ Query is too specific (try simpler keywords)"
echo " β’ Topic not in indexed repos (Django, Flask, FastAPI, Requests, CPython)"
echo ""
echo "Try simpler keywords like: 'async file', 'error handling', 'flask route'"
exit 0
fi
# Display results
echo "$results" | jq -r 'to_entries[] |
"\n\u001b[1;32mβββ Match \(.key + 1) βββ\u001b[0m",
"\u001b[1mFunction:\u001b[0m \(.value.function_name)",
"\u001b[1mSource:\u001b[0m \(.value.source_repo) - \(.value.filepath)",
"",
"\u001b[1mDescription:\u001b[0m",
" \(.value.docstring[:200])...",
"",
"\u001b[1mCode:\u001b[0m",
"\(.value.code)",
"",
"\u001b[2mπ \(.value.source_url // "N/A")\u001b[0m",
(if .value.llm_verified then "\u001b[32mβ LLM Verified\u001b[0m (Quality: \(.value.llm_analysis.quality_score)/10)" else "" end),
""
'
}
# Show cache info
show_info() {
print_header "Cache Information"
if [ -f "$CACHE_FILE" ]; then
local count=$(jq '. | length' "$CACHE_FILE" 2>/dev/null || echo "error")
local size=$(du -h "$CACHE_FILE" 2>/dev/null | cut -f1 || echo "unknown")
if [[ "$OSTYPE" == "darwin"* ]]; then
local modified=$(stat -f "%Sm" -t "%Y-%m-%d %H:%M:%S" "$CACHE_FILE" 2>/dev/null || echo "unknown")
else
local modified=$(stat -c "%y" "$CACHE_FILE" 2>/dev/null | cut -d'.' -f1 || echo "unknown")
fi
echo " π Location: $CACHE_FILE"
echo " π¦ Snippets: $count"
echo " πΎ Size: $size"
echo " π Updated: $modified"
if is_cache_fresh; then
success "Cache is fresh"
else
warning "Cache is stale (older than $CACHE_AGE_HOURS hours)"
echo " Run: code-snatch --update"
fi
else
warning "No cache found"
echo " Run: code-snatch --update"
fi
echo ""
}
# Clear cache
clear_cache() {
if [ -f "$CACHE_FILE" ]; then
rm "$CACHE_FILE"
success "Cache cleared"
else
warning "No cache to clear"
fi
}
# Show help
show_help() {
echo -e "${BOLD}code-snatch${NC} - Python Semantic Code Search"
echo -e ""
echo -e "${BOLD}SCOPE:${NC}"
echo -e " π Python code only (Django, Flask, FastAPI, Requests, stdlib)"
echo -e " β Other languages not supported"
echo -e ""
echo -e "${BOLD}USAGE:${NC}"
echo -e " code-snatch [OPTIONS] \"python code query\""
echo -e ""
echo -e "${BOLD}OPTIONS:${NC}"
echo -e " -u, --update Force update the database from GitHub"
echo -e " -i, --info Show cache information"
echo -e " -c, --clear Clear local cache"
echo -e " -h, --help Show this help message"
echo -e ""
echo -e "${BOLD}EXAMPLES:${NC}"
echo -e " code-snatch \"flask route with error handling\""
echo -e " code-snatch \"async database query\""
echo -e " code-snatch \"decorator for timing functions\""
echo -e " code-snatch --update"
echo -e ""
echo -e "${BOLD}INDEXED REPOSITORIES:${NC}"
echo -e " β’ django/django (Web framework)"
echo -e " β’ psf/requests (HTTP library)"
echo -e " β’ pallets/flask (Micro framework)"
echo -e " β’ fastapi/fastapi (Async framework)"
echo -e " β’ python/cpython (Python stdlib)"
echo -e ""
echo -e "${BOLD}CONFIGURATION:${NC}"
echo -e " Database: https://github.com/${GITHUB_USER}/${REPO_NAME}"
echo -e " Cache: ${CACHE_DIR}"
echo -e ""
echo -e "${BOLD}REQUIREMENTS:${NC}"
echo -e " β’ curl (for downloading database)"
echo -e " β’ jq (for JSON processing)"
echo -e ""
echo -e "${BOLD}TIPS:${NC}"
echo -e " β’ Database updates automatically every 24 hours"
echo -e " β’ Use simple, descriptive keywords"
echo -e " β’ Results are ranked by relevance"
}
# Main function
main() {
check_dependencies
case "${1:-}" in
-h|--help)
show_help
exit 0
;;
-u|--update)
update_cache true
exit 0
;;
-i|--info)
show_info
exit 0
;;
-c|--clear)
clear_cache
exit 0
;;
"")
error "No search query provided.\n Usage: code-snatch \"your query\"\n Help: code-snatch --help"
;;
-*)
error "Unknown option: $1\n Use --help for usage information"
;;
*)
update_cache false
search_snippets "$*"
;;
esac
}
main "$@"