Skip to content

Commit a8a473a

Browse files
committed
chore: shfmt, cleanup
1 parent eb91a43 commit a8a473a

26 files changed

+300
-517
lines changed

config/alias

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,6 @@ X_ALIAS_FILES=(
130130
)
131131
for aliasFile in "${X_ALIAS_FILES[@]}"; do
132132
# shellcheck source=$HOME/.config/alias-secret
133-
[ -f "$aliasFile" ] && source "$aliasFile" && msg "Sourced $aliasFile"
133+
[ -f "$aliasFile" ] && source "$aliasFile" && msgr ok "Sourced $aliasFile"
134134
done
135135
unset X_ALIAS_FILES

config/shared.sh

+24-5
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ if ! declare -f msg > /dev/null; then
4242
# $1 - message (string)
4343
msg()
4444
{
45-
[ "$VERBOSE" -eq 1 ] && echo "$1"
45+
[ "$VERBOSE" -eq 1 ] && msgr msg "$1"
4646
return 0
4747
}
4848
msg "msg was not defined, defined it now"
@@ -54,7 +54,7 @@ if ! declare -f msg_err > /dev/null; then
5454
# $1 - error message (string)
5555
msg_err()
5656
{
57-
echo "(!) ERROR: $1" >&2
57+
msgr err "$1" >&2
5858
exit 1
5959
}
6060
fi
@@ -65,7 +65,7 @@ if ! declare -f msg_done > /dev/null; then
6565
# $1 - message (string)
6666
msg_done()
6767
{
68-
echo "$1"
68+
msgr done "$1"
6969
return 0
7070
}
7171
fi
@@ -76,7 +76,7 @@ if ! declare -f msg_run > /dev/null; then
7676
# $1 - message (string)
7777
msg_run()
7878
{
79-
echo "$1"
79+
msgr run "$1"
8080
return 0
8181
}
8282
fi
@@ -87,10 +87,29 @@ if ! declare -f msg_ok > /dev/null; then
8787
# $1 - message (string)
8888
msg_ok()
8989
{
90-
echo "$1"
90+
msgr ok "$1"
9191
return 0
9292
}
9393
fi
9494

