-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclient-manager.sh
More file actions
executable file
·1906 lines (1624 loc) · 61.2 KB
/
client-manager.sh
File metadata and controls
executable file
·1906 lines (1624 loc) · 61.2 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
#!/bin/bash
# ============================================================================
# Knowledge Graph Client Manager
# ============================================================================
CLIENT_MANAGER_VERSION="0.11.0"
# ============================================================================
#
# Manages Knowledge Graph client tools across platforms:
# - kg CLI (npm package @aaronsb/kg-cli)
# - kg-mcp-server (included with CLI, for AI assistants)
# - kg-fuse (PyPI package, FUSE filesystem driver)
#
# ARCHITECTURE OVERVIEW:
# ----------------------
# This script separates platform-specific logic into clean adapter modules:
#
# ┌─────────────────────────────────────────────────────────────────┐
# │ CONFIGURATION │
# │ (Variables, URLs, package names - single source of truth) │
# ├─────────────────────────────────────────────────────────────────┤
# │ UTILITIES │
# │ (Logging, prompts, config save/load) │
# ├─────────────────────────────────────────────────────────────────┤
# │ PACKAGE MANAGER ADAPTERS │
# │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
# │ │ pacman │ │ apt │ │ dnf │ │ brew │ │
# │ ├──────────┤ ├──────────┤ ├──────────┤ ├──────────┤ │
# │ │ _node() │ │ _node() │ │ _node() │ │ _node() │ │
# │ │ _pipx() │ │ _pipx() │ │ _pipx() │ │ _pipx() │ │
# │ │ _fuse() │ │ _fuse() │ │ _fuse() │ │ _fuse() │ │
# │ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │
# ├─────────────────────────────────────────────────────────────────┤
# │ PLATFORM FAMILY ADAPTERS │
# │ ┌─────────────────────────┐ ┌─────────────────────────┐ │
# │ │ linux │ │ macos │ │
# │ ├─────────────────────────┤ ├─────────────────────────┤ │
# │ │ _stop_fuse() │ │ _stop_fuse() │ │
# │ │ _start_fuse() │ │ _start_fuse() │ │
# │ │ _configure_autostart() │ │ _configure_autostart() │ │
# │ │ _remove_autostart() │ │ _remove_autostart() │ │
# │ │ _get_config_dir() │ │ _get_config_dir() │ │
# │ └─────────────────────────┘ └─────────────────────────┘ │
# ├─────────────────────────────────────────────────────────────────┤
# │ DISPATCH LAYER │
# │ install_node() → ${PKG_MGR}_install_node │
# │ install_pipx() → ${PKG_MGR}_install_pipx │
# │ stop_fuse() → ${PLATFORM}_stop_fuse │
# │ start_fuse() → ${PLATFORM}_start_fuse │
# │ configure_autostart → ${PLATFORM}_configure_autostart │
# ├─────────────────────────────────────────────────────────────────┤
# │ DETECTION & VERIFICATION │
# │ (OS detection, existing install detection, API verification) │
# ├─────────────────────────────────────────────────────────────────┤
# │ INSTALLATION LOGIC │
# │ (Prerequisites, clients, OAuth - uses dispatch layer) │
# ├─────────────────────────────────────────────────────────────────┤
# │ MAIN FLOW │
# │ (Argument parsing, lifecycle menu, mode handlers) │
# └─────────────────────────────────────────────────────────────────┘
#
# KEY INSIGHT - TWO ORTHOGONAL DIMENSIONS:
# ----------------------------------------
# Platform differences break into two independent concerns:
#
# | Dimension | Values | Governs |
# |---------------------|----------------------|----------------------------------|
# | Package Manager | pacman, apt, dnf, | Installing packages (node, pipx, |
# | | brew | fuse3) |
# | Platform Family | linux, macos | Service management, autostart, |
# | | | paths |
#
# Desktop environment (KDE/GNOME/XFCE) doesn't matter because:
# - XDG autostart spec works on all Linux desktops
# - FUSE operations (fusermount) are the same across Linux
#
# USAGE:
# ------
# Interactive: curl -fsSL https://raw.githubusercontent.com/aaronsb/knowledge-graph-system/main/client-manager.sh | bash
# Headless: curl -fsSL ... | bash -s -- --api-url X ...
# Upgrade: ./client-manager.sh --upgrade
# Uninstall: ./client-manager.sh --uninstall
# Status: ./client-manager.sh --status
# Help: ./client-manager.sh --help
#
# ============================================================================
set -e # Exit on error
# ============================================================================
# SECTION 1: CONFIGURATION VARIABLES
# ============================================================================
#
# All configurable options are defined here. This is the single source of truth.
# Both flag parsing and interactive prompts set these same variables.
#
# Naming convention:
# - UPPERCASE for config that can be set by user
# - lowercase for internal/derived values
#
# ============================================================================
# --- Platform Connection ---
# These configure how to connect to the KG platform
API_URL="" # Platform API URL (e.g., https://kg.example.com/api)
API_VERSION="" # Platform version (fetched from API, saved to config)
USERNAME="" # Admin username for authentication
PASSWORD="" # Admin password (prompted, never saved to config)
# --- Component Selection ---
# Choose which client tools to install
INSTALL_CLI=true # kg CLI (required, always true)
INSTALL_MCP=true # MCP server OAuth client
INSTALL_FUSE=false # FUSE driver (optional, needs fuse3)
# --- FUSE Options ---
# Configure FUSE filesystem behavior
FUSE_MOUNT_DIR="" # Mount directory (default: ~/Knowledge)
FUSE_AUTOSTART=false # Configure autostart on login
# --- Mode ---
# Current operation mode
MODE="install" # install, upgrade, uninstall, status
INTERACTIVE=false # Running in interactive mode (vs headless)
SAVE_CONFIG=false # Save config after installation
# --- Package Names ---
# These are the package identifiers used for installation.
# Change these if you fork the project or use a private registry.
NPM_PACKAGE="@aaronsb/kg-cli" # npm package name for kg CLI
PYPI_PACKAGE="kg-fuse" # PyPI package name for FUSE driver
# --- External URLs ---
# URLs for downloading dependencies and documentation.
# Change these if you need to use mirrors or internal repositories.
KG_REPO_RAW="https://raw.githubusercontent.com/aaronsb/knowledge-graph-system/main"
NODESOURCE_SETUP_URL="https://deb.nodesource.com/setup_lts.x"
NODEJS_DOWNLOAD_URL="https://nodejs.org/"
MACFUSE_DOWNLOAD_URL="https://osxfuse.github.io"
# --- Detected Values ---
# These are populated by detection functions. Do not set manually.
DETECTED_OS="" # linux-arch, linux-ubuntu, linux-fedora, macos
DETECTED_PKG_MGR="" # pacman, apt, dnf, brew
DETECTED_NODE_VERSION="" # Node.js version or empty
DETECTED_PYTHON_VERSION="" # Python version or empty
DETECTED_PIPX="" # true/false
DETECTED_DESKTOP="" # kde, gnome, macos, unknown (informational only)
DETECTED_KG_VERSION="" # Existing kg CLI version or empty
DETECTED_FUSE_VERSION="" # Existing kg-fuse version or empty
# --- Derived Values ---
# These are computed from detected values during init
PLATFORM_FAMILY="" # linux, macos (for service management dispatch)
# --- Config File ---
# Location determined at runtime based on platform
CONFIG_DIR="" # Set in init based on platform
CONFIG_FILE="" # Set in init based on platform
CONFIG_VERSION="" # Version from loaded config
# ============================================================================
# SECTION 2: UTILITY FUNCTIONS
# ============================================================================
#
# Helper functions used throughout the script:
# - Logging (colored output)
# - User prompts (for interactive mode)
# - Config save/load
#
# ============================================================================
# --- Terminal Colors ---
# Disable colors if not running in a terminal (e.g., piped to file)
if [ -t 1 ]; then
RED=$'\033[0;31m'
GREEN=$'\033[0;32m'
YELLOW=$'\033[1;33m'
BLUE=$'\033[0;34m'
CYAN=$'\033[0;36m'
GRAY=$'\033[0;90m'
BOLD=$'\033[1m'
NC=$'\033[0m' # No Color
else
RED='' GREEN='' YELLOW='' BLUE='' CYAN='' GRAY='' BOLD='' NC=''
fi
# --- Logging Functions ---
# These write to stderr so they display even when stdout is captured
log_info() {
# Informational message (blue i icon)
echo -e "${BLUE}ℹ${NC} $1" >&2
}
log_success() {
# Success message (green checkmark)
echo -e "${GREEN}✓${NC} $1" >&2
}
log_warning() {
# Warning message (yellow warning icon)
echo -e "${YELLOW}⚠${NC} $1" >&2
}
log_error() {
# Error message (red x icon)
echo -e "${RED}✗${NC} $1" >&2
}
log_step() {
# Major step header - used to mark beginning of a phase
echo -e "\n${BOLD}${BLUE}==>${NC} ${BOLD}$1${NC}" >&2
}
# --- Prompt Functions ---
# These handle user input in interactive mode. All read from /dev/tty
# to work correctly when script is piped (curl | bash).
prompt_value() {
# Prompt for a text value with optional default
# Usage: result=$(prompt_value "Prompt text" "default")
local prompt="$1"
local default="$2"
local value
if [[ -n "$default" ]]; then
echo -ne "${CYAN}?${NC} ${prompt} [${default}]: " >&2
read -r value </dev/tty
echo "${value:-$default}"
else
echo -ne "${CYAN}?${NC} ${prompt}: " >&2
read -r value </dev/tty
echo "$value"
fi
}
prompt_password() {
# Prompt for a password (hidden input)
# Usage: result=$(prompt_password "Password")
local prompt="$1"
local value
echo -ne "${CYAN}?${NC} ${prompt}: " >&2
read -sr value </dev/tty
echo >&2 # Newline after hidden input
echo "$value"
}
prompt_bool() {
# Prompt for yes/no answer
# Usage: if prompt_bool "Enable feature?" "n"; then ...
# Second arg is default: "y" or "n" (defaults to "n")
local prompt="$1"
local default="${2:-n}"
local response
if [[ "$default" == "y" ]]; then
echo -ne "${CYAN}?${NC} ${prompt} [Y/n]: " >&2
else
echo -ne "${CYAN}?${NC} ${prompt} [y/N]: " >&2
fi
read -r response </dev/tty
response="${response:-$default}"
[[ "$response" =~ ^[Yy] ]]
}
prompt_choice() {
# Prompt for selection from numbered options
# Usage: result=$(prompt_choice "Select option" "Option A" "Option B" "Option C")
# Returns 0-indexed selection
local prompt="$1"
shift
local options=("$@")
local i=1
local response
echo -e "${CYAN}?${NC} ${prompt}:" >&2
for opt in "${options[@]}"; do
echo " ${i}) ${opt}" >&2
((i++))
done
while true; do
echo -ne " Enter choice [1-${#options[@]}]: " >&2
read -r response </dev/tty
if [[ "$response" =~ ^[0-9]+$ ]] && [ "$response" -ge 1 ] && [ "$response" -le "${#options[@]}" ]; then
echo $((response - 1))
return
fi
echo " Invalid choice. Please enter a number 1-${#options[@]}." >&2
done
}
# --- Config Functions ---
# Save and load configuration to/from file
save_config() {
# Save current configuration to config file
# Creates config directory if needed
mkdir -p "$CONFIG_DIR"
cat > "$CONFIG_FILE" << EOF
# Knowledge Graph Client Manager Configuration
# Generated: $(date -Iseconds)
# Manager Version: $CLIENT_MANAGER_VERSION
API_URL="$API_URL"
API_VERSION="$API_VERSION"
USERNAME="$USERNAME"
INSTALL_CLI=$INSTALL_CLI
INSTALL_MCP=$INSTALL_MCP
INSTALL_FUSE=$INSTALL_FUSE
FUSE_MOUNT_DIR="$FUSE_MOUNT_DIR"
FUSE_AUTOSTART=$FUSE_AUTOSTART
EOF
log_success "Configuration saved to $CONFIG_FILE"
}
load_config() {
# Load configuration from config file if it exists
# Returns 1 if no config file found
if [[ -f "$CONFIG_FILE" ]]; then
# shellcheck source=/dev/null
source "$CONFIG_FILE"
CONFIG_VERSION=$(grep -E "^# Manager Version:" "$CONFIG_FILE" 2>/dev/null | cut -d: -f2 | tr -d ' ' || true)
return 0
fi
return 1
}
# ============================================================================
# SECTION 3: PACKAGE MANAGER ADAPTERS - PACMAN (Arch Linux)
# ============================================================================
#
# Functions for installing packages on Arch Linux using pacman.
# All functions follow the pattern: pacman_install_{package}
#
# ============================================================================
pacman_install_node() {
# Install Node.js and npm on Arch Linux
log_info "Installing Node.js via pacman..."
sudo pacman -S --noconfirm nodejs npm
}
pacman_install_pipx() {
# Install pipx on Arch Linux
log_info "Installing pipx via pacman..."
sudo pacman -S --noconfirm python-pipx
# Ensure pipx bin directory is in PATH
pipx ensurepath 2>/dev/null || true
}
pacman_install_fuse() {
# Install FUSE3 libraries on Arch Linux
log_info "Installing fuse3 via pacman..."
sudo pacman -S --noconfirm fuse3
}
# ============================================================================
# SECTION 4: PACKAGE MANAGER ADAPTERS - APT (Ubuntu/Debian)
# ============================================================================
#
# Functions for installing packages on Ubuntu/Debian using apt.
# All functions follow the pattern: apt_install_{package}
#
# ============================================================================
apt_install_node() {
# Install Node.js via NodeSource repository on Ubuntu/Debian
# Uses LTS version for stability
log_info "Installing Node.js via NodeSource..."
curl -fsSL "$NODESOURCE_SETUP_URL" | sudo -E bash -
sudo apt-get install -y nodejs
}
apt_install_pipx() {
# Install pipx on Ubuntu/Debian
log_info "Installing pipx via apt..."
sudo apt-get install -y pipx
# Ensure pipx bin directory is in PATH
pipx ensurepath 2>/dev/null || true
}
apt_install_fuse() {
# Install FUSE3 libraries on Ubuntu/Debian
log_info "Installing fuse3 via apt..."
sudo apt-get install -y fuse3
}
# ============================================================================
# SECTION 5: PACKAGE MANAGER ADAPTERS - DNF (Fedora/RHEL)
# ============================================================================
#
# Functions for installing packages on Fedora/RHEL using dnf.
# All functions follow the pattern: dnf_install_{package}
#
# ============================================================================
dnf_install_node() {
# Install Node.js on Fedora/RHEL
log_info "Installing Node.js via dnf..."
sudo dnf install -y nodejs npm
}
dnf_install_pipx() {
# Install pipx on Fedora/RHEL
log_info "Installing pipx via dnf..."
sudo dnf install -y pipx
# Ensure pipx bin directory is in PATH
pipx ensurepath 2>/dev/null || true
}
dnf_install_fuse() {
# Install FUSE3 libraries on Fedora/RHEL
log_info "Installing fuse3 via dnf..."
sudo dnf install -y fuse3
}
# ============================================================================
# SECTION 6: PACKAGE MANAGER ADAPTERS - BREW (macOS)
# ============================================================================
#
# Functions for installing packages on macOS using Homebrew.
# All functions follow the pattern: brew_install_{package}
#
# Note: macFUSE requires special handling (cask install + reboot typically)
#
# ============================================================================
brew_install_node() {
# Install Node.js on macOS via Homebrew
log_info "Installing Node.js via Homebrew..."
brew install node
}
brew_install_pipx() {
# Install pipx on macOS via Homebrew
log_info "Installing pipx via Homebrew..."
brew install pipx
# Ensure pipx bin directory is in PATH
pipx ensurepath 2>/dev/null || true
}
brew_install_fuse() {
# Install macFUSE on macOS
# Note: macFUSE is a cask and may require system extension approval
log_info "Installing macFUSE via Homebrew..."
log_warning "macFUSE requires a system extension. You may need to approve it in System Preferences > Security & Privacy"
brew install --cask macfuse
}
# ============================================================================
# SECTION 7: PLATFORM FAMILY ADAPTERS - LINUX
# ============================================================================
#
# Functions for Linux-specific operations:
# - FUSE mount/unmount (uses fusermount)
# - Autostart (uses XDG desktop entry spec, works on KDE/GNOME/XFCE/etc)
# - Config directories (follows XDG Base Directory spec)
#
# ============================================================================
linux_get_config_dir() {
# Return XDG-compliant config directory for Linux
# Respects XDG_CONFIG_HOME if set, defaults to ~/.config
echo "${XDG_CONFIG_HOME:-$HOME/.config}/kg"
}
linux_stop_fuse() {
# Unmount FUSE filesystem on Linux
# Delegates to kg-fuse CLI which handles systemd/daemon mode correctly
# Args: $1 = mount directory
local mount_dir="$1"
if [[ -z "$mount_dir" ]]; then
log_error "No mount directory specified for linux_stop_fuse"
return 1
fi
if ! command -v kg-fuse &>/dev/null; then
log_warning "kg-fuse not found, trying raw fusermount"
if command -v fusermount3 &>/dev/null; then
fusermount3 -u "$mount_dir" 2>/dev/null || true
elif command -v fusermount &>/dev/null; then
fusermount -u "$mount_dir" 2>/dev/null || true
else
umount "$mount_dir" 2>/dev/null || true
fi
return
fi
log_info "Unmounting FUSE at $mount_dir..."
kg-fuse unmount "$mount_dir" 2>/dev/null && log_success "FUSE unmounted" || log_info "FUSE not mounted at $mount_dir"
}
detect_fuse_mounts() {
# Find active kg-fuse mount points from /proc/mounts
# kg-fuse (via pyfuse3) mounts show as fstype "fuse.pyfuse3"
if [[ -f /proc/mounts ]]; then
awk '$3 == "fuse.pyfuse3" { print $2 }' /proc/mounts
fi
}
fuse_libs_available() {
# Check if FUSE3 userspace tools are installed
command -v fusermount3 &>/dev/null || command -v fusermount &>/dev/null
}
stop_fuse_all() {
# Stop all FUSE mounts
# Delegates to kg-fuse CLI which handles systemd/daemon mode correctly
# Returns the list of previously-mounted directories (space-separated) on stdout
local mounts
mounts=$(detect_fuse_mounts)
if command -v kg-fuse &>/dev/null; then
# Let kg-fuse handle unmounting (respects daemon_mode config)
kg-fuse unmount 2>/dev/null || true
else
# Fallback: raw fusermount if kg-fuse not installed
if [[ -n "$mounts" ]]; then
while IFS= read -r mount_dir; do
if command -v fusermount3 &>/dev/null; then
fusermount3 -u "$mount_dir" 2>/dev/null || true
elif command -v fusermount &>/dev/null; then
fusermount -u "$mount_dir" 2>/dev/null || true
fi
done <<< "$mounts"
fi
fi
# Verify
local leftover
leftover=$(detect_fuse_mounts)
if [[ -n "$leftover" ]]; then
log_warning "Some FUSE mounts remain after cleanup:"
echo "$leftover" | while IFS= read -r m; do log_warning " $m"; done
else
[[ -n "$mounts" ]] && log_success "All FUSE mounts stopped"
fi
# Return the original mount list for restart
echo "$mounts"
}
linux_start_fuse() {
# Start FUSE filesystem on Linux
# Delegates to kg-fuse CLI which handles systemd/daemon mode correctly
# Args: $1 = mount directory
local mount_dir="$1"
if [[ -z "$mount_dir" ]]; then
log_error "No mount directory specified for linux_start_fuse"
return 1
fi
if ! command -v kg-fuse &>/dev/null; then
log_warning "kg-fuse not found, cannot start FUSE"
return 1
fi
# Ensure mount directory exists
mkdir -p "$mount_dir"
log_info "Starting FUSE at $mount_dir..."
kg-fuse mount "$mount_dir"
# Verify it mounted (give systemd a moment)
sleep 1
if mountpoint -q "$mount_dir" 2>/dev/null; then
log_success "FUSE mounted at $mount_dir"
else
log_warning "FUSE may not have mounted correctly — check 'kg-fuse status'"
fi
}
linux_configure_autostart() {
# Configure FUSE autostart on Linux
# Delegates to kg-fuse init which handles systemd vs daemon mode,
# creates the systemd unit or shell RC block as appropriate
# Args: $1 = mount directory
local mount_dir="$1"
if ! command -v kg-fuse &>/dev/null; then
log_warning "kg-fuse not found, cannot configure autostart"
return 1
fi
# kg-fuse init handles autostart setup (systemd unit, RC block, etc.)
log_info "Configuring FUSE autostart via kg-fuse init..."
kg-fuse init "$mount_dir"
log_success "Autostart configured via kg-fuse"
}
linux_remove_autostart() {
# Remove FUSE autostart configuration
# Cleans up legacy XDG .desktop files AND systemd units
local autostart_dir="${XDG_CONFIG_HOME:-$HOME/.config}/autostart"
local desktop_file="$autostart_dir/kg-fuse.desktop"
# Remove legacy XDG .desktop autostart if present
if [[ -f "$desktop_file" ]]; then
rm -f "$desktop_file"
log_info "Removed legacy XDG autostart: $desktop_file"
fi
# Disable systemd unit if present
if command -v systemctl &>/dev/null; then
systemctl --user disable kg-fuse.service 2>/dev/null || true
fi
log_success "Autostart removed"
}
# ============================================================================
# SECTION 8: PLATFORM FAMILY ADAPTERS - MACOS
# ============================================================================
#
# Functions for macOS-specific operations:
# - FUSE mount/unmount (uses diskutil/umount)
# - Autostart (uses launchd with plist files)
# - Config directories (uses ~/Library/Application Support)
#
# ============================================================================
macos_get_config_dir() {
# Return macOS-standard config directory
echo "$HOME/Library/Application Support/kg"
}
macos_stop_fuse() {
# Unmount FUSE filesystem on macOS
# Delegates to kg-fuse CLI which handles daemon mode correctly
# Args: $1 = mount directory
local mount_dir="$1"
if [[ -z "$mount_dir" ]]; then
log_error "No mount directory specified for macos_stop_fuse"
return 1
fi
if ! command -v kg-fuse &>/dev/null; then
log_warning "kg-fuse not found, trying raw unmount"
if diskutil unmount "$mount_dir" 2>/dev/null || umount "$mount_dir" 2>/dev/null; then
log_success "FUSE unmounted"
fi
return
fi
log_info "Unmounting FUSE at $mount_dir..."
kg-fuse unmount "$mount_dir" 2>/dev/null && log_success "FUSE unmounted" || log_info "FUSE not mounted at $mount_dir"
}
macos_start_fuse() {
# Start FUSE filesystem on macOS
# Delegates to kg-fuse CLI which handles daemon mode correctly
# Args: $1 = mount directory
local mount_dir="$1"
if [[ -z "$mount_dir" ]]; then
log_error "No mount directory specified for macos_start_fuse"
return 1
fi
if ! command -v kg-fuse &>/dev/null; then
log_warning "kg-fuse not found, cannot start FUSE"
return 1
fi
# Ensure mount directory exists
mkdir -p "$mount_dir"
log_info "Starting FUSE at $mount_dir..."
kg-fuse mount "$mount_dir"
# Verify it mounted
sleep 1
if mount | grep -q " on $mount_dir "; then
log_success "FUSE mounted at $mount_dir"
else
log_warning "FUSE may not have mounted correctly — check 'kg-fuse status'"
fi
}
macos_configure_autostart() {
# Configure FUSE autostart on macOS
# Delegates to kg-fuse init which handles launchd plist creation
# Args: $1 = mount directory
local mount_dir="$1"
if ! command -v kg-fuse &>/dev/null; then
log_warning "kg-fuse not found, cannot configure autostart"
return 1
fi
log_info "Configuring FUSE autostart via kg-fuse init..."
kg-fuse init "$mount_dir"
log_success "Autostart configured via kg-fuse"
}
macos_remove_autostart() {
# Remove FUSE autostart configuration on macOS
# Cleans up legacy launchd plist
local plist_file="$HOME/Library/LaunchAgents/com.kg.fuse.plist"
if [[ -f "$plist_file" ]]; then
launchctl unload "$plist_file" 2>/dev/null || true
rm -f "$plist_file"
log_info "Removed legacy launchd autostart: $plist_file"
fi
log_success "Autostart removed"
}
# ============================================================================
# SECTION 9: DISPATCH LAYER
# ============================================================================
#
# These functions dispatch to the appropriate adapter based on detected
# package manager and platform family. They provide a uniform interface
# that the rest of the script uses.
#
# Pattern:
# dispatch_function() calls ${DETECTED_PKG_MGR}_action or ${PLATFORM_FAMILY}_action
#
# ============================================================================
# --- Package Manager Dispatch ---
# These dispatch to the correct package manager adapter
install_node() {
# Install Node.js using the detected package manager
local fn="${DETECTED_PKG_MGR}_install_node"
if declare -f "$fn" > /dev/null; then
"$fn"
else
log_error "No Node.js install adapter for package manager: $DETECTED_PKG_MGR"
return 1
fi
}
install_pipx() {
# Install pipx using the detected package manager
local fn="${DETECTED_PKG_MGR}_install_pipx"
if declare -f "$fn" > /dev/null; then
"$fn"
else
log_error "No pipx install adapter for package manager: $DETECTED_PKG_MGR"
return 1
fi
}
install_fuse_libs() {
# Install FUSE libraries using the detected package manager
local fn="${DETECTED_PKG_MGR}_install_fuse"
if declare -f "$fn" > /dev/null; then
"$fn"
else
log_error "No FUSE install adapter for package manager: $DETECTED_PKG_MGR"
return 1
fi
}
# --- Platform Family Dispatch ---
# These dispatch to the correct platform family adapter
get_config_dir() {
# Get config directory for current platform
local fn="${PLATFORM_FAMILY}_get_config_dir"
if declare -f "$fn" > /dev/null; then
"$fn"
else
# Fallback to sensible default
echo "${XDG_CONFIG_HOME:-$HOME/.config}/kg"
fi
}
stop_fuse() {
# Stop FUSE on current platform
# Args: $1 = mount directory
local fn="${PLATFORM_FAMILY}_stop_fuse"
if declare -f "$fn" > /dev/null; then
"$fn" "$@"
else
log_error "No stop_fuse adapter for platform: $PLATFORM_FAMILY"
return 1
fi
}
start_fuse() {
# Start FUSE on current platform
# Args: $1 = mount directory
local fn="${PLATFORM_FAMILY}_start_fuse"
if declare -f "$fn" > /dev/null; then
"$fn" "$@"
else
log_error "No start_fuse adapter for platform: $PLATFORM_FAMILY"
return 1
fi
}
configure_autostart() {
# Configure autostart on current platform
# Args: $1 = mount directory
local fn="${PLATFORM_FAMILY}_configure_autostart"
if declare -f "$fn" > /dev/null; then
"$fn" "$@"
else
log_error "No configure_autostart adapter for platform: $PLATFORM_FAMILY"
return 1
fi
}
remove_autostart() {
# Remove autostart on current platform
local fn="${PLATFORM_FAMILY}_remove_autostart"
if declare -f "$fn" > /dev/null; then
"$fn"
else
log_error "No remove_autostart adapter for platform: $PLATFORM_FAMILY"
return 1
fi
}
fuse_supported() {
# Check if FUSE driver is supported on this platform
# Returns 0 if supported, 1 otherwise (with informational message)
case "$PLATFORM_FAMILY" in
linux)
return 0
;;
macos)
log_warning "FUSE driver not yet supported on macOS (requires macFUSE)"
log_info " macOS support is planned for a future release"
return 1
;;
*)
log_warning "FUSE driver not supported on this platform"
log_info " Linux: supported (FUSE3/libfuse3)"
log_info " macOS: planned (macFUSE)"
log_info " Windows: planned (WinFSP)"
return 1
;;
esac
}
# ============================================================================
# SECTION 10: DETECTION & VERIFICATION
# ============================================================================
#
# Functions to detect the current environment:
# - Operating system and distribution
# - Package manager available
# - Existing installations (Node, Python, kg CLI, kg-fuse)
# - Desktop environment (informational)
#
# And to verify connections:
# - API health check
# - Credential verification
#
# ============================================================================
detect_os() {
# Detect operating system and set DETECTED_OS, DETECTED_PKG_MGR, PLATFORM_FAMILY
log_info "Detecting operating system..."
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
DETECTED_OS="macos"
DETECTED_PKG_MGR="brew"
PLATFORM_FAMILY="macos"
DETECTED_DESKTOP="macos"
log_success "Detected macOS"
return 0
fi
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
PLATFORM_FAMILY="linux"
# Detect Linux distribution
if [[ -f /etc/os-release ]]; then
# shellcheck source=/dev/null
source /etc/os-release
case "$ID" in
arch|manjaro|endeavouros)
DETECTED_OS="linux-arch"
DETECTED_PKG_MGR="pacman"
log_success "Detected Arch Linux (or derivative)"
;;
ubuntu|debian|linuxmint|pop)
DETECTED_OS="linux-ubuntu"
DETECTED_PKG_MGR="apt"
log_success "Detected Ubuntu/Debian (or derivative)"
;;
fedora|rhel|centos|rocky|alma)
DETECTED_OS="linux-fedora"
DETECTED_PKG_MGR="dnf"
log_success "Detected Fedora/RHEL (or derivative)"
;;
*)
log_warning "Unknown Linux distribution: $ID"
# Try to detect package manager anyway
if command -v pacman &>/dev/null; then
DETECTED_OS="linux-arch"
DETECTED_PKG_MGR="pacman"
elif command -v apt &>/dev/null; then
DETECTED_OS="linux-ubuntu"
DETECTED_PKG_MGR="apt"
elif command -v dnf &>/dev/null; then
DETECTED_OS="linux-fedora"
DETECTED_PKG_MGR="dnf"
else
log_error "Could not detect package manager"
return 1
fi
;;
esac
else
log_error "Could not detect Linux distribution (no /etc/os-release)"
return 1
fi
# Detect desktop environment (informational only)
detect_desktop
return 0
fi
log_error "Unsupported operating system: $OSTYPE"
return 1
}
detect_desktop() {
# Detect desktop environment on Linux (informational only)
# This doesn't affect behavior - XDG autostart works on all desktops
if [[ "$XDG_CURRENT_DESKTOP" == *"KDE"* ]] || [[ "$DESKTOP_SESSION" == *"plasma"* ]]; then
DETECTED_DESKTOP="kde"
elif [[ "$XDG_CURRENT_DESKTOP" == *"GNOME"* ]] || [[ "$DESKTOP_SESSION" == *"gnome"* ]]; then
DETECTED_DESKTOP="gnome"
elif [[ "$XDG_CURRENT_DESKTOP" == *"XFCE"* ]] || [[ "$DESKTOP_SESSION" == *"xfce"* ]]; then
DETECTED_DESKTOP="xfce"
else
DETECTED_DESKTOP="unknown"
fi
log_info "Desktop environment: $DETECTED_DESKTOP"
}
detect_prerequisites() {
# Detect existing prerequisites (Node.js, Python, pipx)
log_info "Checking prerequisites..."
# Node.js
if command -v node &>/dev/null; then
DETECTED_NODE_VERSION=$(node --version 2>/dev/null | sed 's/^v//')
log_success "Node.js: $DETECTED_NODE_VERSION"
else
DETECTED_NODE_VERSION=""
log_info "Node.js: not installed"
fi
# Python
if command -v python3 &>/dev/null; then
DETECTED_PYTHON_VERSION=$(python3 --version 2>/dev/null | sed 's/Python //')
log_success "Python: $DETECTED_PYTHON_VERSION"
else
DETECTED_PYTHON_VERSION=""
log_info "Python: not installed"
fi
# pipx
if command -v pipx &>/dev/null; then
DETECTED_PIPX=true
log_success "pipx: installed"
else
DETECTED_PIPX=false
log_info "pipx: not installed"
fi
}
detect_existing_installs() {
# Detect existing kg CLI and kg-fuse installations
log_info "Checking existing installations..."
# kg CLI
if command -v kg &>/dev/null; then