Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

internal/variables/file handle arrays and missing files

  • Loading branch information...
commit d273801f19c5e4e9b3a6c2422685ce446ff9fc4d 1 parent e256726
@mpapis mpapis authored
View
66 core/internal/shell/variables/file/array_file_comment_test.sh
@@ -0,0 +1,66 @@
+#!/usr/bin/env zsh
+
+eval $(./local test sm internal/variables/file )
+
+: one line read
+unset var1 var2
+export -a var1 var2
+
+printf "var1=(a b)" > "${sm_temp_path}/test-me"
+__sm_variables_file_read_one "${sm_temp_path}/test-me" var1
+# status=0; env[var2]=/^$/
+
+echo ${var1[1]}
+# match=/^a$/
+echo ${var1[@]}
+# match=/^a b$/
+
+: multi line read
+unset var1 var2
+export -a var1 var2
+
+printf "var1=(\nc\nd\n)" > "${sm_temp_path}/test-me"
+__sm_variables_file_read_one "${sm_temp_path}/test-me" var1
+# status=0; env[var2]=/^$/
+
+echo ${var1[1]}
+# match=/^c$/
+echo ${var1[@]}
+# match=/^c d$/
+
+: multi line read ... not quoted
+unset var1 var2
+export -a var1 var2
+
+printf "var1=(\ne f\ng h\n)" > "${sm_temp_path}/test-me"
+__sm_variables_file_read_one "${sm_temp_path}/test-me" var1
+# status=0; env[var2]=/^$/
+
+echo ${var1[1]}
+# match=/^e$/
+echo ${var1[@]}
+# match=/^e f g h$/
+
+: one line write
+unset var1 var2
+export -a var1 var2
+
+var1=(a b)
+rm -f "${sm_temp_path}/test-me"
+__sm_variables_file_write_one "${sm_temp_path}/test-me" var1 # status=0
+[[ -f "${sm_temp_path}/test-me" ]] # status=0
+
+cat "${sm_temp_path}/test-me"
+# match=/^var1=\( a b \)$/
+
+: one line update
+unset var1 var2
+export -a var1 var2
+
+var1=(c d)
+printf "var1=(a b)" > "${sm_temp_path}/test-me"
+__sm_variables_file_write_one "${sm_temp_path}/test-me" var1 # status=0
+[[ -f "${sm_temp_path}/test-me" ]] # status=0
+
+cat "${sm_temp_path}/test-me"
+# match=/^var1=\( c d \)$/
View
32 core/internal/shell/variables/file/functions
@@ -12,7 +12,18 @@ __sm_variables_file_write()
__sm_variables_file_read_one()
{
- eval "${2//-/_}=\"\$(sed '/^$2=/! d; s/^$2=//' < \"\$1\")\""
+ typeset GREP_OPTIONS
+ case "$(GREP_OPTIONS="" \grep -o "^$2=.*$" < "$1")" in
+ ($2=\(*\))
+ eval "${2//-/_}=( \$(sed '/^$2=/! d; s/^$2=(//; s/)$//' < \"\$1\") )"
+ ;;
+ ($2=\(*)
+ eval "${2//-/_}=( \$(awk 'BEGIN {m=0} /^$2=/ {m=1; sub(/^$2=\(/,\"\");} /^)/ {m=0;} m==1 {print;}' < \"\$1\") )"
+ ;;
+ ($2=*)
+ eval "${2//-/_}=\"\$(sed '/^$2=/! d; s/^$2=//' < \"\$1\")\""
+ ;;
+ esac
}
__sm_variables_file_read_many()
@@ -43,7 +54,24 @@ __sm_variables_file_read_all()
__sm_variables_file_write_one()
{
- eval "sed -i '/^$2=/ s/^.*$/$2='\"\${${2//-/_}}\"'/' \"\${1}\""
+ typeset GREP_OPTIONS
+ if
+ [[ -f "${1}" ]] && GREP_OPTIONS="" \grep "^$2=" < "$1" >/dev/null
+ then
+ if __sm.variable.is.array "${2//-/_}"
+ then
+ eval "sed -i '/^$2=/ s/^.*$/$2=( '\"\${${2//-/_}}\"' )/' \"\${1}\""
+ else
+ eval "sed -i '/^$2=/ s/^.*$/$2='\"\${${2//-/_}}\"'/' \"\${1}\""
+ fi
+ else
+ if __sm.variable.is.array "${2//-/_}"
+ then
+ eval "printf \"$2=( \${${2//-/_}} )\"" > "${1}"
+ else
+ eval "printf \"$2=\${${2//-/_}}\"" > "${1}"
+ fi
+ fi
}
__sm_variables_file_write_many()
View
1  core/internal/shell/variables/file/includes
@@ -0,0 +1 @@
+internal/variables
Please sign in to comment.
Something went wrong with that request. Please try again.