-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmultigravity
More file actions
1158 lines (977 loc) · 27.1 KB
/
Copy pathmultigravity
File metadata and controls
1158 lines (977 loc) · 27.1 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
set -euo pipefail
shopt -s nullglob
BASE="${MULTIGRAVITY_HOME:-$HOME/AntigravityProfiles}"
cmd="${1-}"
abort() {
echo "Error: $1" >&2
exit 1
}
print_info() {
echo "$1"
}
platform() {
local raw
raw="${MULTIGRAVITY_PLATFORM-}"
if [ -z "$raw" ]; then
raw="$(uname -s)"
fi
raw="$(printf '%s' "$raw" | tr '[:upper:]' '[:lower:]')"
case "$raw" in
darwin | macos | mac | osx)
printf '%s\n' "darwin"
;;
linux)
printf '%s\n' "linux"
;;
*)
abort "unsupported platform '$raw'. Multigravity supports macOS and Linux."
;;
esac
}
profile_dir() {
printf '%s/%s\n' "$BASE" "$1"
}
user_data_dir() {
local profile_path=$1
case "$(platform)" in
darwin)
printf '%s/Library/Application Support/Antigravity\n' "$profile_path"
;;
linux)
printf '%s/.config/Antigravity\n' "$profile_path"
;;
esac
}
extensions_dir() {
local profile_path=$1
printf '%s/.antigravity/extensions\n' "$profile_path"
}
default_user_data_dir() {
case "$(platform)" in
darwin)
printf '%s/Library/Application Support/Antigravity\n' "$HOME"
;;
linux)
printf '%s/.config/Antigravity\n' "$HOME"
;;
esac
}
default_extensions_dir() {
printf '%s/.antigravity/extensions\n' "$HOME"
}
templates_dir() {
printf '%s/.templates\n' "$BASE"
}
is_auth_only() {
[ -f "$(profile_dir "$1")/.auth-only" ]
}
linux_launcher_dir() {
printf '%s/.local/share/multigravity/launchers\n' "$HOME"
}
linux_launcher_path() {
printf '%s/%s.sh\n' "$(linux_launcher_dir)" "$1"
}
linux_desktop_path() {
printf '%s/.local/share/applications/multigravity-%s.desktop\n' "$HOME" "$1"
}
mac_shortcut_path() {
printf '%s/Applications/Multigravity %s.app\n' "$HOME" "$1"
}
validate_name() {
local name=$1
if [[ -z "$name" ]]; then
abort "profile name required"
fi
if ! [[ "$name" =~ ^[a-zA-Z0-9][a-zA-Z0-9-]*$ ]]; then
abort "profile name must start with alphanumeric and contain only letters, numbers, or hyphens"
fi
}
command_or_path() {
local candidate=$1
if [ -z "$candidate" ]; then
return 1
fi
if [ -d "$candidate" ] || [ -x "$candidate" ]; then
printf '%s\n' "$candidate"
return 0
fi
if command -v "$candidate" >/dev/null 2>&1; then
command -v "$candidate"
return 0
fi
return 1
}
find_app() {
local override candidate
override="${MULTIGRAVITY_APP-}"
if [ -n "$override" ] && command_or_path "$override" >/dev/null 2>&1; then
command_or_path "$override"
return 0
fi
case "$(platform)" in
darwin)
for candidate in \
"/Applications/Antigravity.app" \
"$HOME/Applications/Antigravity.app"
do
if [ -d "$candidate" ]; then
printf '%s\n' "$candidate"
return 0
fi
done
;;
linux)
for candidate in \
"antigravity" \
"/usr/share/antigravity/antigravity" \
"/usr/bin/antigravity" \
"/usr/local/bin/antigravity" \
"$HOME/.local/bin/antigravity" \
"$HOME/Applications/Antigravity.AppImage"
do
if command_or_path "$candidate" >/dev/null 2>&1; then
command_or_path "$candidate"
return 0
fi
done
;;
esac
return 1
}
require_app() {
local app
if ! app="$(find_app)"; then
case "$(platform)" in
darwin)
abort "Antigravity.app was not found. Install Antigravity or set MULTIGRAVITY_APP to the app bundle path."
;;
linux)
abort "Antigravity was not found. Install the Linux app so 'antigravity' is on PATH, or set MULTIGRAVITY_APP to the executable path."
;;
esac
fi
printf '%s\n' "$app"
}
create_profile_layout() {
local profile_path=$1
mkdir -p "$profile_path"
mkdir -p "$(extensions_dir "$profile_path")"
case "$(platform)" in
darwin)
mkdir -p "$profile_path/Library/Application Support"
if [ ! -e "$profile_path/Library/Keychains" ] && [ -d "$HOME/Library/Keychains" ]; then
ln -s "$HOME/Library/Keychains" "$profile_path/Library/Keychains"
fi
;;
linux)
mkdir -p \
"$profile_path/.config/Antigravity" \
"$profile_path/.cache" \
"$profile_path/.local/share" \
"$profile_path/.local/state"
;;
esac
}
create_auth_only_layout() {
local profile_path=$1
local default_data default_ext
default_data="$(default_user_data_dir)"
default_ext="$(default_extensions_dir)"
mkdir -p "$profile_path"
# Mark as auth-only
touch "$profile_path/.auth-only"
# Create user data dir (holds isolated auth/account state)
local data_dir
data_dir="$(user_data_dir "$profile_path")"
mkdir -p "$data_dir/User"
# Symlink shared settings from default installation
if [ -d "$default_data/User" ]; then
for item in settings.json keybindings.json snippets; do
local src="$default_data/User/$item"
local dest="$data_dir/User/$item"
if [ -e "$src" ] && [ ! -e "$dest" ]; then
ln -s "$src" "$dest"
fi
done
fi
# Symlink extensions to default (shared, not copied)
local ext_dir
ext_dir="$(extensions_dir "$profile_path")"
if [ -d "$default_ext" ]; then
# Remove the empty dir created by mkdir, replace with symlink
rmdir "$ext_dir" 2>/dev/null || true
ln -s "$default_ext" "$ext_dir"
else
mkdir -p "$ext_dir"
fi
case "$(platform)" in
darwin)
mkdir -p "$profile_path/Library/Application Support"
if [ ! -e "$profile_path/Library/Keychains" ] && [ -d "$HOME/Library/Keychains" ]; then
ln -s "$HOME/Library/Keychains" "$profile_path/Library/Keychains"
fi
;;
linux)
mkdir -p \
"$profile_path/.cache" \
"$profile_path/.local/share" \
"$profile_path/.local/state"
;;
esac
}
script_path() {
local installed dir
installed="$(command -v multigravity 2>/dev/null || true)"
if [ -n "$installed" ]; then
printf '%s\n' "$installed"
return 0
fi
dir="$(cd "$(dirname "$0")" && pwd)"
printf '%s/%s\n' "$dir" "$(basename "$0")"
}
shell_quote() {
printf '%q' "$1"
}
desktop_quote() {
local value=$1
value=${value//\\/\\\\}
value=${value//\"/\\\"}
value=${value//\$/\\$}
value=${value//\`/\\\`}
printf '"%s"' "$value"
}
create_shortcut_darwin() {
local profile=$1
local app_name="Multigravity $profile"
local app_dir
local run_script
local launcher
local icon_src
app_dir="$(mac_shortcut_path "$profile")"
run_script="$app_dir/Contents/MacOS/run"
launcher="$(script_path)"
icon_src="$(dirname "$launcher")/icon.icns"
mkdir -p "$app_dir/Contents/MacOS"
mkdir -p "$app_dir/Contents/Resources"
cat <<EOF > "$run_script"
#!/usr/bin/env bash
export MULTIGRAVITY_HOME=$(shell_quote "$BASE")
exec $(shell_quote "$launcher") $(shell_quote "$profile") "\$@"
EOF
chmod +x "$run_script"
if [ -f "$icon_src" ]; then
cp "$icon_src" "$app_dir/Contents/Resources/icon.icns"
fi
cat <<EOF > "$app_dir/Contents/Info.plist"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key>
<string>run</string>
<key>CFBundleIconFile</key>
<string>icon</string>
<key>CFBundleIdentifier</key>
<string>com.multigravity.profile.$profile</string>
<key>CFBundleName</key>
<string>$app_name</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
</dict>
</plist>
EOF
print_info "Shortcut created: $app_dir"
}
create_shortcut_linux() {
local profile=$1
local launcher_root
local launcher_path
local desktop_path
local launcher
launcher_root="$(linux_launcher_dir)"
launcher_path="$(linux_launcher_path "$profile")"
desktop_path="$(linux_desktop_path "$profile")"
launcher="$(script_path)"
mkdir -p "$launcher_root"
mkdir -p "$(dirname "$desktop_path")"
cat <<EOF > "$launcher_path"
#!/usr/bin/env sh
export MULTIGRAVITY_HOME=$(shell_quote "$BASE")
exec $(shell_quote "$launcher") $(shell_quote "$profile") "\$@"
EOF
chmod +x "$launcher_path"
cat <<EOF > "$desktop_path"
[Desktop Entry]
Version=1.0
Type=Application
Name=Multigravity $profile
Comment=Launch the $profile Antigravity profile
Exec=$(desktop_quote "$launcher_path") %F
Icon=antigravity
Terminal=false
StartupNotify=false
StartupWMClass=Antigravity
Categories=TextEditor;Development;IDE;
MimeType=application/x-antigravity-workspace;
EOF
print_info "Shortcut created: $desktop_path"
}
create_shortcut() {
case "$(platform)" in
darwin)
create_shortcut_darwin "$1"
;;
linux)
create_shortcut_linux "$1"
;;
esac
}
remove_shortcut() {
local profile=$1
local target
case "$(platform)" in
darwin)
target="$(mac_shortcut_path "$profile")"
if [ -d "$target" ]; then
rm -rf "$target"
print_info "Removed shortcut: $target"
fi
;;
linux)
target="$(linux_launcher_path "$profile")"
if [ -f "$target" ]; then
rm -f "$target"
print_info "Removed shortcut: $target"
fi
target="$(linux_desktop_path "$profile")"
if [ -f "$target" ]; then
rm -f "$target"
print_info "Removed shortcut: $target"
fi
;;
esac
}
launch_profile() {
local profile=$1
shift || true
local profile_path
local app
local data_dir
local ext_dir
profile_path="$(profile_dir "$profile")"
if [ ! -d "$profile_path" ]; then
abort "profile '$profile' does not exist. Run: multigravity new $profile"
fi
create_profile_layout "$profile_path"
app="$(require_app)"
data_dir="$(user_data_dir "$profile_path")"
ext_dir="$(extensions_dir "$profile_path")"
print_info "Launching Antigravity profile '$profile'"
case "$(platform)" in
darwin)
HOME="$profile_path" open -n "$app" --args \
--user-data-dir "$data_dir" \
--extensions-dir "$ext_dir" \
"$@"
;;
linux)
HOME="$profile_path" \
XDG_CONFIG_HOME="$profile_path/.config" \
XDG_CACHE_HOME="$profile_path/.cache" \
XDG_DATA_HOME="$profile_path/.local/share" \
XDG_STATE_HOME="$profile_path/.local/state" \
"$app" \
--user-data-dir "$data_dir" \
--extensions-dir "$ext_dir" \
"$@"
;;
esac
}
list_profiles() {
local entries=""
local dir
local raw=${1-}
if [ "$raw" != "--raw" ]; then
echo "Existing profiles:"
fi
if [ ! -d "$BASE" ]; then
if [ "$raw" != "--raw" ]; then
echo "(none)"
fi
return 0
fi
for dir in "$BASE"/*; do
[ -d "$dir" ] || continue
local name
name="$(basename "$dir")"
[ "$name" = ".templates" ] && continue
entries="${entries}${name}"$'\n'
done
if [ -z "$entries" ]; then
if [ "$raw" != "--raw" ]; then
echo "(none)"
fi
else
printf '%s' "$entries" | sort
fi
}
new_profile() {
local profile=""
local auth_only=false
local from_template=""
while [ $# -gt 0 ]; do
case "$1" in
--auth-only) auth_only=true ;;
--from) from_template="${2-}"; shift ;;
-*) abort "unknown option: $1" ;;
*) profile="$1" ;;
esac
shift
done
validate_name "$profile"
local profile_path
profile_path="$(profile_dir "$profile")"
if [ -d "$profile_path" ]; then
abort "profile '$profile' already exists"
fi
mkdir -p "$BASE"
if [ -n "$from_template" ]; then
local tpl_path
tpl_path="$(templates_dir)/$from_template"
if [ ! -d "$tpl_path" ]; then
abort "template '$from_template' does not exist. Run: multigravity template list"
fi
print_info "Creating profile '$profile' from template '$from_template'..."
cp -R "$tpl_path" "$profile_path"
elif [ "$auth_only" = true ]; then
create_auth_only_layout "$profile_path"
else
create_profile_layout "$profile_path"
fi
print_info "Created profile '$profile'"
create_shortcut "$profile"
}
delete_profile() {
local profile=$1
local profile_path
local confirm
validate_name "$profile"
profile_path="$(profile_dir "$profile")"
if [ ! -d "$profile_path" ]; then
abort "profile '$profile' does not exist"
fi
read -r -p "Delete profile '$profile' and all its data? [y/N] " confirm
if [[ "$confirm" =~ ^[Yy]$ ]]; then
rm -rf "$profile_path"
remove_shortcut "$profile"
print_info "Deleted profile '$profile'"
else
print_info "Aborted."
fi
}
rename_profile() {
local old=$1
local new=$2
local old_path
local new_path
validate_name "$old"
validate_name "$new"
old_path="$(profile_dir "$old")"
new_path="$(profile_dir "$new")"
if [ ! -d "$old_path" ]; then
abort "profile '$old' does not exist"
fi
if [ -d "$new_path" ]; then
abort "profile '$new' already exists"
fi
mv "$old_path" "$new_path"
remove_shortcut "$old"
create_shortcut "$new"
print_info "Renamed profile '$old' to '$new'"
}
clone_profile() {
local src=$1
local dest=$2
local src_path
local dest_path
validate_name "$src"
validate_name "$dest"
src_path="$(profile_dir "$src")"
dest_path="$(profile_dir "$dest")"
if [ ! -d "$src_path" ]; then
abort "source profile '$src' does not exist"
fi
if [ -d "$dest_path" ]; then
abort "destination profile '$dest' already exists"
fi
print_info "Cloning profile '$src' to '$dest'..."
cp -R "$src_path" "$dest_path"
create_shortcut "$dest"
print_info "Successfully cloned '$src' to '$dest'"
}
doctor_cli() {
local errors=0
local warnings=0
print_info "Checking multigravity environment..."
# 1. Platform Check
if platform &>/dev/null; then
echo " [✓] Platform: $(platform)"
else
echo " [✗] Platform: Unknown"
errors=$((errors + 1))
fi
# 2. Antigravity Installation
if app=$(find_app); then
echo " [✓] Antigravity: Found at $app"
else
echo " [✗] Antigravity: Not found. Ensure it is installed or set MULTIGRAVITY_APP."
errors=$((errors + 1))
fi
# 3. Path Check
local installed
installed="$(command -v multigravity 2>/dev/null || true)"
if [ -n "$installed" ]; then
echo " [✓] Global Binary: $installed"
else
echo " [!] Global Binary: Not found in PATH. Run install script or update PATH."
warnings=$((warnings + 1))
fi
# 4. Icon Check (macOS)
if [ "$(platform)" = "darwin" ]; then
local icon_path
icon_path="$(dirname "$(script_path)")/icon.icns"
if [ -f "$icon_path" ]; then
echo " [✓] Application Icon: Found"
else
echo " [!] Application Icon: Missing ($icon_path). Shortcuts will have default icons."
warnings=$((warnings + 1))
fi
fi
# 5. Base Directory
if [ -d "$BASE" ]; then
if [ -w "$BASE" ]; then
echo " [✓] Profile storage: $BASE (writable)"
else
echo " [✗] Profile storage: $BASE (NOT writable)"
errors=$((errors + 1))
fi
else
echo " [!] Profile storage: $BASE (Not yet created)"
fi
echo ""
if [ $errors -eq 0 ]; then
if [ $warnings -eq 0 ]; then
print_info "✓ Your environment looks perfect!"
else
print_info "Found $warnings warning(s). Multigravity should still work, but some features might be degraded."
fi
else
print_info "✗ Found $errors error(s) and $warnings warning(s). Please fix the errors above."
fi
}
profile_stats() {
if [ ! -d "$BASE" ]; then
print_info "No profiles found."
return 0
fi
print_info "Profile Storage Usage:"
printf "%-20s %-10s %-10s\n" "PROFILE" "SIZE" "EXTENSIONS"
printf "%-20s %-10s %-10s\n" "-------" "----" "----------"
local dir
for dir in "$BASE"/*; do
[ -d "$dir" ] || continue
local name
name=$(basename "$dir")
[ "$name" = ".templates" ] && continue
local size
if [ "$(uname -s)" = "Darwin" ]; then
size=$(du -sh "$dir" | cut -f1)
else
size=$(du -sh "$dir" | cut -f1)
fi
local ext_count
ext_count=$(ls -1 "$(extensions_dir "$dir")" 2>/dev/null | wc -l | xargs)
[ -z "$ext_count" ] && ext_count=0
printf "%-20s %-10s %-10s\n" "$name" "$size" "$ext_count"
done
echo ""
local total
total=$(du -sh "$BASE" | cut -f1)
print_info "Total usage: $total"
}
update_cli() {
local script_url="https://raw.githubusercontent.com/sujitagarwal/multigravity-cli/main/multigravity"
local target
target="$(script_path)"
if [ -z "$target" ]; then
abort "could not determine script path for update"
fi
print_info "Updating multigravity from $script_url ..."
if curl -fsSL "$script_url" -o "$target.tmp"; then
mv "$target.tmp" "$target"
chmod +x "$target"
print_info "Successfully updated multigravity!"
else
rm -f "$target.tmp"
abort "failed to download update"
fi
}
help_completion() {
local shell_name
shell_name="$(basename "$SHELL")"
echo "To enable autocompletion, add the following to your shell profile:"
echo ""
case "$shell_name" in
zsh)
echo " # Add to ~/.zshrc"
echo ' source <(multigravity completion zsh)'
;;
bash)
echo " # Add to ~/.bashrc"
echo ' source <(multigravity completion bash)'
;;
*)
echo " # Your shell ($shell_name) is not natively supported for completion."
;;
esac
echo ""
echo "Then restart your terminal or run: source ~/.$shell_name""rc"
}
generate_completion() {
local shell_type=$1
case "$shell_type" in
zsh)
cat <<'EOF'
#compdef multigravity
_multigravity() {
local -a commands
commands=(
'new:Create a new profile'
'list:List existing profiles'
'status:Show profile status (running, type, last used)'
'rename:Rename a profile'
'delete:Delete a profile'
'clone:Clone an existing profile'
'template:Manage profile templates (save, list, delete)'
'export:Export a profile to tar.gz'
'import:Import a profile from tar.gz'
'update:Update multigravity'
'doctor:Run system diagnosis'
'stats:Show profile storage usage'
'completion:Setup shell completion'
'help:Show help'
)
local curcontext="$curcontext" state line
_arguments -C '1: :->command' '*: :->args'
case $state in
command)
_describe -t commands 'multigravity command' commands
local -a profiles
profiles=($(multigravity list --raw 2>/dev/null))
(( ${#profiles} > 0 )) && _describe -t profiles 'profile' profiles
;;
args)
case $line[1] in
rename|delete|clone|help)
local -a profiles
profiles=($(multigravity list --raw 2>/dev/null))
(( ${#profiles} > 0 )) && _describe -t profiles 'profile' profiles
;;
esac
;;
esac
}
_multigravity "$@"
EOF
;;
bash)
cat <<'EOF'
_multigravity_completions() {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="new list status rename delete clone template export import update doctor stats completion help"
pocket
if [[ ${COMP_CWORD} -eq 1 ]] ; then
local profiles=$(multigravity list --raw 2>/dev/null)
COMPREPLY=( $(compgen -W "${opts} ${profiles}" -- ${cur}) )
return 0
fi
case "${prev}" in
delete|rename|clone)
local profiles=$(multigravity list --raw 2>/dev/null)
COMPREPLY=( $(compgen -W "${profiles}" -- ${cur}) )
return 0
;;
esac
}
complete -F _multigravity_completions multigravity
EOF
;;
esac
}
# ─── Template commands ────────────────────────────────────────────────────────
template_save() {
local profile=$1
local tpl_name=$2
validate_name "$profile"
validate_name "$tpl_name"
local profile_path
profile_path="$(profile_dir "$profile")"
if [ ! -d "$profile_path" ]; then
abort "profile '$profile' does not exist"
fi
local tpl_dir
tpl_dir="$(templates_dir)"
local tpl_path="$tpl_dir/$tpl_name"
if [ -d "$tpl_path" ]; then
abort "template '$tpl_name' already exists"
fi
mkdir -p "$tpl_dir"
print_info "Saving profile '$profile' as template '$tpl_name'..."
cp -R "$profile_path" "$tpl_path"
# Remove auth-only marker from template (let the user choose on creation)
rm -f "$tpl_path/.auth-only"
print_info "Template '$tpl_name' saved"
}
template_list() {
local tpl_dir
tpl_dir="$(templates_dir)"
echo "Available templates:"
if [ ! -d "$tpl_dir" ]; then
echo " (none)"
return 0
fi
local found=false
for dir in "$tpl_dir"/*; do
[ -d "$dir" ] || continue
found=true
local name
name=$(basename "$dir")
local size
size=$(du -sh "$dir" 2>/dev/null | cut -f1)
echo " $name ($size)"
done
if [ "$found" = false ]; then
echo " (none)"
fi
}
template_delete() {
local tpl_name=$1
validate_name "$tpl_name"
local tpl_path
tpl_path="$(templates_dir)/$tpl_name"
if [ ! -d "$tpl_path" ]; then
abort "template '$tpl_name' does not exist"
fi
rm -rf "$tpl_path"
print_info "Deleted template '$tpl_name'"
}
# ─── Status command ───────────────────────────────────────────────────────────
status_profiles() {
if [ ! -d "$BASE" ]; then
print_info "No profiles found."
return 0
fi
printf "%-16s %-10s %-12s %-20s %s\n" "PROFILE" "STATUS" "TYPE" "LAST USED" "SIZE"
printf "%-16s %-10s %-12s %-20s %s\n" "-------" "------" "----" "---------" "----"
for dir in "$BASE"/*; do
[ -d "$dir" ] || continue
local name
name=$(basename "$dir")
# Skip .templates directory
[ "$name" = ".templates" ] && continue
# Check if running
local status="stopped"
local data_dir
data_dir="$(user_data_dir "$dir")"
if ps -eo args 2>/dev/null | grep -v grep | grep -qF "$data_dir" 2>/dev/null; then
status="running"
fi
# Type
local ptype="full"
if [ -f "$dir/.auth-only" ]; then
ptype="auth-only"
fi
# Last used (directory modification time)
local last_used="unknown"
case "$(platform)" in
darwin)
last_used=$(stat -f "%Sm" -t "%Y-%m-%d %H:%M" "$dir" 2>/dev/null || echo "unknown")
;;
linux)
last_used=$(stat -c "%y" "$dir" 2>/dev/null | cut -d'.' -f1 || echo "unknown")
;;
esac
# Size
local size
size=$(du -sh "$dir" 2>/dev/null | cut -f1)
# Color for running status
if [ "$status" = "running" ]; then
printf "%-16s \033[32m%-10s\033[0m %-12s %-20s %s\n" "$name" "$status" "$ptype" "$last_used" "$size"
else
printf "%-16s %-10s %-12s %-20s %s\n" "$name" "$status" "$ptype" "$last_used" "$size"
fi
done
}
# ─── Export / Import ──────────────────────────────────────────────────────────
export_profile() {
local profile=$1
local output_path=${2-}
validate_name "$profile"
local profile_path
profile_path="$(profile_dir "$profile")"
if [ ! -d "$profile_path" ]; then
abort "profile '$profile' does not exist"
fi
if [ -z "$output_path" ]; then
output_path="./${profile}.tar.gz"
fi
print_info "Exporting profile '$profile'..."
# Resolve symlinks before archiving (follow links)
tar -czf "$output_path" -C "$BASE" "$profile"