Skip to content

Commit

Permalink
Merge pull request #21 from 0jonjo/19_refactor_variables
Browse files Browse the repository at this point in the history
19 refactor variables
  • Loading branch information
0jonjo authored Aug 3, 2024
2 parents cd01819 + 1f33397 commit 1a564ca
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ This command creates a file using the YYMMDD format.
./woche.sh <day> "<task>"
```

Replace <day> with the day of the week and <task> with the task description. The days in German are: mon, die, mit, don, frei, sam and son, in English: mond, tue, wed, thu, fri, sat and sun.
Replace <day> with the day of the week and <task> with the task description. The days in German are: mont, die, mit, don, frei, sam and son, in English: mon, tue, wed, thu, fri, sat and sun.

**Display the tasks of the current week:**

Expand Down
10 changes: 5 additions & 5 deletions functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ tips() {
echo "all: show all markdown files in the current directory"
}

start_day_of_week() {
start_day=$(date -d "last monday" "+%y%m%d")
current_week() {
current_week=$(date -d "last monday" "+%y%m%d")

if [ "$(date "+%u")" == 1 ]; then
start_day=$(date "+%y%m%d")
current_week=$(date "+%y%m%d")
fi
}

file_exists() {
if [ ! -e "$file.md" ]; then
echo "Error: The file $$file.md does not exist."
echo "Error: The file $file.md does not exist."
exit 1
fi
}

file_already_exists() {
if [ -e "$file.md" ]; then
echo "Error: The file $$file.md already exists."
echo "Error: The file $file.md already exists."
exit 1
fi
}
Expand Down
13 changes: 7 additions & 6 deletions tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ source variables.sh

woche_script_path="./woche.sh"

start_day_of_week
current_week
file=$current_week

check_test_result() {
if [[ "$output" == *"FAILED"* ]]; then
Expand All @@ -15,7 +16,7 @@ check_test_result() {

delete_file() {
cd "$path_to_files" > /dev/null
rm -f "$start_day.md"
rm -f "$current_week.md"
cd - > /dev/null
}

Expand Down Expand Up @@ -95,8 +96,8 @@ check_test_result

## Check if the file is created on last test
cd "$path_to_files" > /dev/null
if [ ! -e "$start_day.md" ]; then
echo "Error: The file $start_day.md does not exist."
if [ ! -e "$file.md" ]; then
echo "Error: The file $file.md does not exist."
exit 1
fi
cd - > /dev/null
Expand All @@ -121,7 +122,7 @@ check_test_result

## Check if the task is added on last test
cd "$path_to_files" > /dev/null
if [ -z "$(sed -n "/# $mon/ p" "$start_day.md")" ]; then
if [ -z "$(sed -n "/# $mon/ p" "$file.md")" ]; then
echo "Error: Task has not been added."
exit 1
fi
Expand All @@ -137,7 +138,7 @@ fi

## Check if the task is edited on last test
cd "$path_to_files" > /dev/null
if [ -z "$(sed -n "2 p" "$start_day.md" | grep "New task")" ]; then
if [ -z "$(sed -n "2 p" "$file.md" | grep "New task")" ]; then
echo "Error: Task has not been edited."
exit 1
fi
Expand Down
20 changes: 7 additions & 13 deletions woche.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ cd "$path_to_files" > /dev/null
day=""
task="$2"
new_task="$3"
start_day_of_week
current_week
file=$current_week

# Check the number of arguments
if [ "$#" -gt 3 ]; then
Expand All @@ -23,36 +24,30 @@ if [[ ! " ${options_to_check[@]} " =~ " $1 " ]]; then
exit 1
fi

# Set file variable to show option
if [ "$task" ]; then
file=$task
else
file=$start_day
fi

case $1 in
create)
file_already_exists
create_file
exit 0
;;
delete)
file=$start_day
file_exists
line_exists
delete_line
exit 0
;;
edit)
file=$start_day
file_exists
line_exists
edit_line
exit 0
;;
show)
file_exists $file
show_file $file
if [ "$task" ]; then
file=$task
fi
file_exists
show_file
exit 0
;;
all)
Expand All @@ -64,7 +59,6 @@ case $1 in
exit 0
;;
*)
file=$start_day
file_exists
day=$(eval echo \$$1)
sed -i "/# $day/ a\\- $task" "$file.md"
Expand Down

0 comments on commit 1a564ca

Please sign in to comment.