-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbott.sh
executable file
·136 lines (131 loc) · 3.58 KB
/
bott.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
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
if [ -z "$BOTT_EXECUTABLE_DIR" ]; then
export BOTT_EXECUTABLE_DIR="$BOTT_DIR"
fi
alias bott_="$BOTT_EXECUTABLE_DIR/bott"
function bott_init() {
export bott_last_run_executed_code=""
export bott_last_run_output=""
export bott_last_run_exit_code=0
export bott_last_query_response=""
export bott_last_query_exit_code=0
export bott_last_debug_response=""
export bott_last_debug_exit_code=0
export bott_last_other_response=""
export bott_last_other_exit_code=0
export bott_context=""
}
function bott_execute_code() {
bott_last_run_executed_code=$1
bott_last_run_output=$(eval "$1" 2>&1)
bott_last_run_exit_code=$?
if [ $bott_last_run_exit_code -ne 0 ]; then
bott_last_run_output="${bott_last_run_output/"(eval):1: "/""}"
fi
return $bott_last_run_exit_code
}
function bott_get_distro() {
local d=""
if [[ -f /etc/os-release ]]; then
# On Linux systems
source /etc/os-release
d=$ID
else
# On systems other than Linux (e.g. Mac or FreeBSD)
d=$(uname)
fi
case $d in
"raspbian")
echo Raspbian
;;
"fedora")
echo Fedora
;;
"ubuntu")
echo Ubuntu
;;
"Darwin")
echo macOS
;;
esac
}
function bott_get_shell() {
if [ -z "${SHELL}" ]; then
_SHELL=$(ps -o args= -p "$$" | cut -d- -f2)
else
_SHELL=$SHELL
fi
echo "$_SHELL"
}
function bott!() {
local subcommand=$1
case $subcommand in
"run")
local code_to_exec="${*/"run"/""}"
bott_execute_code $code_to_exec
echo $bott_last_run_output
return "$bott_last_run_exit_code"
;;
"query")
local distro="$(bott_get_distro)"
local shell="$(bott_get_shell)"
local query="${*/"query"/""}"
local code_to_exec="bott_ query -d \"$distro\" -s \"$shell\" -q \"$query\""
bott_last_query_response=$(eval "$code_to_exec")
bott_last_query_exit_code=$?
if [ $bott_last_query_exit_code -ne 0 ]; then
echo "$bott_last_query_response"
return $bott_last_query_exit_code
fi
local answer=$(echo "$bott_last_query_response" | awk -v RS="<ANSWER>" -v ORS="" 'NR>1{gsub(/<\/ANSWER>.*/, ""); print}')
local context=$(echo "$bott_last_query_response" | awk -v RS="<CONTEXT>" -v ORS="" 'NR>1{gsub(/<\/CONTEXT>.*/, ""); print}')
if [ -z $answer ]; then
echo "Didnt get your question. Please try asking only questions related to bash commands"
return 1
fi
bott_context="$context"
echo "Answer: $answer"
if bott_ confirm -q "Do you want to run the command?"; then
bott_execute_code $answer
echo $bott_last_run_output
return "$bott_last_run_exit_code"
fi
;;
"clear")
bott_init
echo "session cleared"
;;
"debug")
local distro="$(bott_get_distro)"
local shell="$(bott_get_shell)"
local code_to_exec="bott_ debug -d \"$distro\" -s \"$shell\""
bott_last_debug_response=$(eval "$code_to_exec")
bott_last_debug_exit_code=$?
if [ $bott_last_debug_exit_code -ne 0 ]; then
echo "Didnt get your question. Please try asking only questions related to bash commands"
return 1
fi
local answer=$(echo "$bott_last_debug_response" | awk -v RS="<ANSWER>" -v ORS="" 'NR>1{gsub(/<\/ANSWER>.*/, ""); print}')
echo "Answer: $answer"
;;
"config")
local code_to_exec="bott_ $*"
bott_last_other_response=$(eval "$code_to_exec" 2>&1)
bott_last_other_exit_code=$?
echo "$bott_last_other_response"
local query="${*/"config"/""}"
if [ $bott_last_other_exit_code -eq 0 ] && [[ ! "$query" =~ ^\ get ]]; then
bott_init
echo "config set and session cleared"
fi
return $bott_last_other_exit_code
;;
*)
local code_to_exec="bott_ $*"
bott_last_other_response=$(eval "$code_to_exec" 2>&1)
bott_last_other_exit_code=$?
echo "$bott_last_other_response"
return $bott_last_other_exit_code
;;
esac
}
bott_init