95+
if ! declare -f array_diff > /dev/null; then
96+
# Function to compare two arrays and return the difference
97+
# Example: array_diff DIFFERENCE ARRAY1 ARRAY2
98+
# $1 - variable to store the difference
99+
# $2 - first array
100+
# $3 - second array
101+
# Output to $1 the difference between $2 and $3
102+
# Source: https://stackoverflow.com/a/42399479/594940
103+
array_diff()
104+
{
105+
# shellcheck disable=SC1083,SC2086
106+
eval local ARR1=\(\"\${$2[@]}\"\)
107+
# shellcheck disable=SC1083,SC2086
108+
eval local ARR2=\(\"\${$3[@]}\"\)
109+
local IFS=$'\n'
110+
mapfile -t "$1" < <(comm -23 <(echo "${ARR1[*]}" | sort) <(echo "${ARR2[*]}" | sort))
111+
}
112+
fi
113+
95114
source "$DOTFILES/config/exports"
96115
source "$DOTFILES/config/alias"

install

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ git submodule update --init --recursive "${DOTBOT_DIR}"
2525
if [ "${DOTBOT_HOST}" != "" ]; then
2626
DOTBOT_HOST_CONFIG="${BASEDIR}/hosts/${DOTBOT_HOST}/${CONFIG}"
2727
echo "-> Trying if host config can be found: ${DOTBOT_HOST_CONFIG}"
28-
[ -r "$DOTBOT_HOST_CONFIG" ] && [ -f "$DOTBOT_HOST_CONFIG" ] &&
29-
echo "(!) Found $DOTBOT_HOST_CONFIG" &&
30-
"$DOTBOT_BIN_PATH" \
28+
[ -r "$DOTBOT_HOST_CONFIG" ] && [ -f "$DOTBOT_HOST_CONFIG" ] \
29+
&& echo "(!) Found $DOTBOT_HOST_CONFIG" \
30+
&& "$DOTBOT_BIN_PATH" \
3131
-d "$BASEDIR" \
3232
--plugin-dir=tools/dotbot-asdf \
3333
--plugin-dir=tools/dotbot-brew \

local/bin/a

+21-14
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ for arg in "$@"; do
1919
done
2020

2121
# Ensure log directory and file exist with correct permissions
22-
prepare_log_file() {
22+
prepare_log_file()
23+
{
2324
local log_dir
2425
log_dir=$(dirname "$LOG_FILE")
2526

@@ -40,7 +41,8 @@ prepare_log_file() {
4041
prepare_log_file
4142

4243
# Logging function
43-
log_message() {
44+
log_message()
45+
{
4446
local message="$1"
4547
echo "$(date +'%Y-%m-%d %H:%M:%S') - $message" >> "$LOG_FILE"
4648

@@ -51,8 +53,9 @@ log_message() {
5153
}
5254

5355
# Function to print usage
54-
print_help() {
55-
cat <<EOF
56+
print_help()
57+
{
58+
cat << EOF
5659
Usage: a [command] [file_or_directory] [options]
5760
5861
Commands:
@@ -88,13 +91,15 @@ EOF
8891
}
8992

9093
# Function to print version
91-
print_version() {
94+
print_version()
95+
{
9296
echo "a version $VERSION"
9397
echo "Created by Ismo Vuorinen <https://github.com/ivuorinen>"
9498
}
9599

96100
# Function to fetch keys if missing
97-
fetch_keys_if_missing() {
101+
fetch_keys_if_missing()
102+
{
98103
if [[ ! -f "$KEYS_FILE" ]]; then
99104
log_message "Keys file '$KEYS_FILE' not found. Attempting to fetch from $KEYS_SOURCE..."
100105
mkdir -p "$(dirname "$KEYS_FILE")"
@@ -111,7 +116,8 @@ fetch_keys_if_missing() {
111116
}
112117

113118
# Function to encrypt files or directories
114-
encrypt_file_or_directory() {
119+
encrypt_file_or_directory()
120+
{
115121
local file="$1"
116122
if [[ -d "$file" ]]; then
117123
for f in "$file"/*; do
@@ -120,7 +126,7 @@ encrypt_file_or_directory() {
120126
elif [[ -f "$file" ]]; then
121127
fetch_keys_if_missing
122128
local output_file="${file}.age"
123-
age -R "$KEYS_FILE" "$file" >"$output_file"
129+
age -R "$KEYS_FILE" "$file" > "$output_file"
124130
if [[ $? -eq 0 ]]; then
125131
log_message "File encrypted successfully: $output_file"
126132
else
@@ -131,7 +137,8 @@ encrypt_file_or_directory() {
131137
}
132138

133139
# Function to decrypt files or directories
134-
decrypt_file_or_directory() {
140+
decrypt_file_or_directory()
141+
{
135142
local file="$1"
136143
if [[ -d "$file" ]]; then
137144
for f in "$file"/*.age; do
@@ -140,7 +147,7 @@ decrypt_file_or_directory() {
140147
elif [[ -f "$file" ]]; then
141148
fetch_keys_if_missing
142149
local output_file="${file%.age}"
143-
age -d -i "$KEYS_FILE" "$file" >"$output_file"
150+
age -d -i "$KEYS_FILE" "$file" > "$output_file"
144151
if [[ $? -eq 0 ]]; then
145152
log_message "File decrypted successfully: $output_file"
146153
else
@@ -152,26 +159,26 @@ decrypt_file_or_directory() {
152159

153160
# Main logic
154161
case "$1" in
155-
e|enc|encrypt)
162+
e | enc | encrypt)
156163
if [[ $# -lt 2 ]]; then
157164
log_message "Error: No file or directory specified for encryption."
158165
print_help
159166
exit 1
160167
fi
161168
encrypt_file_or_directory "$2"
162169
;;
163-
d|dec|decrypt)
170+
d | dec | decrypt)
164171
if [[ $# -lt 2 ]]; then
165172
log_message "Error: No file or directory specified for decryption."
166173
print_help
167174
exit 1
168175
fi
169176
decrypt_file_or_directory "$2"
170177
;;
171-
help|--help)
178+
help | --help)
172179
print_help
173180
;;
174-
version|--version)
181+
version | --version)
175182
print_version
176183
;;
177184
*)

local/bin/ad

+3-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ KEYS_FILE="${AGE_KEYSFILE:-$HOME/.ssh/keys.txt}"
66
KEYS_SOURCE="${AGE_KEYSSOURCE:-https://github.com/ivuorinen.keys}"
77

88
# Check for required commands
9-
if ! command -v age &>/dev/null; then
9+
if ! command -v age &> /dev/null; then
1010
echo "Error: age is not installed. Please install it to continue."
1111
exit 1
1212
fi
1313

14-
if ! command -v curl &>/dev/null; then
14+
if ! command -v curl &> /dev/null; then
1515
echo "Error: curl is not installed. Please install it to continue."
1616
exit 1
1717
fi
@@ -28,7 +28,6 @@ if [[ ! -f "$FILE" ]]; then
2828
exit 1
2929
fi
3030

31-
3231
# Check if keys file exists, otherwise fetch it
3332
if [[ ! -f "$KEYS_FILE" ]]; then
3433
echo "Keys file '$KEYS_FILE' not found. Attempting to fetch from $KEYS_SOURCE..."
@@ -51,12 +50,11 @@ fi
5150

5251
# Decrypt the file
5352
OUTPUT_FILE="${FILE%.age}"
54-
age -d -i "$KEYS_FILE" "$FILE" >"$OUTPUT_FILE"
53+
age -d -i "$KEYS_FILE" "$FILE" > "$OUTPUT_FILE"
5554

5655
if [[ $? -eq 0 ]]; then
5756
echo "File decrypted successfully: $OUTPUT_FILE"
5857
else
5958
echo "Error: Failed to decrypt file."
6059
exit 1
6160
fi
62-

local/bin/ae

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ KEYS_FILE="${AGE_KEYSFILE:-$HOME/.ssh/keys.txt}"
66
KEYS_SOURCE="${AGE_KEYSSOURCE:-https://github.com/ivuorinen.keys}"
77

88
# Check for required commands
9-
if ! command -v age &>/dev/null; then
9+
if ! command -v age &> /dev/null; then
1010
echo "Error: age is not installed. Please install it to continue."
1111
exit 1
1212
fi
1313

14-
if ! command -v curl &>/dev/null; then
14+
if ! command -v curl &> /dev/null; then
1515
echo "Error: curl is not installed. Please install it to continue."
1616
exit 1
1717
fi
1818

1919
# Ensure a file is provided
20+
# shellcheck disable=SC2181
2021
if [[ $# -lt 1 ]]; then
2122
echo "Usage: $0 <file-to-encrypt>"
2223
exit 1
@@ -50,7 +51,7 @@ fi
5051

5152
# Encrypt the file
5253
OUTPUT_FILE="${FILE}.age"
53-
age -R "$KEYS_FILE" "$FILE" >"$OUTPUT_FILE"
54+
age -R "$KEYS_FILE" "$FILE" > "$OUTPUT_FILE"
5455

5556
if [[ $? -eq 0 ]]; then
5657
echo "File encrypted successfully: $OUTPUT_FILE"

0 commit comments

Comments
 (0)