Skip to content

Commit ba8bbb6

Browse files
committed
start
1 parent cbe8d56 commit ba8bbb6

File tree

1 file changed

+82
-55
lines changed

1 file changed

+82
-55
lines changed

http

Lines changed: 82 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ STATUS_CODE_SUCCESS='\033[42;97;1m'
2626
STATUS_CODE_FAIL='\033[43;97;1m'
2727
STATUS_CODE_ERROR='\033[41;97;1m'
2828

29-
get_variable() {
29+
__get_config_value() {
3030
local local_curlrc="$CONFIG_FILE"
3131
local home_curlrc="$HOME/.curlrc"
3232
local value=""
@@ -39,66 +39,60 @@ get_variable() {
3939
value=$(grep '^variable host=' "$home_curlrc" | head -n1)
4040
fi
4141

42-
value=$(echo "$value" | sed 's/^variable host= *//;s/^"//;s/"$//;s/ *$//')
42+
value=$(echo "$value" | sed 's/^variable host=*//;s/^"//;s/"$//;s/ *$//')
4343

4444
echo -n "$value"
4545
}
4646

47-
configure() {
48-
if [[ -z "$1" ]]; then
49-
echo -e "${RED}Usage: http configure <base-url>${NC}" >&2
50-
exit 1
51-
fi
52-
53-
local host="$1"
54-
if ! grep -q "^variable host=" "$CONFIG_FILE" 2>/dev/null; then
55-
echo "variable host=$host" >> "$CONFIG_FILE"
56-
else
57-
if [[ "$OSTYPE" == "darwin"* ]]; then
58-
sed -i '' "s|^variable host=.*|variable host=$host|" "$CONFIG_FILE"
59-
else
60-
sed -i "s|^variable host=.*|variable host=$host|" "$CONFIG_FILE"
61-
fi
62-
fi
63-
echo -e "${GREEN} Host '$host' configured in '$CONFIG_FILE'${NC}"
64-
exit 0
65-
}
66-
67-
save_to_config_file() {
47+
__set_config_value() {
6848
if [[ ! -f "$CONFIG_FILE" ]]; then
6949
touch "$CONFIG_FILE"
7050
fi
71-
local key=$(echo "$1" | tr '[:lower:]' '[:upper:]')
51+
local key="$1"
7252
local value="$2"
7353

74-
# Use a more portable approach for sed
75-
if grep -q "^$key=" "$CONFIG_FILE"; then
76-
if [[ "$OSTYPE" == "darwin"* ]]; then
77-
sed -i '' "s|^$key=.*|$key=$value|" "$CONFIG_FILE"
54+
if [[ "$key" == variable* ]]; then
55+
if grep -q "^$key=" "$CONFIG_FILE"; then
56+
if [[ "$OSTYPE" == "darwin"* ]]; then
57+
sed -i '' "s|^$key=.*|$key=$value|" "$CONFIG_FILE"
58+
else
59+
sed -i "s|^$key=.*|$key=$value|" "$CONFIG_FILE"
60+
fi
7861
else
79-
sed -i "s|^$key=.*|$key=$value|" "$CONFIG_FILE"
62+
echo "$key=$value" >> "$CONFIG_FILE"
8063
fi
81-
else
82-
echo "$key=$value" >> "$CONFIG_FILE"
83-
fi
84-
}
85-
86-
logout() {
87-
if grep -q '^AUTHORIZATION=' "$CONFIG_FILE"; then
88-
if [[ "$OSTYPE" == "darwin"* ]]; then
89-
sed -i '' '/^AUTHORIZATION=/d' "$CONFIG_FILE"
64+
elif [[ "$key" == --* ]]; then
65+
local full_line="$key \"$value\""
66+
local escaped_key
67+
escaped_key=${key//[[\.*^$()+?{|]/\\&}
68+
69+
if grep -q "^$escaped_key " "$CONFIG_FILE"; then
70+
if [[ "$OSTYPE" == "darwin"* ]]; then
71+
sed -i '' "s|^$escaped_key .*|$full_line|" "$CONFIG_FILE"
72+
else
73+
sed -i "s|^$escaped_key .*|$full_line|" "$CONFIG_FILE"
74+
fi
9075
else
91-
sed -i '/^AUTHORIZATION=/d' "$CONFIG_FILE"
76+
echo "$full_line" >> "$CONFIG_FILE"
9277
fi
93-
echo -e "${GREEN}Authorization token removed.${NC}"
9478
else
95-
echo -e "${YELLOW}No authorization token found.${NC}"
79+
# Handle regular key=value format (uppercase key) - for backward compatibility
80+
local upper_key
81+
upper_key=$(echo "$key" | tr '[:lower:]' '[:upper:]')
82+
if grep -q "^$upper_key=" "$CONFIG_FILE"; then
83+
if [[ "$OSTYPE" == "darwin"* ]]; then
84+
sed -i '' "s|^$upper_key=.*|$upper_key=$value|" "$CONFIG_FILE"
85+
else
86+
sed -i "s|^$upper_key=.*|$upper_key=$value|" "$CONFIG_FILE"
87+
fi
88+
else
89+
echo "$upper_key=$value" >> "$CONFIG_FILE"
90+
fi
9691
fi
97-
exit 0
9892
}
9993

10094
# Function to colorize headers
101-
colorize_headers() {
95+
__colorize_headers() {
10296
while IFS= read -r line; do
10397
if [[ "$line" =~ ^([^:]+):[[:space:]]*(.*) ]]; then
10498
header_name="${BASH_REMATCH[1]}"
@@ -110,7 +104,7 @@ colorize_headers() {
110104
done
111105
}
112106

113-
beautify() {
107+
__beautify() {
114108
local json="$1"
115109

116110
if [[ -z "$json" ]]; then
@@ -132,6 +126,18 @@ beautify() {
132126
-e "s/^([[:space:]]*)null/\1${JSON_NULL_COLOR}null${COLOR_RESET}/g"
133127
}
134128

129+
configure() {
130+
if [[ -z "$1" ]]; then
131+
echo -e "${RED}Usage: http configure <base-url>${NC}" >&2
132+
exit 1
133+
fi
134+
135+
local host="$1"
136+
__set_config_value "variable host" "$host"
137+
echo -e "${GREEN} Host '$host' configured in '$CONFIG_FILE'${NC}"
138+
exit 0
139+
}
140+
135141
login() {
136142
ENDPOINT=$1
137143
if [[ "$ENDPOINT" != /* ]]; then
@@ -170,11 +176,11 @@ login() {
170176
fi
171177

172178
echo -e "${YELLOW}Attempting login...${NC}"
173-
beautify "$JSON_DATA"
179+
__beautify "$JSON_DATA"
174180
echo
175181

176182
# Use the request method for consistency
177-
RESPONSE=$(request "post" "$ENDPOINT" "$JSON_DATA")
183+
RESPONSE=$(__request "post" "$ENDPOINT" "$JSON_DATA")
178184

179185
# Extract only the body from the response (remove headers)
180186
SEPARATOR_LINE=$(echo "$RESPONSE" | grep -n "^[[:space:]]*$" | head -n1 | cut -d: -f1)
@@ -185,7 +191,7 @@ login() {
185191
BODY_CONTENT=$(echo "$RESPONSE" | sed -n '/^{/,$p')
186192
fi
187193

188-
beautify "$BODY_CONTENT"
194+
__beautify "$BODY_CONTENT"
189195

190196
# Extract token - try multiple possible fields
191197
if command -v jq >/dev/null 2>&1; then
@@ -199,7 +205,7 @@ login() {
199205
fi
200206

201207
if [[ -n "$TOKEN" && "$TOKEN" != "null" ]]; then
202-
save_to_config_file "AUTHORIZATION" "$TOKEN"
208+
__set_config_value "--header" "Authorization: Bearer $TOKEN"
203209
echo -e "${GREEN}Login successful. Token stored.${NC}"
204210
else
205211
echo -e "${RED}Login failed or token not found.${NC}"
@@ -208,7 +214,28 @@ login() {
208214
exit 0
209215
}
210216

211-
request() {
217+
logout() {
218+
# Remove both old and new format authorization entries
219+
local removed=false
220+
221+
if grep -q '^--header "Authorization:' "$CONFIG_FILE"; then
222+
if [[ "$OSTYPE" == "darwin"* ]]; then
223+
sed -i '' '/^--header "Authorization:/d' "$CONFIG_FILE"
224+
else
225+
sed -i '/^--header "Authorization:/d' "$CONFIG_FILE"
226+
fi
227+
removed=true
228+
fi
229+
230+
if [[ "$removed" == "true" ]]; then
231+
echo -e "${GREEN}Authorization token removed.${NC}"
232+
else
233+
echo -e "${YELLOW}No authorization token found.${NC}"
234+
fi
235+
exit 0
236+
}
237+
238+
__request() {
212239
local HTTP_VERB="$1"
213240
shift
214241

@@ -260,7 +287,7 @@ request() {
260287
;;
261288
esac
262289

263-
HTTP_HOST=$(get_variable)
290+
HTTP_HOST=$(__get_config_value)
264291
if [[ "$ENDPOINT" == /* ]]; then
265292
HTTP_LOCATION="${HTTP_HOST}${ENDPOINT}"
266293
HTTP_EXPAND_URL="{{host}}$ENDPOINT"
@@ -289,34 +316,34 @@ run() {
289316
local METHOD="$1"
290317
shift
291318

292-
RESPONSE=$(request "$METHOD" "$@")
319+
RESPONSE=$(__request "$METHOD" "$@")
293320

294321
SEPARATOR_LINE=$(echo "$RESPONSE" | grep -n "^[[:space:]]*$" | head -n1 | cut -d: -f1)
295322

296323
if [[ -n "$SEPARATOR_LINE" ]]; then
297324
if [[ "$VERBOSE_MODE" -eq 1 ]]; then
298-
echo "$RESPONSE" | head -n "$((SEPARATOR_LINE - 1))" | colorize_headers
325+
echo "$RESPONSE" | head -n "$((SEPARATOR_LINE - 1))" | __colorize_headers
299326
echo
300327
fi
301328

302329
BODY_CONTENT=$(echo "$RESPONSE" | tail -n +$((SEPARATOR_LINE + 1)))
303330
BODY_CONTENT=$(echo "$BODY_CONTENT" | sed '/^[[:space:]]*$/d')
304331

305332
if [[ -n "$BODY_CONTENT" ]]; then
306-
beautify "$BODY_CONTENT"
333+
__beautify "$BODY_CONTENT"
307334
fi
308335
else
309336
if [[ "$VERBOSE_MODE" -eq 1 ]]; then
310337
HEADERS=$(echo "$RESPONSE" | sed '/^{/,$d')
311338
if [[ -n "$HEADERS" ]]; then
312-
echo "$HEADERS" | colorize_headers
339+
echo "$HEADERS" | __colorize_headers
313340
echo
314341
fi
315342
fi
316343

317344
BODY=$(echo "$RESPONSE" | sed -n '/^{/,$p')
318345
if [[ -n "$BODY" ]]; then
319-
beautify "$BODY"
346+
__beautify "$BODY"
320347
else
321348
echo "$RESPONSE"
322349
fi

0 commit comments

Comments
 (0)