libreboot

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit d0b1906801bd62029acd6ae9c015d95ab33af657
parent 4394e7ef874398eeb22922b395c5a0880a28d7bf
Author: Paul Kocialkowski <contact@paulk.fr>
Date:   Sat, 24 Dec 2016 22:27:30 +0100

libs: tool: Add utility functions for files

Signed-off-by: Paul Kocialkowski <contact@paulk.fr>

Diffstat:
libs/tool | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 82 insertions(+), 0 deletions(-)

diff --git 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 +}