@@ -44,92 +44,67 @@ quote_value() {
4444giv_config () {
4545 cmd=" $1 "
4646 case " $cmd " in
47- --list|list)
47+ --list|list|show )
4848 if [ ! -f " $GIV_CONFIG_FILE " ]; then
4949 printf ' %s\n' " config file not found" >&2
5050 return 1
5151 fi
52- # Check for malformed lines
53- if [ -s " $GIV_CONFIG_FILE " ]; then
54- # Updated malformed config check to ignore empty lines and lines with only whitespace
55- # Simple validation: check for lines that don't contain = and aren't comments or empty
56- while IFS= read -r line; do
57- case " $line " in
58- ' #' * |' ' ) continue ;; # Skip comments and empty lines
59- * =* ) continue ;; # Valid config line
60- * )
61- printf ' %s\n' " Malformed config line: $line " >&2
62- return 1
63- ;;
64- esac
65- done < " $GIV_CONFIG_FILE "
52+
53+ if [ ! -s " $GIV_CONFIG_FILE " ]; then
54+ printf ' %s\n' " No configuration found." >&2
55+ return 0
6656 fi
67- # Convert GIV_... keys to user keys for output from config file
68- if [ -s " $GIV_CONFIG_FILE " ]; then
69- while IFS= read -r line; do
70- case " $line " in
71- GIV_* )
72- k=${line# GIV_}
73- k=${k%% =* }
74- key=$( printf ' %s' " $k " | tr ' A-Z_' ' a-z.' )
75- value=" ${line#* =} "
76- printf ' %s\n' " $key =$value "
77- ;;
78- * =* )
79- printf ' %s\n' " $line "
80- ;;
81- * )
57+
58+ # Check for malformed lines
59+ while IFS= read -r line; do
60+ case " $line " in
61+ ' #' * |' ' ) continue ;; # Skip comments and empty lines
62+ * =* ) continue ;; # Valid config line
63+ * )
64+ printf ' %s\n' " Malformed config line: $line " >&2
65+ return 1
8266 ;;
83- esac
84- done < " $GIV_CONFIG_FILE "
85- fi
86-
87- # Also show environment variables with GIV_ prefix that may have been loaded from --config-file
88- for var in $( env | grep ' ^GIV_' ) ; do
89- case " $var " in
67+ esac
68+ done < " $GIV_CONFIG_FILE "
69+
70+ # Convert GIV_... keys to user keys for output from config file
71+ while IFS= read -r line; do
72+ case " $line " in
9073 GIV_* )
91- k=${var # GIV_}
74+ k=${line # GIV_}
9275 k=${k%% =* }
9376 key=$( printf ' %s' " $k " | tr ' A-Z_' ' a-z.' )
94- value=" ${var #* =} "
95- # Only show if not already shown from config file
96- if [ -z " $( grep " ^GIV_ $k = " " $GIV_CONFIG_FILE " 2> /dev/null ) " ] ; then
97- printf ' %s\n ' " $key = $value "
98- fi
77+ value=" ${line #* =} "
78+ printf ' %s\n ' " $key = $value "
79+ ;;
80+ * = * )
81+ printf ' %s\n ' " $line "
9982 ;;
10083 esac
101- done
84+ done < " $GIV_CONFIG_FILE "
10285 ;;
10386 --get|get)
10487 key=" $2 "
105- # Validate key format
106- env_key=" $( normalize_key " $key " ) "
107- if [ -z " $env_key " ]; then
108- printf ' %s\n' " Invalid key format: $key " >&2
109- return 1
110- fi
111- # ENV override
112- if [ -n " $env_key " ]; then
113- eval " env_val=\"\$ {$env_key -}\" "
114- if [ -n " $env_val " ]; then
115- printf ' %s\n' " $env_val "
116- return 0
117- fi
118- fi
88+ # Only support dot-separated keys in the config file
11989 if [ ! -f " $GIV_CONFIG_FILE " ]; then
12090 printf ' %s\n' " config file not found" >&2
12191 return 1
12292 fi
123-
124- # Try user key first, then GIV_ key
12593 val=$( grep -E " ^$key =" " $GIV_CONFIG_FILE " | cut -d' =' -f2-)
126- if [ -z " $val " ]; then
127- givkey=" $( normalize_key " $key " ) "
128- val=$( grep -E " ^$givkey =" " $GIV_CONFIG_FILE " | cut -d' =' -f2-)
129- fi
130- # Remove surrounding quotes if present (both single and double)
13194 val=$( printf ' %s' " $val " | sed -e ' s/^"\(.*\)"$/\1/' -e " s/^'\(.*\)'$/\1/" )
132- printf ' %s\n' " $val "
95+ if [ -n " $val " ]; then
96+ printf ' %s\n' " $val "
97+ return 0
98+ fi
99+ # Fallback to GIV_... env var
100+ env_key=" $( normalize_key " $key " ) "
101+ env_val=" $( printenv " $env_key " 2> /dev/null) "
102+ if [ -n " $env_val " ]; then
103+ printf ' %s\n' " $env_val "
104+ return 0
105+ fi
106+ # Not found
107+ return 1
133108 ;;
134109 --unset|unset)
135110 key=" $2 "
@@ -150,11 +125,8 @@ giv_config() {
150125 touch " $GIV_CONFIG_FILE "
151126 fi
152127 tmpfile=$( mktemp)
153- givkey=" $( normalize_key " $key " ) "
154- grep -v -E " ^($key |$givkey )=" " $GIV_CONFIG_FILE " | grep -v ' ^$' > " $tmpfile "
155- # Always write in GIV_... format
156- writekey=" $( normalize_key " $key " ) "
157- printf ' %s=%s\n' " $writekey " " $( quote_value " $value " ) " >> " $tmpfile "
128+ grep -v -E " ^$key =" " $GIV_CONFIG_FILE " | grep -v ' ^$' > " $tmpfile "
129+ printf ' %s=%s\n' " $key " " $( quote_value " $value " ) " >> " $tmpfile "
158130 mv " $tmpfile " " $GIV_CONFIG_FILE "
159131 ;;
160132 -* |help)
@@ -165,51 +137,39 @@ giv_config() {
165137 * )
166138 key=" $1 "
167139 value=" ${2:- } "
168- # Validate key format
169- env_key=" $( normalize_key " $key " ) "
170- if [ -z " $env_key " ]; then
171- printf ' %s\n' " Invalid key format: $key " >&2
172- return 1
173- fi
174140 if [ -z " $value " ]; then
175- # ENV override
176- if [ -n " $env_key " ]; then
177- eval " env_val=\"\$ {$env_key -}\" "
178- if [ -n " $env_val " ]; then
179- printf ' %s\n' " $env_val "
180- return 0
181- fi
182- fi
141+ # ENV override fallback for dot-separated keys
183142 if [ ! -f " $GIV_CONFIG_FILE " ]; then
184143 printf ' %s\n' " config file not found" >&2
185144 return 1
186145 fi
187146 if [ -s " $GIV_CONFIG_FILE " ]; then
188- # Updated malformed config check to ignore empty lines and lines with only whitespace
189147 if grep -qvE ' ^(#|[^=]+=.*|[[:space:]]*)$' " $GIV_CONFIG_FILE " ; then
190148 printf ' %s\n' " Malformed config" >&2
191149 return 1
192150 fi
193151 fi
194152 val=$( grep -E " ^$key =" " $GIV_CONFIG_FILE " | cut -d' =' -f2-)
195- if [ -z " $val " ]; then
196- givkey=" $( normalize_key " $key " ) "
197- val=$( grep -E " ^$givkey =" " $GIV_CONFIG_FILE " | cut -d' =' -f2-)
198- fi
199- # Remove surrounding quotes if present (both single and double)
200153 val=$( printf ' %s' " $val " | sed -e ' s/^"\(.*\)"$/\1/' -e " s/^'\(.*\)'$/\1/" )
201- printf ' %s\n' " $val "
154+ if [ -n " $val " ]; then
155+ printf ' %s\n' " $val "
156+ return 0
157+ fi
158+ env_key=" $( normalize_key " $key " ) "
159+ env_val=" $( printenv " $env_key " 2> /dev/null) "
160+ if [ -n " $env_val " ]; then
161+ printf ' %s\n' " $env_val "
162+ return 0
163+ fi
164+ return 1
202165 else
203166 mkdir -p " $GIV_HOME "
204167 if [ ! -f " $GIV_CONFIG_FILE " ]; then
205168 touch " $GIV_CONFIG_FILE "
206169 fi
207170 tmpfile=$( mktemp)
208- givkey=" $( normalize_key " $key " ) "
209- grep -v -E " ^($key |$givkey )=" " $GIV_CONFIG_FILE " | grep -v ' ^$' > " $tmpfile "
210- # Always write in GIV_... format
211- writekey=" $( normalize_key " $key " ) "
212- printf ' %s=%s\n' " $writekey " " $( quote_value " $value " ) " >> " $tmpfile "
171+ grep -v -E " ^$key =" " $GIV_CONFIG_FILE " | grep -v ' ^$' > " $tmpfile "
172+ printf ' %s=%s\n' " $key " " $( quote_value " $value " ) " >> " $tmpfile "
213173 mv " $tmpfile " " $GIV_CONFIG_FILE "
214174 fi
215175 ;;
0 commit comments