From d0b1906801bd62029acd6ae9c015d95ab33af657 Mon Sep 17 00:00:00 2001 From: Paul Kocialkowski Date: Sat, 24 Dec 2016 22:27:30 +0100 Subject: libs: tool: Add utility functions for files Signed-off-by: Paul Kocialkowski --- libs/tool | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) (limited to 'libs') diff --git a/libs/tool b/libs/tool index 889ee8d5..6bf57be1 100755 --- a/libs/tool +++ b/libs/tool @@ -285,3 +285,85 @@ tool_usage_arguments_recursive() { done fi } + +tool_file_path() { + local tool=$1 + shift + local directory=$1 + shift + local file=$1 + shift + + local tool_path=$( tool_path "$tool" ) + local path="$tool_path/$directory" + local argument + local file_path + + for argument in "" "$@" + do + if ! [ -z "$argument" ] + then + path="$path/$argument" + fi + + if ! [ -f "$path/$file" ] + then + continue + fi + + file_path="$path/$file" + done + + if [ -z "$file_path" ] + then + return 1 + fi + + echo "$file_path" +} + +tool_file_test() { + local file_path=$( tool_file_path "$@" ) + + test -f "$file_path" +} + +tool_file_contents() { + local file_path=$( tool_file_path "$@" ) + + if [ -f "$file_path" ] + then + cat "$file_path" + fi +} + +tool_file_contents_herit() { + local tool=$1 + shift + local directory=$1 + shift + local file=$1 + shift + + local tool_path=$( tool_path "$tool" ) + local path="$tool_path/$directory" + local argument + local file_path + + for argument in "" "$@" + do + if ! [ -z "$argument" ] + then + path="$path/$argument" + fi + + file_path="$path/$file" + + if ! [ -f "$file_path" ] + then + continue + fi + + cat "$file_path" + done +} -- cgit v1.2.3-70-g09d2