-
Notifications
You must be signed in to change notification settings - Fork 0
/
heycli_client.sh
96 lines (78 loc) · 2.46 KB
/
heycli_client.sh
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
# By heycli.com
# License: Any kind of usage of this file is allowed
# Install jq using apt-get or yum depending on the detected OS
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
# Debian/Ubuntu-based systems
apt-get update
apt-get install -y jq
elif [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
brew install jq
else
# Unsupported OS
echo "Error: Unsupported OS"
exit 1
fi
PROD_API_ENDPOINT="https://api.heycli.com"
LOCAL_API_ENDPOINT="http://127.0.0.1:8080"
API_END_POINT=$PROD_API_ENDPOINT
function hey() {
if [[ $1 == "setup" && $# -eq 1 ]];
then
history | cut -c 8- > ~/.hist.txt
HISTFILE=~/.hist.txt
curl \
-F "curr_dir=$current_dir" \
-H "Authorization:$HEYCLI_API_KEY" \
-F "histfile=@$HISTFILE" \
"$API_END_POINT/setup"
return
fi
history | tail -n 50 | cut -c 8- > ~/.recent_hist.txt
recent_history=~/.recent_hist.txt
cmd="$*"
current_dir="$(pwd)"
result=$(curl -s \
-F "command=$cmd" \
-F "recent_history=@$recent_history" \
-H "Authorization:$HEYCLI_API_KEY" \
-F "curr_dir=$current_dir" \
"$API_END_POINT/heycli")
# echo $result
command_line=$(echo $result | jq -r '.command')
answer=$(echo $result | jq -r '.original_text')
echo "Answer: $answer"
if [[ -n "$command_line" ]]; then
echo -n "Execute command ($command_line)? (type \"yes\" to approve): " # prompt user to confirm choice
read choice # read user input
case "$choice" in
[Yy][Ee][Ss]) # if user enters "yes"
eval "$command_line" # execute the command
;;
*) # if user enters anything else
echo "Command not executed."
;;
esac
fi
}
function save_output {
# run the command and capture its output
# output=$($@ 2>&1 | tee ~/.command_outputs)
output=$($@ 2>&1 | tee /dev/tty)
command="$*"
current_dir="$(pwd)"
curl -s \
-F "command=$command" \
-F "output=$output" \
-H "Authorization:$HEYCLI_API_KEY" \
-F "curr_dir=$current_dir" \
"$API_END_POINT//send_context"
}
# alias the function to every command
alias ls='save_output ls'
alias sudo='save_output sudo'
alias ssh='save_output ssh'
alias ping='save_output ping'
alias python3='save_output python3'
alias python='save_output python'
# add more aliases for other commands as needed