-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcops.sh
More file actions
executable file
·221 lines (204 loc) · 5.02 KB
/
Copy pathcops.sh
File metadata and controls
executable file
·221 lines (204 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/bin/bash
# shellcheck shell=bash
# Exit on error or undefined variable
set -e
set -u
# Check for yq installation (needed for YAML parsing)
if ! command -v yq &>/dev/null; then
echo "yq is required but not installed. Installing..."
if command -v brew &>/dev/null; then
brew install yq
else
echo "Error: brew not found. Please install Homebrew first."
exit 1
fi
fi
# Function to find config file with .yaml or .yml extension
find_config_file() {
local base_path="$1"
# Check exact path first
if [[ -f "$base_path" ]]; then
echo "$base_path"
return 0
fi
# If no extension provided, try both
if [[ "$base_path" != *.yml && "$base_path" != *.yaml ]]; then
if [[ -f "${base_path}.yaml" ]]; then
echo "${base_path}.yaml"
return 0
elif [[ -f "${base_path}.yml" ]]; then
echo "${base_path}.yml"
return 0
fi
fi
return 1
}
# Global configuration
CONFIG_FILE="config.yaml"
export AUTO_AGREE
# Parse command line arguments
AUTO_AGREE=false
COMMAND=""
TYPE=""
FILE=""
TIMESTAMP=""
DRY_RUN=false
TEST_MODE=false
print_usage() {
printf "Usage: %s [OPTIONS]\n" "$0"
printf "\nOptions:\n"
printf " --config-file <path> Specify custom config file (default: config.yaml)\n"
printf " --auto-agree Skip confirmation prompts\n"
printf " --dry-run Show what would be done without making changes\n"
printf " --test Run tests for restore functionality\n"
printf " --list-backups [type] List available backups (optional: specify type)\n"
printf " --restore type file Restore a specific file\n"
printf " --restore-all timestamp Restore all files from a timestamp\n"
printf "\nBackup types: preferences, shell, git, vim\n"
}
while [[ $# -gt 0 ]]; do
case $1 in
--config-file)
if [[ $# -lt 2 ]]; then
print_error "Missing path for --config-file"
print_usage
exit 1
fi
if config_path=$(find_config_file "$2"); then
CONFIG_FILE="$config_path"
else
print_error "Config file not found: $2 (tried both .yaml and .yml)"
exit 1
fi
shift 2
;;
--auto-agree)
AUTO_AGREE=true
shift
;;
--dry-run)
DRY_RUN=true
shift
;;
--test)
COMMAND="test"
TEST_MODE=true
shift
;;
--list-backups)
COMMAND="list"
if [[ $# -gt 1 && ! $2 =~ ^-- ]]; then
TYPE="$2"
shift
fi
shift
;;
--restore)
if [[ $# -lt 3 ]]; then
print_error "Missing arguments for --restore"
print_usage
exit 1
fi
COMMAND="restore"
TYPE="$2"
FILE="$3"
shift 3
;;
--restore-all)
if [[ $# -lt 2 ]]; then
print_error "Missing timestamp for --restore-all"
print_usage
exit 1
fi
COMMAND="restore-all"
TIMESTAMP="$2"
shift 2
;;
-h | --help)
print_usage
exit 0
;;
*)
print_error "Unknown option: $1"
print_usage
exit 1
;;
esac
done
COPS_ROOT=$(yq eval '.paths.cops' "$CONFIG_FILE" | envsubst)
export COPS_ROOT
# Source all library files from lib directory
LIB_DIR="lib"
# shellcheck source=lib/output.sh
# shellcheck disable=SC1091
source "${LIB_DIR}/output.sh"
# shellcheck source=lib/validate.sh
# shellcheck disable=SC1091
source "${LIB_DIR}/validate.sh"
# shellcheck source=lib/config.sh
# shellcheck disable=SC1091
source "${LIB_DIR}/config.sh"
# shellcheck source=lib/checks.sh
# shellcheck disable=SC1091
source "${LIB_DIR}/checks.sh"
# shellcheck source=lib/setup.sh
# shellcheck disable=SC1091
source "${LIB_DIR}/setup.sh"
# shellcheck source=lib/install.sh
# shellcheck disable=SC1091
source "${LIB_DIR}/install.sh"
# shellcheck source=lib/preferences-discovery.sh
# shellcheck disable=SC1091
source "${LIB_DIR}/preferences-discovery.sh"
# shellcheck source=lib/preferences.sh
# shellcheck disable=SC1091
source "${LIB_DIR}/preferences.sh"
# shellcheck source=lib/restore.sh
# shellcheck disable=SC1091
source "${LIB_DIR}/restore.sh"
# shellcheck source=lib/brewbundle.sh
# shellcheck disable=SC1091
source "${LIB_DIR}/brewbundle.sh"
# shellcheck source=lib/aliases.sh
# shellcheck disable=SC1091
source "${LIB_DIR}/aliases.sh"
# shellcheck source=lib/spotlight.sh
# shellcheck disable=SC1091
source "${LIB_DIR}/spotlight.sh"
# shellcheck source=lib/main.sh
# shellcheck disable=SC1091
source "${LIB_DIR}/main.sh"
# Export test mode for library files
export TEST_MODE
# Check dependencies first
if ! check_dependencies; then
print_error "Dependency check failed - aborting"
exit 1
fi
# Validate configuration file before proceeding
export CONFIG_FILE
if ! validate_config_file "$CONFIG_FILE"; then
print_error "Configuration validation failed - aborting"
exit 1
fi
# Handle restore commands if present
if [[ -n "$COMMAND" ]]; then
case "$COMMAND" in
list)
list_backups "$TYPE"
;;
restore)
restore_file "$TYPE" "$FILE" "" "$DRY_RUN"
;;
restore-all)
restore_all "$TIMESTAMP" "$DRY_RUN"
;;
test)
print_header "Running restore tests"
test_restore
;;
esac
exit 0
fi
# Run the main installation if no restore command
main