diff options
author | Paul Kocialkowski <contact@paulk.fr> | 2016-12-24 22:27:30 +0100 |
---|---|---|
committer | Leah Rowe <info@minifree.org> | 2017-01-15 14:24:46 +0000 |
commit | d0b1906801bd62029acd6ae9c015d95ab33af657 (patch) | |
tree | 09730ded73eb33dbb974d3e9da26dd81cccc27e2 /libs/tool | |
parent | 4394e7ef874398eeb22922b395c5a0880a28d7bf (diff) | |
download | librebootfr-d0b1906801bd62029acd6ae9c015d95ab33af657.tar.gz librebootfr-d0b1906801bd62029acd6ae9c015d95ab33af657.zip |
libs: tool: Add utility functions for files
Signed-off-by: Paul Kocialkowski <contact@paulk.fr>
Diffstat (limited to 'libs/tool')
-rwxr-xr-x | libs/tool | 82 |
1 files changed, 82 insertions, 0 deletions
@@ -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 +} |