Commit e40817eb authored by sebt3's avatar sebt3
Browse files

Improved "utils" part

parent 1c8fb8eb
Loading
Loading
Loading
Loading
+52 −19
Original line number Diff line number Diff line
@@ -11,13 +11,12 @@ SCRIPT_path=$(dirname "$(readlink -f "$0")")
is.set() { 	[ ! -z ${!1+x} ]; }
is.array() { 	is.set $1 && [[ "$(declare -p $1)" =~ "declare -a" ]]; }

OUT_levels=(NONE FAIL ERROR OK TASK WARNING NOTICE CMD INFO OUTPUT DETAIL ALL)
OUT_levels=(NONE FAIL ERROR OK TASK WARNING NOTICE CMD INFO OUTPUT DETAIL ASSERT ALL)
OUT_level=${OUT_level:-OUTPUT}
out.levelID() {
	local i=0
	for l in ${OUT_levels[*]};do
		i=$(( $i + 1 ))
		if [[ "$1" = "$l" ]];then
	local i
	for (( i=0; i<${#OUT_levels[@]}; i++));do
		if [[ "$1" = "${OUT_levels[$i]}" ]];then
			echo $i
			return
		fi
@@ -31,6 +30,8 @@ OUT_color[$(out.levelID OK)]="${OUT_color[$(out.levelID OK)]:-$(tput setf 2;tput
OUT_color[$(out.levelID WARNING)]="${OUT_color[$(out.levelID WARNING)]:-$(tput setf 6;tput setaf 3)}"
OUT_color[$(out.levelID INFO)]="${OUT_color[$(out.levelID INFO)]:-$(tput setf 3;tput setaf 4)}"
OUT_color[$(out.levelID NOTICE)]="${OUT_color[$(out.levelID NOTICE)]:-$(tput setf 5;tput setaf 13)}"
OUT_color[$(out.levelID DETAIL)]="${OUT_color[$(out.levelID DETAIL)]:-$(tput setf 3;tput setaf 11)}"
OUT_color[$(out.levelID ASSERT)]="${OUT_color[$(out.levelID ASSERT)]:-$(tput setf 3;tput setaf 12)}"
OUT_color[$(out.levelID CMD)]="${OUT_color[$(out.levelID CMD)]:-$(tput setf 6;tput setaf 4)}"
OUT_cmd=()
out.init() {
@@ -48,20 +49,28 @@ out.init() {
		esac
	done
	OUT_levelID=$(out.levelID $OUT_level)
	OUT_levelID=${OUT_levelID:-$(out.levelID OUTPUT)}
}
out.lvl() {
	local lvl=$1;shift
	local id=$(out.levelID $lvl)
	[ ${id:-10} -le ${OUT_levelID:-13} ] && eval "${OUT_cmd[$id]}  >&$OUT_fd"
	id=${id:-10}
	local cmd=${OUT_cmd[$id]}
	[ $id -le ${OUT_levelID:-13} ] && eval "${cmd:-"printf '\\r%s\\n' \"\$*\""} >&${OUT_fd:-1}"
	log.lvl "$lvl" "$*"
	return 0
}
out.return() {
	local id=$(out.levelID $1)
	[ ${id:-10} -le ${OUT_levelID:-13} ] && eval "printf '\\n' >&${OUT_fd:-1}"
}
out.put() {	while read line;do out.lvl OUTPUT "$line";done; }
out.error() {	out.lvl ERROR "$*"; }
out.ok() {	out.lvl OK "$*"; }
out.warn() {	out.lvl WARNING "$*"; }
out.info() {	out.lvl INFO "$*"; }
out.notice() {	out.lvl NOTICE "$*"; }
out.detail() {	out.lvl DETAIL "$*"; }
out.task() {	out.lvl TASK "$*"; }
out.fail() {	out.lvl FAIL "$*";exit 1; }
out.cmd() {	out.lvl CMD "$*";eval "$*" 2>&1|out.put; test ${PIPESTATUS[0]} -eq 0; }
@@ -110,15 +119,22 @@ ARGS_short=()
ARGS_long=()
ARGS_haveVal=()
ARGS_mandatory=()
ARGS_validate=()
ARGS_option=()
ARGS_desc=()
ARGS_cnt=0
ARGS_cb=()
ARGS_info=${ARGS_info:-''}
ARGS_cmd="$*"
ARGS_short_cmd=()
args.declare() {
	ARGS_cnt=$(( $ARGS_cnt + 1 ))
	local ARGS_cnt=${#ARGS_vars[@]}
	ARGS_validate[$ARGS_cnt]="N"
	if [[ $1 == "N" ]] || [[ $1 == "Y" ]];then
		ARGS_validate[$ARGS_cnt]="$1"
		shift
	fi
	LOG_head+=($1)
	ARGS_cb[$ARGS_cnt]=""
	ARGS_vars[$ARGS_cnt]=$1;shift
	ARGS_short[$ARGS_cnt]=$1;shift
	ARGS_long[$ARGS_cnt]=$1;shift
@@ -136,12 +152,21 @@ args.declare() {
	esac
	ARGS_desc[$ARGS_cnt]="$*";
}
args.callback() {
	local i v=$1;shift
	for (( i=0; i<${#ARGS_vars[@]}; i++ ));do
		if [[ ${ARGS_vars[$i]} == $v ]];then
			ARGS_cb[$i]="$*"
		fi
	done
}
args.option.declare() {
	local V=$1;shift;
	local s=$1;shift;
	local l=$1;shift;
	local m=$1;shift;
	args.declare $V $s $l Y Y $m "$*"
	local c=$1;shift;
	args.declare "$c" "$V" "$s" "$l" Y Y $m "$*"
}
args.option() {
	local A=$1;shift;local V=$1;shift
@@ -161,7 +186,7 @@ args.help() {
		ARGS_tmp=("${ARGS_short_cmd[@]}")
		while is.set ARGS_tmp;do 
			v=${ARGS_tmp[0]};ARGS_tmp=("${ARGS_tmp[@]:1}")
			for i in $(seq 1 $ARGS_cnt);do
			for (( i=0; i<${#ARGS_vars[@]}; i++ ));do
				if [[ "${ARGS_vars[$i]}" == "$v" ]];then
					if [[ "${ARGS_mandatory[$i]}" = "Y" ]];then
						s="$s $v"
@@ -172,7 +197,7 @@ args.help() {
			done
		done
	fi
	for i in $(seq 1 $ARGS_cnt);do
	for (( i=0; i<${#ARGS_vars[@]}; i++ ));do
		v="${ARGS_short[$i]}|${ARGS_long[$i]}"
		[[ "${ARGS_haveVal[$i]}" = "Y" ]] && v="$v ${ARGS_vars[$i]}"
		if [[ "${ARGS_mandatory[$i]}" = "Y" ]];then
@@ -184,12 +209,12 @@ args.help() {
	[ -n "$ARGS_info" ] && echo "$ARGS_info"
	echo $l
	is.set ARGS_short_cmd && echo $s
	for i in $(seq 1 $ARGS_cnt);do
	for (( i=0; i<${#ARGS_vars[@]}; i++ ));do
		v="${ARGS_short[$i]}|${ARGS_long[$i]}"
		[[ "${ARGS_haveVal[$i]}" = "Y" ]] && v="$v ${ARGS_vars[$i]}"
		printf "%-25s: %s\n" "$v" "${ARGS_desc[$i]}"
	done
	for i in $(seq 1 $ARGS_cnt);do
	for (( i=0; i<${#ARGS_vars[@]}; i++ ));do
		if [[ "${ARGS_option[$i]}" = "Y" ]];then
			echo;echo "Available values for ${ARGS_vars[$i]}:";n=0
			for j in $(eval "echo \${ARGS_values_${ARGS_vars[$i]}[*]}");do
@@ -207,7 +232,7 @@ args.parse() {
	if [ $# -gt 0 ] && [[ "$1" == -* ]];then
		while [ $# -gt 0 ];do
			f=0
			for i in $(seq 1 $ARGS_cnt);do
			for (( i=0; i<${#ARGS_vars[@]}; i++ ));do
				case "$1" in
				${ARGS_short[$i]}|${ARGS_long[$i]})
					if [[ "${ARGS_vars[$i]}" = "ARGS_help" ]];then
@@ -221,7 +246,7 @@ args.parse() {
						out.error "$1 expect a value"
						args.help
						exit 1
					elif [[ "${ARGS_option[$i]}" = "Y" ]];then
					elif [[ "${ARGS_option[$i]}" = "Y" ]] && [[ "${ARGS_validate[$i]}" = "Y" ]];then
						for j in $(eval "echo \${ARGS_values_${ARGS_vars[$i]}[*]}");do
							[[ "$2" = "$j" ]] && f=1
						done
@@ -238,6 +263,14 @@ args.parse() {
						ARGS_mandatory[$i]='N'
						f=1
						shift
					fi
					if [[ "${ARGS_cb[$i]}" != "" ]];then
						local v=$(eval echo \$${ARGS_vars[$i]})
						if ! ${ARGS_cb[$i]} $v;then
							out.error "\"$v\" is an invalid value for \"${ARGS_vars[$i]}\""
							args.help
							exit 1
						fi
					fi;;
				esac
			done
@@ -252,10 +285,10 @@ args.parse() {
		ARGS_tmp=("${ARGS_short_cmd[@]}")
		while is.set ARGS_tmp;do 
			v=${ARGS_tmp[0]};ARGS_tmp=("${ARGS_tmp[@]:1}")
			for i in $(seq 1 $ARGS_cnt);do
			for (( i=0; i<${#ARGS_vars[@]}; i++ ));do
				if [[ "${ARGS_vars[$i]}" == "$v" ]];then
					ARGS_mandatory[$i]='N'
					if [[ "${ARGS_option[$i]}" = "Y" ]];then
					if [[ "${ARGS_option[$i]}" = "Y" ]] && [[ "${ARGS_validate[$i]}" = "Y" ]];then
						f=0
						for j in $(eval "echo \${ARGS_values_${ARGS_vars[$i]}[*]}");do
							[[ "$1" = "$j" ]] && f=1
@@ -277,7 +310,7 @@ args.parse() {
		done
	fi
	f=0
	for i in $(seq 1 $ARGS_cnt);do
	for (( i=0; i<${#ARGS_vars[@]}; i++ ));do
		if [[ "${ARGS_mandatory[$i]}" = "Y" ]];then
			f=1
			out.error "flag ${ARGS_long[$i]} should be used"
@@ -451,7 +484,7 @@ ARGS_info="Pyra toolchain installer for debian"
args.use.help
args.declare DEST -d --dest Y N N "Installation destination (Default: ${DEST})"
args.declare OPTS -o --option Y N N "Optionnal argument for the given task"
args.option.declare ACTION -a --action Y "Task to do"
args.option.declare ACTION -a --action Y Y "Task to do"
args.option ACTION apt_install    "Install debian dependencies"
args.option ACTION get.setup	  "Get the environment-setup and related files"
args.option ACTION rootfs.get     "Download the pyra rootfs"