-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.sh
More file actions
executable file
·144 lines (117 loc) · 2.94 KB
/
core.sh
File metadata and controls
executable file
·144 lines (117 loc) · 2.94 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
#!/usr/bin/env bash
set -e
# Colors
BOLD='\033[1m'
CYAN='\033[36m'
DIM='\033[2m'
RED='\033[31m'
RESET='\033[0m'
# Print error and exit
error() {
echo -e "${RED}Error:${RESET} $1" >&2
exit 1
}
# Shared setup (used by both init and clone)
do_setup() {
cd "$HOME"
# Guards
[[ -d ".jj" ]] && error "Already initialized. Remove ~/.jj to re-initialize."
# Check jj is installed
command -v jj >/dev/null 2>&1 || error "jj is not installed. See https://github.com/martinvonz/jj"
# Initialize jj repo
jj git init --no-colocate
# Create .nadm directory
[[ -d ".nadm" ]] || mkdir -p .nadm
# Create config with add/sync aliases
cat > .nadm/config.toml << 'EOF'
[aliases]
add = ["util", "exec", "--", "bash", "-c", """
#!/usr/bin/env bash
printf '%s\n' "$@" >> .nadm/tracked
jj sync
""", ""]
sync = ["util", "exec", "--", "bash", "-c", """
#!/usr/bin/env bash
if [[ ! -f .nadm/tracked ]]; then
exit 0
fi
jj sparse set --clear
jj sparse set --add .nadm/
while IFS= read -r path; do
[[ -z "$path" ]] && continue
jj sparse set --add "$path"
done < .nadm/tracked
""", ""]
EOF
# Remove auto-generated config if it exists
rm -f .jj/repo/config.toml
# Symlink config
ln -s "$HOME/.nadm/config.toml" .jj/repo/config.toml
echo '*' > .gitignore
jj sparse set --clear
rm .gitignore
jj add .nadm
}
show_usage() {
echo "Usage: nadm <command>"
echo ""
echo "Commands:"
echo " init"
echo " clone <url>"
echo " clone --github <owner/repo>"
}
cmd_init() {
echo -e "${BOLD}Initializing nadm...${RESET}"
do_setup
echo
echo -e "${CYAN}Done!${RESET} Your home directory is now a jj repo."
echo "Use 'jj add <file>' to start tracking dotfiles."
echo
jj status
}
cmd_clone() {
local url="$1"
[[ -z "$url" ]] && error "Usage: nadm clone <url>"
echo -e "${BOLD}Cloning dotfiles...${RESET}"
do_setup
jj git remote add origin $url
jj git fetch
echo
echo -e "${CYAN}Done!${RESET} Your home directory is now a jj repo."
echo "Use 'jj add <file>' to start tracking dotfiles."
echo
jj status
}
main() {
local -a args=()
if [[ $# -gt 0 ]]; then
args=("$@")
elif [[ -n "${NADM_ARGS:-}" ]]; then
read -r -a args <<< "${NADM_ARGS}"
fi
local cmd="${args[0]:-}"
case "$cmd" in
init)
cmd_init
;;
clone)
if [[ "${args[1]:-}" == "--github" ]]; then
local repo="${args[2]:-}"
[[ -z "$repo" ]] && error "Usage: nadm clone --github <owner/repo>"
cmd_clone "https://github.com/${repo}.git"
else
cmd_clone "${args[1]:-}"
fi
;;
""|help|--help|-h)
show_usage
;;
*)
error "Unknown command: $cmd"
;;
esac
}
# Run if executed directly (not sourced)
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi