Skip to content

Commit 782ad0b

Browse files
author
Test
committed
feat: enhance configuration management and improve testing setup
1 parent 959d198 commit 782ad0b

File tree

14 files changed

+118
-348
lines changed

14 files changed

+118
-348
lines changed

.giv/config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ GIV_PROJECT_DESCRIPTION="cli tool"
66
GIV_PROJECT_URL="giv is a CLI utility for generating changelogs and summaries."
77
GIV_API_URL="http://localhost:11434/v1/chat/completions"
88
GIV_API_MODEL="devstral"
9+
GIV_API_KEY="ollama"
910
GIV_INITIALIZED="true"

src/commands/changelog.sh

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
# Allow test harness to inject mock functions (for bats)
2-
if [ -n "$GIV_TEST_MOCKS" ] && [ -f "$GIV_TEST_MOCKS" ]; then
3-
. "$GIV_TEST_MOCKS"
4-
fi
51
#!/bin/sh
62
# changelog.sh: Generate or update a changelog
73

8-
# Source initialization script
9-
#. "$GIV_LIB_DIR/init.sh"
4+
# Load initialization and shared functions
5+
. "$GIV_LIB_DIR/init.sh"
106

7+
# Allow test harness to inject mock functions (for bats)
8+
if [ -n "${GIV_TEST_MOCKS:-}" ] && [ -f "${GIV_TEST_MOCKS:-}" ]; then
9+
. "$GIV_TEST_MOCKS"
10+
fi
1111

12-
# Parse arguments specific to changelog
13-
parse_document_args "$@"
12+
# Arguments are already parsed by the unified parser
13+
# All environment variables are set by parse_arguments in giv.sh
1414

15-
revision="$1"
16-
pathspec="$2"
15+
revision="$GIV_REVISION"
16+
pathspec="$GIV_PATHSPEC"
1717
output_file="${output_file:-$changelog_file}"
1818
print_debug "Changelog file: $output_file"
1919

src/commands/config.sh

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ normalize_key() {
2929
esac
3030
}
3131

