Skip to content

Commit

Permalink
feat: create logging, provide mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
JowiAoun committed Oct 7, 2024
1 parent 6bf018f commit 038299b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
9 changes: 5 additions & 4 deletions bin/dirclean.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ source ./lib/actions.sh
# Main function to the program.
function main() {

# Create default argument values
# Set default argument values
directory=.
log=false
dry_run=false
Expand All @@ -30,13 +30,14 @@ function main() {
shift $((OPTIND - 1))

# Create mapping of current directories to new directories
create_mapping "$directory" "$log"
declare -A _mapping
create_mapping _mapping "$directory"

# Organize
if [[ $dry_run == false ]]; then
organize #TODO: provide mapping
organize _mapping "$log"
else
echo "This was a dry run, therefore no modifications were completed."
echo "Because this was a dry run, no modifications were completed."
fi
}

Expand Down
42 changes: 38 additions & 4 deletions lib/actions.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,50 @@
#!/bin/bash

# shellcheck disable=SC1091
source ./lib/interface.sh

LOG_DIR=logs

# Create a mapping of current file & directory positions, and their new position.
function create_mapping() {
# declare -A dir_map
declare -n mapping="$1"

cd "$1" || exit 1;
cd "$2" || exit 1;

files=$(ls)

echo "$files"
while IFS= read -r line; do
if [[ -f "$line" ]]; then
# extension="${line##*.}"

mapping["$line"]="someDir/$line" #TODO: define a new directory based on extension

# echo "$line -> ${mapping[$line]}"

elif [[ -d "$line" ]]; then
mapping["$line"]="directories/$line"

# echo "$line -> ${mapping[$line]}"

else
print_invalid_file_or_dir "$line" ; exit 1
fi
done <<< "$files"
}

function organize() {
echo "Organize" #TODO
# shellcheck disable=SC2178
declare -n mapping="$1"

current_datetime=$(date +"%Y-%m-%d-%H:%M:%S")

echo "Organizing..."

if [[ "$2" == true && ! -d $LOG_DIR ]]; then
mkdir $LOG_DIR
fi

for key in "${!mapping[@]}"; do
echo "$key -> ${mapping[$key]}" | tee -a "$LOG_DIR/$current_datetime.log"
done
}
6 changes: 3 additions & 3 deletions lib/interface.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ function print_invalid_value() {
echo "Option '-$1' requires an argument" >&2
}

function print_run() {
echo "Run" #TODO, and pick up dry_run flag
}
function print_invalid_file_or_dir() {
echo "File '$1' does not exist" >&2
}

0 comments on commit 038299b

Please sign in to comment.