#!/usr/bin/env bash # SPDX-FileCopyrightText: 2022 Robin Vobruba # SPDX-License-Identifier: AGPL-3.0-or-later # # See the output of "$0 -h" for details. # Exit immediately on each error and unset variable; # see: https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/ set -Eeuo pipefail #set -Eeu APP_NAME="Listing of files to tree view converter" script_name="$(basename "$0")" output=/dev/stdout html=false title="" # parameters function print_help() { echo -e "" echo -e "$APP_NAME - Given a file listing - as for example created by 'find'," echo -e "generates a tree view." echo -e "The input file-listing format has to follow these rules:" echo -e "* one file/dir per line" echo -e "* full paths, relative to the output-dir" echo -e "* each line indicating a dir needs to end with a '/'" echo -e "* empty lines and lines starting with '#' or ';', are ignored" echo -e "* any other line indicates a file" echo -e "" echo -e "Usage:" echo -e "\t$script_name [OPTION...]" echo -e "Options:" echo -e "\t-o, --output [FILE]" echo -e "\t\tSets the output file to write the tree to (default: '$output')" echo -e "\t--html" echo -e "\t\tWrite output as HTML (default: ASCII)" echo -e "\t--title [STRING]" echo -e "\t\tUse the given string as the title and H1 header, if HTML output" echo -e "\t-h, --help" echo -e "\t\tPrint this usage help and exit" echo -e "Examples:" echo -e "\tfind * | $script_name" echo -e "" } # Process command line arguments while [[ $# -gt 0 ]] do arg="$1" shift # $2 -> $1, $3 -> $2, ... case "$arg" in -h|--help) print_help exit 0 ;; -o|--output) output="$1" shift ;; --html) html=true ;; --title) title="$1" shift ;; -*) # unknown switch >&2 echo "ERROR: Unknown switch '$arg'" exit 1 ;; *) # non-switch POSITIONAL+=("$arg") # save it in an array for later ;; esac done set -- "${POSITIONAL[@]}" # restore positional parameters lst_clean_tmp=$(mktemp "/tmp/$script_name-clean_listing-XXXXXX.txt") while IFS= read -r line do if [ -z "$line" ] || [[ $line =~ ^[#\;].* ]] then continue fi if [[ $line =~ (^\|/)\.\.(/\|$) ]] then >&2 echo "Invalid sequence '..' found in: '$line'" exit 1 fi if [[ $line =~ ^/ ]] then >&2 echo "Please use relative listing paths; found: '$line'" exit 1 fi echo "$line" done > "$lst_clean_tmp" tree_extra_args=() if $html then tree_extra_args+=("-H") tree_extra_args+=(".") tree_extra_args+=("--nolinks") if [ -n "$title" ] then tree_extra_args+=("-T") tree_extra_args+=("$title") fi fi tree \ -a \ -F \ --noreport \ -I ".git" \ --dirsfirst \ --fromfile \ "${tree_extra_args[@]}" \ -o "$output" \ . \ < "$lst_clean_tmp" rm "$lst_clean_tmp"