32+
# Quote value if it contains spaces, special characters, or is empty
33+
quote_value() {
34+
case "$1" in
35+
*[[:space:]]*|*[\"\'\`\$\\]*|'')
36+
printf '"%s"' "$1"
37+
;;
38+
*)
39+
printf '%s' "$1"
40+
;;
41+
esac
42+
}
43+
3244
giv_config() {
3345
cmd="$1"
3446
case "$cmd" in
@@ -72,8 +84,13 @@ giv_config() {
7284
;;
7385
--get|get)
7486
key="$2"
75-
# ENV override
87+
# Validate key format
7688
env_key="$(normalize_key "$key")"
89+
if [ -z "$env_key" ]; then
90+
printf '%s\n' "Invalid key format: $key" >&2
91+
return 1
92+
fi
93+
# ENV override
7794
if [ -n "$env_key" ]; then
7895
eval "env_val=\"\${$env_key-}\""
7996
if [ -n "$env_val" ]; then
@@ -92,8 +109,8 @@ giv_config() {
92109
givkey="$(normalize_key "$key")"
93110
val=$(grep -E "^$givkey=" "$GIV_CONFIG_FILE" | cut -d'=' -f2-)
94111
fi
95-
# Remove surrounding quotes if present
96-
val=$(printf '%s' "$val" | sed 's/^"\(.*\)"$/\1/')
112+
# Remove surrounding quotes if present (both single and double)
113+
val=$(printf '%s' "$val" | sed -e 's/^"\(.*\)"$/\1/' -e "s/^'\(.*\)'$/\1/")
97114
printf '%s\n' "$val"
98115
;;
99116
--unset|unset)
@@ -119,7 +136,7 @@ giv_config() {
119136
grep -v -E "^($key|$givkey)=" "$GIV_CONFIG_FILE" | grep -v '^$' > "$tmpfile"
120137
# Always write in GIV_... format
121138
writekey="$(normalize_key "$key")"
122-
printf '%s\n' "$writekey=$value" >> "$tmpfile"
139+
printf '%s=%s\n' "$writekey" "$(quote_value "$value")" >> "$tmpfile"
123140
mv "$tmpfile" "$GIV_CONFIG_FILE"
124141
;;
125142
-*|help)
@@ -130,9 +147,14 @@ giv_config() {
130147
*)
131148
key="$1"
132149
value="${2:-}"
150+
# Validate key format
151+
env_key="$(normalize_key "$key")"
152+
if [ -z "$env_key" ]; then
153+
printf '%s\n' "Invalid key format: $key" >&2
154+
return 1
155+
fi
133156
if [ -z "$value" ]; then
134157
# ENV override
135-
env_key="$(normalize_key "$key")"
136158
if [ -n "$env_key" ]; then
137159
eval "env_val=\"\${$env_key-}\""
138160
if [ -n "$env_val" ]; then
@@ -156,8 +178,8 @@ giv_config() {
156178
givkey="$(normalize_key "$key")"
157179
val=$(grep -E "^$givkey=" "$GIV_CONFIG_FILE" | cut -d'=' -f2-)
158180
fi
159-
# Remove surrounding quotes if present
160-
val=$(printf '%s' "$val" | sed 's/^"\(.*\)"$/\1/')
181+
# Remove surrounding quotes if present (both single and double)
182+
val=$(printf '%s' "$val" | sed -e 's/^"\(.*\)"$/\1/' -e "s/^'\(.*\)'$/\1/")
161183
printf '%s\n' "$val"
162184
else
163185
mkdir -p "$GIV_HOME"
@@ -169,7 +191,7 @@ giv_config() {
169191
grep -v -E "^($key|$givkey)=" "$GIV_CONFIG_FILE" | grep -v '^$' > "$tmpfile"
170192
# Always write in GIV_... format
171193
writekey="$(normalize_key "$key")"
172-
printf '%s\n' "$writekey=$value" >> "$tmpfile"
194+
printf '%s=%s\n' "$writekey" "$(quote_value "$value")" >> "$tmpfile"
173195
mv "$tmpfile" "$GIV_CONFIG_FILE"
174196
fi
175197
;;

src/commands/document.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
. "$GIV_LIB_DIR/init.sh"
88

99
# Allow test harness to inject mock functions (for bats)
10-
if [ -n "$GIV_TEST_MOCKS" ] && [ -f "$GIV_TEST_MOCKS" ]; then
10+
if [ -n "${GIV_TEST_MOCKS:-}" ] && [ -f "${GIV_TEST_MOCKS:-}" ]; then
1111
. "$GIV_TEST_MOCKS"
1212
fi
1313
# Function to generate documents based on a prompt template

src/commands/message.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
# Load initialization and shared functions
44
. "$GIV_LIB_DIR/init.sh"
55

6+
# Allow test harness to inject mock functions (for bats)
7+
if [ -n "${GIV_TEST_MOCKS:-}" ] && [ -f "${GIV_TEST_MOCKS:-}" ]; then
8+
. "$GIV_TEST_MOCKS"
9+
fi
10+
611

712
# All arguments are already parsed by the unified parser
813
# Use environment variables set by the parser

src/commands/release-notes.sh

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
# Allow test harness to inject mock functions (for bats)
2-
if [ -n "$GIV_TEST_MOCKS" ] && [ -f "$GIV_TEST_MOCKS" ]; then
3-
. "$GIV_TEST_MOCKS"
4-
fi
1+
#!/bin/sh
2+
# -------------------------------------------------------------------
53
# release-notes.sh: Generate release notes for a tagged release
64

75
# Source initialization script
8-
#. "$GIV_LIB_DIR/init.sh"
6+
. "$GIV_LIB_DIR/init.sh"
97

108
# Wrapper to call document.sh with appropriate arguments
119
if [ -f "${GIV_SRC_DIR}/commands/document.sh" ]; then

src/commands/summary.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Allow test harness to inject mock functions (for bats)
2-
if [ -n "$GIV_TEST_MOCKS" ] && [ -f "$GIV_TEST_MOCKS" ]; then
3-
. "$GIV_TEST_MOCKS"
4-
fi
51
#!/bin/sh
62
# summary.sh: Generate a summary of changes
73

84
# Source initialization script
9-
# . "$GIV_LIB_DIR/init.sh"
5+
. "$GIV_LIB_DIR/init.sh"
6+
# Allow test harness to inject mock functions (for bats)
7+
if [ -n "${GIV_TEST_MOCKS:-}" ] && [ -f "${GIV_TEST_MOCKS:-}" ]; then
8+
. "$GIV_TEST_MOCKS"
9+
fi
1010

1111
# All arguments are already parsed by the unified parser
1212
# Use environment variables set by the parser: GIV_REVISION, GIV_PATHSPEC, etc.

src/lib/llm.sh

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,21 @@ extract_content_from_response() {
114114
generate_remote() {
115115
content=$(cat "$1")
116116

117-
print_debug "Generating remote response with content: $GIV_API_MODEL"
117+
# Strip surrounding quotes from URL, API key, and model if present
118+
API_URL=$(printf '%s' "$GIV_API_URL" | sed -e 's/^"\(.*\)"$/\1/' -e "s/^'\(.*\)'$/\1/")
119+
API_KEY=$(printf '%s' "$GIV_API_KEY" | sed -e 's/^"\(.*\)"$/\1/' -e "s/^'\(.*\)'$/\1/")
120+
API_MODEL=$(printf '%s' "$GIV_API_MODEL" | sed -e 's/^"\(.*\)"$/\1/' -e "s/^'\(.*\)'$/\1/")
121+
122+
print_debug "Generating remote response using: $API_URL"
123+
print_debug "Original API URL: '$GIV_API_URL'"
124+
print_debug "Cleaned API URL: '$API_URL'"
125+
print_debug "Original API KEY: '$GIV_API_KEY'"
126+
print_debug "Cleaned API KEY: '$API_KEY'"
127+
print_debug "Generating remote response with model: $GIV_API_MODEL"
118128
# Check required environment variables
119129
if [ -z "${GIV_API_MODEL}" ] || [ -z "${GIV_API_URL}" ] || [ -z "${GIV_API_KEY}" ]; then
120130
printf 'Error: Missing required environment variables for remote generation.\n' >&2
131+
print_plain "Please check your API key and URL configuration."
121132
return 1
122133
fi
123134

@@ -127,17 +138,26 @@ generate_remote() {
127138

128139
# shellcheck disable=SC2154
129140
body=$(printf '{"model":"%s","messages":[{"role":"user","content":%s}],"max_completion_tokens":8192}' \
130-
"${GIV_API_MODEL}" "${escaped_content}")
141+
"${API_MODEL}" "${escaped_content}")
142+
143+
print_debug "Request body: $body"
131144

132145
# shellcheck disable=SC2154
133-
response=$(curl -sS --fail -H "Authorization: Bearer ${GIV_API_KEY}" -H "Content-Type: application/json" -d "${body}" "${GIV_API_URL}")
146+
# Use cleaned values and make Authorization header optional for local services like Ollama
147+
if [ -n "${API_KEY}" ] && [ "${API_KEY}" != "ollama" ] && [ "${API_KEY}" != "giv" ] && [ "${API_URL}" != *"localhost"* ]; then
148+
print_debug "Using Authorization header with API key"
149+
response=$(curl -sS --fail -H "Authorization: Bearer ${API_KEY}" -H "Content-Type: application/json" -d "${body}" "${API_URL}")
150+
else
151+
print_debug "Skipping Authorization header (local service detected)"
152+
response=$(curl -sS --fail -H "Content-Type: application/json" -d "${body}" "${API_URL}")
153+
fi
154+
134155

135156
print_debug "Response from remote API:"
136157
print_debug "${response}"
137158

138159
if [ -z "${response}" ]; then
139-
print_error "No response received from remote API: ${GIV_API_URL}"
140-
print_plain "Please check your API key and URL configuration."
160+
print_error "No response received from remote API: ${API_URL}"
141161
return 1
142162
fi
143163
# Try to extract content using jq if available

tests/fixtures/project-repo.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ set -eu
88
TARGET="$1"
99
mkdir -p "$TARGET"
1010
cd "$TARGET"
11+
printf "GIV_TEST_MOCKS=%s\n" "$GIV_TEST_MOCKS" > .env
1112
git init >/dev/null 2>&1
1213
echo "First line" > file.txt
1314
git add file.txt

tests/helpers/mock_generate_response.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ This release introduces server capabilities and improves the project foundation.
4141
echo "Generated content for integration testing"
4242
;;
4343
esac
44+
printf "%s\n" "$@"
45+
cat $1
4446
}
4547

4648
generate_response() { mock_generate_response "$@"; }

0 commit comments

Comments
 (0)