Skip to content

Commit

Permalink
fs-structure: handle missing and empty paths
Browse files Browse the repository at this point in the history
also remove unused code
  • Loading branch information
balupton committed Nov 30, 2024
1 parent c4d9ac6 commit b06ce34
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
2 changes: 2 additions & 0 deletions commands/fs-rm
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash

# @todo write tests, use fs-trim for reference

function fs_rm() (
source "$DOROTHY/sources/bash.bash"

Expand Down
55 changes: 31 additions & 24 deletions commands/fs-structure
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,12 @@ function fs_structure() (
if [[ $option_eza != 'no' ]] && __command_exists -- eza; then
# -h, --header: Add a header row to each column.
# -l, --long: Display extended file metadata as a table.
# -a, --all: show hidden and 'dot' files. Use this twice to also show the '.' and '..' directories
# -A, --almost-all: Equivalent to –all; included for compatibility with ls -A.
# -M, --mounts: how mount details (Linux and Mac only)
# --absolute: display entries with their absolute path (on, follow, off)
# --total-size: show the size of a directory as the size of all files and directories inside (unix only)
# don't do absolute for now, as not sure if it is better with or without it
eza_cmd+=(eza -hlAM --total-size)

# hide the permissions?
Expand Down Expand Up @@ -171,36 +174,40 @@ function fs_structure() (
function __wrap {
sudo-helper --inherit --no-wrap --sudo="$option_sudo" --user="$option_user" --group="$option_group" -- "$@"
}
function __act() (
# subshell to prevent multiple cds conflicting with each other
local path="$1" basename
if [[ -d $path ]]; then
cd "$path"
basename='.'
else
cd "$(dirname "$path")"
basename="$(basename "$path")"
fi
function __list {
local path="$1"
if [[ ${#eza_cmd[@]} -ne 0 ]]; then
__wrap "${eza_cmd[@]}" "$basename"
__wrap "${eza_cmd[@]}" "$path"
fi
if [[ ${#ls_cmd[@]} -ne 0 ]]; then
__wrap "${ls_cmd[@]}" "$basename"
__wrap "${ls_cmd[@]}" "$path"
fi
# if [[ ${#dust_sizes_cmd[@]} -ne 0 ]]; then
# __wrap "${dust_sizes_cmd[@]}" "$basename"
# fi
# if [[ ${#dust_counts_cmd[@]} -ne 0 ]]; then
# __wrap "${dust_counts_cmd[@]}" "$basename"
# fi
# if [[ ${#du_cmd[@]} -ne 0 ]]; then
# __wrap "${du_cmd[@]}" "$basename"
# fi
)
}

local input
local input path
for input in "${option_inputs[@]}"; do
__act "$input"
# check is invalid
if [[ -z $input ]]; then
return 22 # EINVAL 22 Invalid argument
fi

# just -e is faulty, as -e fails on broken symlinks
if is-missing --sudo="$option_sudo" --user="$option_user" --group="$option_group" -- "$input"; then
echo-style --error1='The path is missing: ' --code-error1="$input" >/dev/stderr
return 2 # ENOENT 2 No such file or directory
fi

# now that we know it exists, get its path
path="$(fs-absolute --sudo="$option_sudo" --user="$option_user" --group="$option_group" -- "$input")"

# if it is empty, note it, as otherwise the output of eza and ls is unintuitive
if is-empty-directory --sudo="$option_sudo" --user="$option_user" --group="$option_group" -- "$path"; then
echo-style --notice1='The directory is empty: ' --code-notice1="$path"
continue
fi

# it exists and isn't an empty directory, list its contents
__list "$path"
done
)

Expand Down

0 comments on commit b06ce34

Please sign in to comment.