forked from ungleich-public/cdist
		
	[type/__uci_section] Externalise functions to separate file
This commit is contained in:
		
					parent
					
						
							
								7b30119504
							
						
					
				
			
			
				commit
				
					
						a6c37095f1
					
				
			
		
					 2 changed files with 54 additions and 40 deletions
				
			
		
							
								
								
									
										52
									
								
								cdist/conf/type/__uci_section/files/functions.sh
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								cdist/conf/type/__uci_section/files/functions.sh
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,52 @@
 | 
			
		|||
# -*- mode: sh; indent-tabs-mode: t -*-
 | 
			
		||||
 | 
			
		||||
append_values() {
 | 
			
		||||
	while read -r _value
 | 
			
		||||
	do
 | 
			
		||||
		set -- "$@" --value "${_value}"
 | 
			
		||||
	done
 | 
			
		||||
	unset _value
 | 
			
		||||
	"$@" </dev/null
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
grep_line() {
 | 
			
		||||
	{ shift; printf '%s\n' "$@"; } | grep -qxF "$1"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
prefix_lines() {
 | 
			
		||||
	while test $# -gt 0
 | 
			
		||||
	do
 | 
			
		||||
		echo "$2" | awk -v prefix="$1" '$0 { printf "%s %s\n", prefix, $0 }'
 | 
			
		||||
		shift; shift
 | 
			
		||||
	done
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
print_errors() {
 | 
			
		||||
	awk -v prefix="${1:-Found errors:}" -v suffix="${2-}" '
 | 
			
		||||
		BEGIN {
 | 
			
		||||
			if (getline) {
 | 
			
		||||
				print prefix
 | 
			
		||||
				print
 | 
			
		||||
				rc = 1
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		{ print }
 | 
			
		||||
		END {
 | 
			
		||||
			if (rc && suffix) print suffix
 | 
			
		||||
			exit rc
 | 
			
		||||
		}' >&2
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uci_validate_name() {
 | 
			
		||||
	# like util.c uci_validate_name()
 | 
			
		||||
	test -n "$*" && test -z "$(printf %s "$*" | tr -d '[:alnum:]_' | tr -c '' .)"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
unquote_lines() {
 | 
			
		||||
	sed -e '/^".*"$/{s/^"//;s/"$//}' \
 | 
			
		||||
	    -e '/'"^'.*'"'$/{s/'"^'"'//;s/'"'$"'//}'
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
validate_options() {
 | 
			
		||||
	grep -shv -e '^[[:alnum:]_]\{1,\}=' "$@"
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -18,46 +18,8 @@
 | 
			
		|||
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
#
 | 
			
		||||
 | 
			
		||||
grep_line() { { shift; printf '%s\n' "$@"; } | grep -qxF "$1"; }
 | 
			
		||||
prefix_lines() {
 | 
			
		||||
	while test $# -gt 0
 | 
			
		||||
	do
 | 
			
		||||
		echo "$2" | awk -v prefix="$1" '$0 { printf "%s %s\n", prefix, $0 }'
 | 
			
		||||
		shift; shift
 | 
			
		||||
	done
 | 
			
		||||
}
 | 
			
		||||
uci_validate_name() {
 | 
			
		||||
	# like util.c uci_validate_name()
 | 
			
		||||
	test -n "$*" && test -z "$(printf %s "$*" | tr -d '[:alnum:]_' | tr -c '' .)"
 | 
			
		||||
}
 | 
			
		||||
validate_options() { grep -shv -e '^[[:alnum:]_]\{1,\}=' "$@"; }
 | 
			
		||||
unquote_lines() {
 | 
			
		||||
	sed -e '/^".*"$/{s/^"//;s/"$//}' \
 | 
			
		||||
	    -e '/'"^'.*'"'$/{s/'"^'"'//;s/'"'$"'//}'
 | 
			
		||||
}
 | 
			
		||||
append_values() {
 | 
			
		||||
	while read -r _value
 | 
			
		||||
	do
 | 
			
		||||
		set -- "$@" --value "${_value}"
 | 
			
		||||
	done
 | 
			
		||||
	unset _value
 | 
			
		||||
	"$@" </dev/null
 | 
			
		||||
}
 | 
			
		||||
print_errors() {
 | 
			
		||||
	awk -v prefix="${1:-Found errors:}" -v suffix="${2-}" '
 | 
			
		||||
		BEGIN {
 | 
			
		||||
			if (getline) {
 | 
			
		||||
				print prefix
 | 
			
		||||
				print
 | 
			
		||||
				rc = 1
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		{ print }
 | 
			
		||||
		END {
 | 
			
		||||
			if (rc && suffix) print suffix
 | 
			
		||||
			exit rc
 | 
			
		||||
		}' >&2
 | 
			
		||||
}
 | 
			
		||||
# shellcheck source=files/functions.sh
 | 
			
		||||
. "${__type:?}/files/functions.sh"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## Check section name and error if invalid!
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue