Skip to content

Commit cdffd51

Browse files
Release v2.1.0 (#30)
1 parent 75687e1 commit cdffd51

File tree

6 files changed

+230
-124
lines changed

6 files changed

+230
-124
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## Changelog
22

3+
## 2.1.0
4+
5+
* Added function to remove backed up folder from config
6+
* Safety check to prevent adding the same folder twice
7+
38
## 2.0.0
49

510
* Only one repo needed

backup.sh

100755100644
File mode changed.

kgb.sh

100755100644
File mode changed.

lib/functions/config_folders.sh

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
#!/bin/bash
2+
3+
detect_printer_data() {
4+
### Detects all directories in ${HOME} thet end in ´_data´
5+
6+
local printer_data
7+
local re='^.*_data/$'
8+
9+
### Loop over all directories in ${HOME}
10+
for dir in "${HOME}"/*/; do
11+
### If the directory matches, add it to a list
12+
if [[ ${dir} =~ ${re} ]]; then
13+
printer_data+=("${dir}")
14+
fi
15+
done
16+
17+
### Return all found directories
18+
echo "${printer_data[*]}"
19+
}
20+
21+
add_config_folder() {
22+
### Add config folders to the list
23+
24+
local input
25+
26+
### Auto detect printer_data folders
27+
data=$(detect_printer_data)
28+
29+
### Make the detected printer data folders an array
30+
mapfile -t auto_detected_dirs < <(echo "${data}" | tr ' ' "\n")
31+
32+
### List auto detected folders
33+
success_msg "Detected the following config directories:"
34+
for i in "${!auto_detected_dirs[@]}"; do
35+
echo -e "$((i + 1))) ${auto_detected_dirs[${i}]}"
36+
done
37+
38+
### Loop until user input is valid
39+
echo -e "${PURPLE}Which folder should be included in the backup?${NC}"
40+
echo -e "${PURPLE}Choose only one per prompt${NC}"
41+
echo -e "${PURPLE}'all' for all detected folders${NC}"
42+
echo -e "${PURPLE}'stop' to stop adding folders${NC}"
43+
while true; do
44+
### Prompt user for input
45+
read -r -p "$(echo -e "${PURPLE}Add folder: ${NC}")" -i "all" -e input
46+
### Validate user input
47+
case ${input} in
48+
[0-9]*)
49+
### Add directory to list
50+
if [[ ${input} -le ${#auto_detected_dirs[@]} ]]; then
51+
IFS="%"
52+
if [[ "${IFS}${CONFIG_FOLDER_LIST[*]}${IFS}" =~ ${IFS}${auto_detected_dirs[$((input - 1))]}${IFS} ]]; then
53+
warning_msg "Selected folder already exists in config"
54+
unset IFS
55+
else
56+
unset IFS
57+
CONFIG_FOLDER_LIST+=("${auto_detected_dirs[$((input - 1))]}")
58+
success_msg "Added ${auto_detected_dirs[$((input - 1))]}"
59+
fi
60+
else
61+
### Invalid input
62+
deny_action
63+
fi
64+
;;
65+
stop)
66+
### Stop adding direcories
67+
break
68+
;;
69+
all)
70+
for dir in "${auto_detected_dirs[@]}"; do
71+
IFS="%"
72+
if [[ "${IFS}${CONFIG_FOLDER_LIST[*]}${IFS}" =~ ${IFS}${dir}${IFS} ]]; then
73+
warning_msg "Selected folder already exists in config"
74+
unset IFS
75+
else
76+
unset IFS
77+
CONFIG_FOLDER_LIST+=("${dir}")
78+
success_msg "Added ${dir}"
79+
fi
80+
done
81+
break
82+
;;
83+
*)
84+
### Invalid input
85+
deny_action
86+
;;
87+
esac
88+
done && input=""
89+
}
90+
91+
add_additional_dirs() {
92+
### Add additional directories to config folder list
93+
94+
local input
95+
96+
### Loop until user input is valid
97+
while true; do
98+
### Prompt user for input
99+
input=""
100+
read -r -p "$(echo -e "${PURPLE}Do you want to add additional directories? [y|n] ${NC}")" -i "n" -e input
101+
### Validate user input
102+
case ${input} in
103+
y | Y)
104+
### Add directory to list
105+
input=""
106+
107+
### Prompt user for input
108+
read -r -p "$(echo -e "${PURPLE}Enter the path of config folder: ${NC}")" input
109+
### Expand user input to full paths
110+
#! Only if the user didn't input a full path
111+
if ! echo "${input}" | grep -q "^/"; then
112+
### Check if ´~´ was input and replace it with ${HOME}
113+
if echo "${input}" | grep -q "^~"; then
114+
input=${input/\~/${HOME}}
115+
else
116+
### Relative path detected
117+
#! Prepend ${HOME} in the hope that the user set up klipper as a standard installation
118+
warning_msg "Relative path detected. Assuming relative to ${HOME}"
119+
input="${HOME}/${input}"
120+
fi
121+
fi
122+
123+
### Append input to the config folder list
124+
IFS="%"
125+
if [[ "${IFS}${CONFIG_FOLDER_LIST[*]}${IFS}" =~ ${IFS}${input}${IFS} ]]; then
126+
warning_msg "Selected folder already exists in config"
127+
unset IFS
128+
else
129+
unset IFS
130+
CONFIG_FOLDER_LIST+=("${input}")
131+
success_msg "Added ${input}"
132+
fi
133+
134+
### Reset input to prevent duplicate entries
135+
input=""
136+
;;
137+
n | N)
138+
### Stop adding direcories
139+
break
140+
;;
141+
*)
142+
### Invalid input
143+
deny_action
144+
;;
145+
esac
146+
done && input=""
147+
}
148+
149+
remove_config_folder() {
150+
### Removes config folders from list
151+
152+
local input
153+
local del_dir
154+
155+
### Loop until user input is valid
156+
while true; do
157+
### Print all current folders
158+
info_msg "The following folders are currently in config:"
159+
for i in "${!CONFIG_FOLDER_LIST[@]}"; do
160+
echo -e "$((i + 1))) ${CONFIG_FOLDER_LIST[${i}]}"
161+
done
162+
### Prompt user for input
163+
echo -e "${PURPLE}'stop' to stop removing folders${NC}"
164+
read -r -p "$(echo -e "${PURPLE}Which folder do you want to remove? ${NC}")" input
165+
### Validate user input
166+
case ${input} in
167+
[0-9]*)
168+
if [[ ${input} -le ${#auto_detected_dirs[@]} ]]; then
169+
### Loop over all folders
170+
#! Skip input
171+
for i in "${!CONFIG_FOLDER_LIST[@]}"; do
172+
if [[ ${i} -ne $((input - 1)) ]]; then
173+
tmp_array+=("${CONFIG_FOLDER_LIST[${i}]}")
174+
else
175+
del_dir="${CONFIG_FOLDER_LIST[${i}]}"
176+
fi
177+
done
178+
CONFIG_FOLDER_LIST=("${tmp_array[@]}")
179+
success_msg "Removed ${del_dir} from backed up folders"
180+
else
181+
### Invalid input
182+
deny_action
183+
fi
184+
;;
185+
stop)
186+
break
187+
;;
188+
*)
189+
### Invalid input
190+
deny_action
191+
;;
192+
esac
193+
done && input=""
194+
}
195+
196+
config_folders() {
197+
### Configure folders to be backed up
198+
199+
local input
200+
201+
### Loop until user input is valid
202+
while true; do
203+
### Prompt user for input
204+
read -r -p "$(echo -e "${PURPLE}Do you want to add or remove folders from the config? ${NC}")" -i "add" -e input
205+
### Validate user input
206+
case ${input} in
207+
add)
208+
add_config_folder
209+
add_additional_dirs
210+
break
211+
;;
212+
remove)
213+
remove_config_folder
214+
break
215+
;;
216+
*)
217+
### Invalid input
218+
deny_action
219+
;;
220+
esac
221+
done && input=""
222+
}

lib/functions/migrate.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ migrate_config() {
66

77
local input
88
local new_dir
9-
local tmp_arr
9+
local tmp_array
1010

1111
### Check if requirements are met
1212
if [[ ! -f ${HOME}/.secrets/gh-token ]]; then
@@ -61,11 +61,11 @@ migrate_config() {
6161
### Fix config folder list
6262
for dir in "${CONFIG_FOLDER_LIST[@]}"; do
6363
### Only leave path to instance folder intact
64-
tmp_arr+=("${dir//config/}")
64+
tmp_array+=("${dir//config/}")
6565
done
6666

6767
### Reassign array
68-
CONFIG_FOLDER_LIST=("${tmp_arr[@]}")
68+
CONFIG_FOLDER_LIST=("${tmp_array[@]}")
6969

7070
### Save config
7171
save_config

lib/functions/misc.sh

Lines changed: 0 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -106,124 +106,3 @@ check_install() {
106106
done && input=""
107107
fi
108108
}
109-
110-
detect_printer_data() {
111-
### Detects all directories in ${HOME} thet end in ´_data´
112-
113-
local printer_data
114-
local re='^.*_data/$'
115-
116-
### Loop over all directories in ${HOME}
117-
for dir in "${HOME}"/*/; do
118-
### If the directory matches, add it to a list
119-
if [[ ${dir} =~ ${re} ]]; then
120-
printer_data+=("${dir}")
121-
fi
122-
done
123-
124-
### Return all found directories
125-
echo "${printer_data[*]}"
126-
}
127-
128-
config_folders() {
129-
### Configure folders to be backed up
130-
131-
local input
132-
local data
133-
local auto_detected_dirs
134-
135-
### Auto detect printer_data folders
136-
data=$(detect_printer_data)
137-
138-
### Make the detected printer data folders an array
139-
mapfile -t auto_detected_dirs < <(echo "${data}" | tr ' ' "\n")
140-
141-
### List auto detected folders
142-
success_msg "Detected the following config directories:"
143-
for i in "${!auto_detected_dirs[@]}"; do
144-
echo -e "$((i + 1))) ${auto_detected_dirs[${i}]}"
145-
done
146-
147-
### Loop until user input is valid
148-
echo -e "${PURPLE}Which directories should be included in the backup?${NC}"
149-
echo -e "${PURPLE}Choose only one per prompt${NC}"
150-
echo -e "${PURPLE}'y' for all detected folders${NC}"
151-
while true; do
152-
### Prompt user for input
153-
input=""
154-
read -r -p "$(echo -e "${PURPLE}Add direcory number (x to stop adding): ${NC}")" input
155-
### Validate user input
156-
case ${input} in
157-
[0-9]*)
158-
### Add directory to list
159-
if [[ ${input} -le ${#auto_detected_dirs[@]} ]]; then
160-
CONFIG_FOLDER_LIST+=("${auto_detected_dirs[$((input - 1))]}")
161-
success_msg "Added ${auto_detected_dirs[$((input - 1))]}"
162-
else
163-
### Invalid input
164-
deny_action
165-
fi
166-
;;
167-
x | X)
168-
### Stop adding direcories
169-
break
170-
;;
171-
y | Y)
172-
for dir in "${auto_detected_dirs[@]}"; do
173-
CONFIG_FOLDER_LIST+=("${dir}")
174-
success_msg "Added ${dir}"
175-
done
176-
break
177-
;;
178-
*)
179-
### Invalid input
180-
deny_action
181-
;;
182-
esac
183-
done && input=""
184-
185-
### Add additional directories
186-
while true; do
187-
### Prompt user for input
188-
input=""
189-
read -r -p "$(echo -e "${PURPLE}Do you want to add additional directories? [y|n] ${NC}")" -i "n" -e input
190-
### Validate user input
191-
case ${input} in
192-
y | Y)
193-
### Add directory to list
194-
input=""
195-
196-
### Prompt user for input
197-
read -r -p "$(echo -e "${PURPLE}Enter the path of config folder: ${NC}")" input
198-
### Expand user input to full paths
199-
#! Only if the user didn't input a full path
200-
if ! echo "${input}" | grep -q "^/"; then
201-
### Check if ´~´ was input and replace it with ${HOME}
202-
if echo "${input}" | grep -q "^~"; then
203-
input=${input/\~/${HOME}}
204-
else
205-
### Relative path detected
206-
#! Prepend ${HOME} in the hope that the user set up klipper as a standard installation
207-
warning_msg "Relative path detected. Assuming relative to ${HOME}"
208-
input="${HOME}/${input}"
209-
fi
210-
fi
211-
212-
### Append input to the config folder list
213-
CONFIG_FOLDER_LIST+=("${input}")
214-
success_msg "${input} has been added to the list"
215-
216-
### Reset input to prevent duplicate entries
217-
input=""
218-
;;
219-
n | N)
220-
### Stop adding direcories
221-
break
222-
;;
223-
*)
224-
### Invalid input
225-
deny_action
226-
;;
227-
esac
228-
done && input=""
229-
}

0 commit comments

Comments
 (0)