-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosa-cli.zsh
More file actions
executable file
·2087 lines (1793 loc) · 71.1 KB
/
osa-cli.zsh
File metadata and controls
executable file
·2087 lines (1793 loc) · 71.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 zsh
# OSA (Open Source Automation) CLI
# Interactive setup tool with platform detection and component opt-in
# Get script directory (absolute path to directory)
OSA_CLI_DIR="$(cd "$(dirname "$0")" && pwd)"
# Export repo path for use in setup scripts
export OSA_REPO_PATH="$OSA_CLI_DIR"
# Source required modules
source "$OSA_CLI_DIR/src/zsh/constants/paths.zsh" || { echo "Failed to source paths.zsh"; exit 1; }
source "$OSA_CLI_DIR/src/zsh/constants/versions.zsh" || { echo "Failed to source versions.zsh"; exit 1; }
source "$OSA_CLI_DIR/src/zsh/helpers/detect-platform.zsh" || { echo "Failed to source detect-platform.zsh"; exit 1; }
source "$OSA_CLI_DIR/src/zsh/helpers/system-report.zsh" || { echo "Failed to source system-report.zsh"; exit 1; }
# Colors for output (use direct escape codes in zsh)
COLOR_RESET=$'\033[0m'
COLOR_BOLD=$'\033[1m'
COLOR_GREEN=$'\033[0;32m'
COLOR_YELLOW=$'\033[0;33m'
COLOR_BLUE=$'\033[0;34m'
COLOR_RED=$'\033[0;31m'
COLOR_CYAN=$'\033[0;36m'
# Configuration file
OSA_CONFIG_FILE="$HOME/.osa-config"
# Verbose mode flag (export so scripts can use it)
export OSA_VERBOSE=false
# Mise global setup flag (export so scripts can use it)
export OSA_SKIP_MISE_GLOBAL=false
# OSA Snippets flag (enabled by default, can be disabled with --disable-osa-snippets)
export OSA_SKIP_SNIPPETS=false
# Git configuration flag (can be disabled with --disable-git)
export OSA_SKIP_GIT_CONFIG=false
# CocoaPods flag (can be disabled with --skip-cocoapods for testing)
export OSA_SKIP_COCOAPODS=false
# Setup profile name (tracks which preset was used: minimal, react-native, etc.)
export OSA_SETUP_PROFILE="everything"
# Helper function to normalize component key to valid variable name
# Replaces hyphens with underscores and converts to uppercase
normalize_key() {
echo "${1:gs/-/_/:u}"
}
# Flatten nested YAML structure into flat OSA_CONFIG_* environment variables
# Examples:
# components.symlinks=true → OSA_CONFIG_COMPONENTS_SYMLINKS=true
# runtimes.node.version=22 → OSA_CONFIG_RUNTIMES_NODE_VERSION=22
# snippets.osasnippets.enabled=true → OSA_CONFIG_SNIPPETS_OSASNIPPETS_ENABLED=true
flatten_yaml_to_env_vars() {
local resolved_path="$1"
if [[ ! -f "$resolved_path" ]]; then
echo -e "${COLOR_RED}✗${COLOR_RESET} Config file not found: $resolved_path"
return 1
fi
# Parse profile name
local profile=$(yq eval '.profile' "$resolved_path" 2>/dev/null)
if [[ -z "$profile" ]]; then
echo -e "${COLOR_RED}✗${COLOR_RESET} Config missing 'profile' field"
return 1
fi
typeset -gx "OSA_CONFIG_PROFILE=$profile"
# Flatten components section - get ALL keys dynamically
local all_component_keys=$(yq eval '.components | keys | .[]' "$resolved_path" 2>/dev/null)
while IFS= read -r key; do
[[ -z "$key" ]] && continue
local value=$(yq eval ".components.${key}" "$resolved_path" 2>/dev/null)
local var_name="OSA_CONFIG_COMPONENTS_$(echo $key | tr a-z A-Z | tr '-' '_')"
typeset -gx "$var_name=$value"
done <<< "$all_component_keys"
# Flatten runtimes section
local -a runtime_keys=(node python ruby java rust go deno elixir erlang)
for runtime in "${runtime_keys[@]}"; do
local enabled=$(yq eval ".runtimes.${runtime}.enabled // false" "$resolved_path" 2>/dev/null)
local version=$(yq eval ".runtimes.${runtime}.version // \"latest\"" "$resolved_path" 2>/dev/null)
local enabled_var="OSA_CONFIG_RUNTIMES_$(echo $runtime | tr a-z A-Z)_ENABLED"
local version_var="OSA_CONFIG_RUNTIMES_$(echo $runtime | tr a-z A-Z)_VERSION"
typeset -gx "$enabled_var=$enabled"
typeset -gx "$version_var=$version"
done
# Flatten snippets section
# Get list of snippet repo names
local snippet_repos=$(yq eval '.snippets | keys | .[]' "$resolved_path" 2>/dev/null)
if [[ -n "$snippet_repos" ]]; then
while IFS= read -r repo; do
[[ -z "$repo" ]] && continue
local repo_upper=$(echo "$repo" | tr a-z A-Z | tr '-' '_')
local enabled=$(yq eval ".snippets.${repo}.enabled" "$resolved_path" 2>/dev/null)
local enabled_var="OSA_CONFIG_SNIPPETS_${repo_upper}_ENABLED"
typeset -gx "$enabled_var=$enabled"
# Get features array
local features=$(yq eval ".snippets.${repo}.features" "$resolved_path" 2>/dev/null)
# If features is not a list/null, continue
if [[ "$features" == "null" || -z "$features" ]]; then
continue
fi
# Parse each feature from the list
local feature_list=$(yq eval ".snippets.${repo}.features | .[]" "$resolved_path" 2>/dev/null)
while IFS= read -r feature; do
[[ -z "$feature" ]] && continue
local feature_upper=$(echo "$feature" | tr a-z A-Z | tr '-' '_')
local feature_var="OSA_CONFIG_SNIPPETS_${repo_upper}_${feature_upper}"
typeset -gx "$feature_var=true"
done <<< "$feature_list"
done <<< "$snippet_repos"
fi
# Flatten duti section (macOS default app overrides)
# Pattern: .extension: bundle_id
local duti_overrides=$(yq eval '.duti | keys | .[]' "$resolved_path" 2>/dev/null)
if [[ -n "$duti_overrides" ]]; then
while IFS= read -r extension; do
[[ -z "$extension" ]] && continue
local bundle_id=$(yq eval ".duti.\"${extension}\"" "$resolved_path" 2>/dev/null | tr -d '\n')
if [[ -n "$bundle_id" && "$bundle_id" != "null" ]]; then
local ext_var="OSA_CONFIG_DUTI_${extension//./}_BUNDLE_ID"
typeset -gx "${ext_var}=${bundle_id}"
fi
done <<< "$duti_overrides"
fi
return 0
}
# Validate runtime version string (security: prevent command injection)
validate_version_string() {
local version="$1"
local runtime_name="$2"
# Only allow: alphanumeric, dots, dashes, underscores
# Block: semicolons, pipes, backticks, $(), spaces, etc.
if [[ ! "$version" =~ ^[a-zA-Z0-9._-]+$ ]]; then
echo -e "${COLOR_RED}✗ Invalid version format for ${runtime_name}: $version${COLOR_RESET}"
echo -e "${COLOR_YELLOW}Version must contain only: a-z A-Z 0-9 . _ -${COLOR_RESET}"
return 1
fi
# Length limit (prevent buffer overflow / DoS)
if [[ ${#version} -gt 50 ]]; then
echo -e "${COLOR_RED}✗ Version string too long for ${runtime_name}: $version${COLOR_RESET}"
return 1
fi
return 0
}
# Load configuration from JSON file
# Usage: load_json_config "minimal" or load_json_config "configs/minimal.yaml" or load_json_config "/full/path/config.yaml"
load_json_config() {
local json_file="$1"
local resolved_path=""
# Auto-resolve config name to configs/ directory
if [[ ! "$json_file" =~ "/" ]]; then
# No path separator - try configs/ directory with and without .yaml extension
if [[ -f "$OSA_CLI_DIR/configs/${json_file}.yaml" ]]; then
resolved_path="$OSA_CLI_DIR/configs/${json_file}.yaml"
elif [[ -f "$OSA_CLI_DIR/configs/$json_file" ]]; then
resolved_path="$OSA_CLI_DIR/configs/$json_file"
else
echo -e "${COLOR_RED}Error: Config not found: $json_file${COLOR_RESET}"
echo -e "${COLOR_YELLOW}Available configs:${COLOR_RESET}"
list_configs 2>/dev/null | head -20
return 1
fi
elif [[ "$json_file" =~ ^/ ]]; then
# Absolute path - use as-is
resolved_path="$json_file"
else
# Relative path - resolve relative to repo root
resolved_path="$OSA_CLI_DIR/$json_file"
fi
if [[ ! -f "$resolved_path" ]]; then
echo -e "${COLOR_RED}Error: Config file not found: $resolved_path${COLOR_RESET}"
return 1
fi
# Check if yq is available
if ! command -v yq &> /dev/null; then
echo -e "${COLOR_RED}✗${COLOR_RESET} yq not found - required for YAML config parsing"
echo ""
echo "Install with:"
if [[ "$OSA_IS_MACOS" == "true" ]]; then
echo " brew install yq"
else
echo " apt-get install yq (Ubuntu/Debian)"
echo " yum install yq (RHEL/CentOS)"
echo " pacman -S yq (Arch)"
fi
echo ""
echo "Or run './osa-cli.zsh --interactive' instead (no yq needed)"
return 1
fi
# Flatten YAML to env vars (populates OSA_CONFIG_* variables)
if ! flatten_yaml_to_env_vars "$resolved_path"; then
return 1
fi
# For backward compatibility during installation, also set OSA_SETUP_* variables
# These are used by run_component() to determine what to install
local -a component_keys=(symlinks oh_my_zsh zsh_plugins homebrew mise osa_snippets git android iterm2 vscode cocoapods depot_tools 7zip duti)
for key in "${component_keys[@]}"; do
local enabled=$(yq eval ".components.${key} // false" "$resolved_path" 2>/dev/null | tr -d '\n')
local var_name="OSA_SETUP_$(normalize_key "$key")"
if [[ "$enabled" == "true" ]]; then
typeset -gx "$var_name=true"
else
typeset -gx "$var_name=false"
fi
done
# Load runtimes into MISE_*_VERSION for runtime installation
local -a runtime_keys=(node python ruby java rust go deno elixir erlang)
for runtime in "${runtime_keys[@]}"; do
local enabled=$(yq eval ".runtimes.${runtime}.enabled // false" "$resolved_path" 2>/dev/null | tr -d '\n')
local version=$(yq eval ".runtimes.${runtime}.version // \"latest\"" "$resolved_path" 2>/dev/null | tr -d '\n')
if [[ "$enabled" == "true" ]]; then
# Validate version string contains only safe characters
if ! validate_version_string "$version" "$runtime"; then
return 1
fi
local var_name="MISE_$(echo $runtime | tr a-z A-Z)_VERSION"
# Safe assignment without eval
typeset -g "$var_name=$version"
fi
done
echo -e "${COLOR_GREEN}✓${COLOR_RESET} Configuration loaded from: $resolved_path"
return 0
}
# Load remote configuration from URL
# Usage: load_remote_config "https://example.com/config.yaml"
load_remote_config() {
local url="$1"
local temp_config="/tmp/osa-remote-config-$$.yaml"
# Validate URL is HTTPS only (security: prevent HTTP MITM attacks)
if [[ ! "$url" =~ ^https:// ]]; then
echo -e "${COLOR_RED}Error: Only HTTPS URLs are supported for security${COLOR_RESET}"
echo -e "${COLOR_YELLOW}Provided: $url${COLOR_RESET}"
return 1
fi
echo -e "${COLOR_CYAN}Downloading remote configuration...${COLOR_RESET}"
echo -e "${COLOR_CYAN}URL: $url${COLOR_RESET}"
echo ""
# Try curl first, then wget
if command -v curl &> /dev/null; then
if ! curl -fsSL "$url" -o "$temp_config" 2>/dev/null; then
echo -e "${COLOR_RED}Error: Failed to download config from: $url${COLOR_RESET}"
rm -f "$temp_config"
return 1
fi
elif command -v wget &> /dev/null; then
if ! wget -q "$url" -O "$temp_config" 2>/dev/null; then
echo -e "${COLOR_RED}Error: Failed to download config from: $url${COLOR_RESET}"
rm -f "$temp_config"
return 1
fi
else
echo -e "${COLOR_RED}Error: Neither curl nor wget found. Install one to use remote configs.${COLOR_RESET}"
return 1
fi
# Validate it's a YAML file
if ! command -v yq &> /dev/null; then
echo -e "${COLOR_RED}Error: yq is required to parse remote configs. Install with: brew install yq${COLOR_RESET}"
rm -f "$temp_config"
return 1
fi
if ! yq eval '.' "$temp_config" > /dev/null 2>&1; then
echo -e "${COLOR_RED}Error: Downloaded file is not valid YAML${COLOR_RESET}"
rm -f "$temp_config"
return 1
fi
echo -e "${COLOR_GREEN}✓${COLOR_RESET} Downloaded configuration"
echo ""
# Show preview of what will be installed
echo -e "${COLOR_BOLD}Configuration Preview:${COLOR_RESET}"
local description=$(yq eval '.description // "No description"' "$temp_config" 2>/dev/null)
echo " Description: $description"
echo ""
# Ask for confirmation unless in dry-run mode
if [[ "$OSA_DRY_RUN" != "true" ]]; then
echo -n -e "${COLOR_YELLOW}?${COLOR_RESET} Do you want to proceed with this remote configuration? [y/N] "
read response
if [[ ! "$response" =~ ^[Yy] ]]; then
echo -e "${COLOR_YELLOW}Cancelled${COLOR_RESET}"
rm -f "$temp_config"
return 1
fi
echo ""
fi
# Load the config
load_json_config "$temp_config"
local result=$?
# Clean up temp file
rm -f "$temp_config"
return $result
}
# Setup components registry
declare -A SETUP_COMPONENTS
declare -A COMPONENT_DESCRIPTIONS
declare -A COMPONENT_PLATFORMS
# Register setup components
register_component() {
local key="$1"
local description="$2"
local platforms="$3" # comma-separated: macos,linux,wsl or "all"
local script_path="$4"
SETUP_COMPONENTS[$key]="$script_path"
COMPONENT_DESCRIPTIONS[$key]="$description"
COMPONENT_PLATFORMS[$key]="$platforms"
}
# Initialize components
init_components() {
# Core setup components (REQUIRED - have installation scripts)
register_component "symlinks" "Create symlinks for zshrc and repo" "all" "src/setup/initialize-repo-symlinks.zsh"
register_component "homebrew" "Install Homebrew package manager" "macos" "src/setup/install-brew.zsh"
register_component "oh-my-zsh" "Install Oh My Zsh framework" "all" "src/setup/oh-my-zsh.zsh"
register_component "zsh-plugins" "Install zsh plugins (syntax highlighting, etc)" "all" "src/setup/install-oh-my-zsh-plugins.zsh"
# Runtime/Version managers (RECOMMENDED - mise-based)
register_component "mise" "Install mise (polyglot runtime manager)" "all" "src/setup/install-mise.zsh"
# Community scripts and helpers
register_component "osa-snippets" "Clone osa-snippets repo (community shell functions/helpers)" "all" "src/setup/install-osa-snippets.zsh"
# Development tools with install scripts
register_component "git" "Configure Git (version control)" "all" "src/setup/git.zsh"
register_component "cocoapods" "Install CocoaPods for iOS development" "macos" "src/setup/install-cocoapods.zsh"
register_component "depot_tools" "Install depot_tools (Chromium development utilities)" "all" "src/setup/install-depot-tools.zsh"
register_component "7zip" "Install 7zip (fast multi-threaded archive extraction for macOS)" "macos" "src/setup/install-7zip.zsh"
register_component "duti" "Install duti (macOS default app manager)" "macos" "src/setup/install-duti.zsh"
}
# Check if component is available for current platform
is_component_available() {
local key="$1"
local platforms="${COMPONENT_PLATFORMS[$key]}"
if [[ "$platforms" == "all" ]]; then
return 0
fi
# Check if current platform is in the list
if [[ ",$platforms," == *",$OSA_OS,"* ]]; then
return 0
fi
return 1
}
# Load configuration from file
# Sources ~/.osa-config which contains flattened OSA_CONFIG_* variables
load_config() {
if [[ -f "$OSA_CONFIG_FILE" ]]; then
# Source config file (contains flattened OSA_CONFIG_* variables)
source "$OSA_CONFIG_FILE" 2>/dev/null
return 0
fi
return 1
}
# NOTE FOR TESTS: Do NOT call load_config here automatically during script startup
# This prevents stale user configuration from affecting test runs or interactive
# invocation. Tests look for the exact marker string: "Do NOT call load_config here"
# Save configuration to file in flattened format
save_config() {
local temp_file="/tmp/osa-config-$$.tmp"
{
echo "# OSA Configuration"
echo "# Generated on $(date)"
echo "# Profile: ${OSA_CONFIG_PROFILE:-${OSA_SETUP_PROFILE:-custom}}"
echo "#"
echo "# This file contains flattened configuration variables from YAML"
echo "# Format: OSA_CONFIG_<SECTION>_<KEY>=<VALUE>"
echo "#"
echo "# Component flags (setup components to install):"
echo "# OSA_CONFIG_COMPONENTS_<NAME>=true|false"
echo "#"
echo "# Runtime versions (installed via mise):"
echo "# OSA_CONFIG_RUNTIMES_<NAME>_ENABLED=true|false"
echo "# OSA_CONFIG_RUNTIMES_<NAME>_VERSION=<version>"
echo "#"
echo "# Snippets features (loaded from snippet repos):"
echo "# OSA_CONFIG_SNIPPETS_<REPO>_ENABLED=true|false"
echo "# OSA_CONFIG_SNIPPETS_<REPO>_<FEATURE>=true"
echo "#"
echo ""
# Export OSA_CONFIG_PROFILE
echo "OSA_CONFIG_PROFILE='${OSA_CONFIG_PROFILE:-${OSA_SETUP_PROFILE:-everything}}'"
echo ""
# Export all OSA_CONFIG_* variables that were set during config loading
# Use a more robust method than compgen
local all_vars=(${(k)parameters})
for var in $all_vars; do
if [[ $var == OSA_CONFIG_* ]]; then
local value="${(P)var}"
# Escape single quotes in values
value="${value//\'/\'\\\'\'}"
echo "${var}='${value}'"
fi
done
# If no OSA_CONFIG_* variables exist (interactive mode), convert OSA_SETUP_* to OSA_CONFIG_*
if [[ -z "$(echo $all_vars | grep '^OSA_CONFIG_')" ]]; then
# Convert OSA_SETUP_* component flags to OSA_CONFIG_COMPONENTS_*
local -a component_keys=(symlinks homebrew oh_my_zsh zsh_plugins mise osa_snippets git android iterm2 vscode cocoapods depot_tools 7zip duti)
for key in "${component_keys[@]}"; do
local var_name="OSA_SETUP_$(echo $key | tr a-z A-Z | tr '-' '_')"
local value="${(P)var_name}"
local config_name="OSA_CONFIG_COMPONENTS_$(echo $key | tr a-z A-Z | tr '-' '_')"
echo "${config_name}='${value:-false}'"
done
# Export runtimes (from OSA_SETUP_RUNTIME_* or OSA_SETUP_RUNTIMES_*)
local -a runtime_keys=(node python ruby java rust go deno elixir erlang)
for runtime in "${runtime_keys[@]}"; do
local enabled_var="OSA_SETUP_RUNTIME_$(echo $runtime | tr a-z A-Z)_ENABLED"
local version_var="OSA_SETUP_RUNTIME_$(echo $runtime | tr a-z A-Z)_VERSION"
local enabled="${(P)enabled_var}"
local version="${(P)version_var}"
echo "OSA_CONFIG_RUNTIMES_$(echo $runtime | tr a-z A-Z)_ENABLED='${enabled:-false}'"
echo "OSA_CONFIG_RUNTIMES_$(echo $runtime | tr a-z A-Z)_VERSION='${version:-latest}'"
done
fi
} > "$temp_file"
# Move temp file to final location
mv "$temp_file" "$OSA_CONFIG_FILE"
echo -e "${COLOR_GREEN}✓${COLOR_RESET} Configuration saved to $OSA_CONFIG_FILE"
}
# Ask user yes/no question
ask_yes_no() {
local question="$1"
local default="${2:-n}"
local prompt
if [[ "$default" == "y" ]]; then
prompt="[Y/n]"
else
prompt="[y/N]"
fi
echo -n -e "${COLOR_CYAN}?${COLOR_RESET} $question $prompt "
read response
response="${response:-$default}"
if [[ "$response" =~ ^[Yy] ]]; then
return 0
else
return 1
fi
}
# Ask user to select from a list of options with scrollable menu
select_from_list() {
local prompt="$1"
shift
local -a options=("$@")
local selection=""
local i
echo -e "${COLOR_CYAN}?${COLOR_RESET} $prompt" >&2
for ((i=1; i<=${#options[@]}; i++)); do
echo " $i) ${options[$i]}" >&2
done
echo -n -e "Select option [1]: " >&2
read selection
selection=${selection:-1}
if [[ "$selection" =~ ^[0-9]+$ ]] && [[ "$selection" -ge 1 ]] && [[ "$selection" -le ${#options[@]} ]]; then
echo "${options[$selection]}"
return 0
else
echo "Invalid selection, using default: ${options[1]}" >&2
echo "${options[1]}"
return 1
fi
}
# Interactive menu to select multiple runtimes for mise
select_runtimes_for_mise() {
local all_runtimes
all_runtimes=(node python ruby java rust go deno elixir erlang)
echo -e "${COLOR_BOLD}Select runtimes to install with mise:${COLOR_RESET}"
echo ""
echo "Available runtimes:"
# Display all runtimes with numbers (1-indexed)
local i=1
for runtime in "${all_runtimes[@]}"; do
echo " $i. $runtime"
((i++))
done
echo ""
echo "Enter runtime numbers to install (space-separated, or enter for Node.js only)"
echo "Example: 1 2 4 (installs Node, Python, Java)"
echo -n "> "
read -r selection
# Parse selections (default to node if empty)
selection="${selection:-1}"
echo ""
echo -e "${COLOR_BOLD}Configuring selected runtimes:${COLOR_RESET}"
echo ""
# Process each selected index
for idx in $selection; do
if [[ "$idx" =~ ^[0-9]+$ ]] && [[ "$idx" -ge 1 ]] && [[ "$idx" -le ${#all_runtimes[@]} ]]; then
local runtime_idx=$((idx - 1))
local runtime="${all_runtimes[$runtime_idx]}"
case "$runtime" in
node)
echo "Node.js versions available: 20 (LTS), 22 (LTS), 24 (Latest)"
echo -n "Select Node version [22]: "
read -r node_version
node_version="${node_version:-22}"
if ! validate_version_string "$node_version" "Node.js"; then
echo "Skipping Node.js installation due to invalid version"
continue
fi
export MISE_NODE_VERSION="$node_version"
echo " ✓ Node.js: $node_version"
;;
python)
echo "Python versions available: 3.11, 3.12, 3.13"
echo -n "Select Python version [3.13]: "
read -r python_version
python_version="${python_version:-3.13}"
if ! validate_version_string "$python_version" "Python"; then
echo "Skipping Python installation due to invalid version"
continue
fi
export MISE_PYTHON_VERSION="$python_version"
echo " ✓ Python: $python_version"
;;
ruby)
echo "Ruby versions available: 3.2.0, 3.3.0, 3.4.0"
echo -n "Select Ruby version [3.4.0]: "
read -r ruby_version
ruby_version="${ruby_version:-3.4.0}"
if ! validate_version_string "$ruby_version" "Ruby"; then
echo "Skipping Ruby installation due to invalid version"
continue
fi
export MISE_RUBY_VERSION="$ruby_version"
echo " ✓ Ruby: $ruby_version"
;;
java)
echo "Java versions available: zulu-17 zulu-17, zulu-21, zulu zulu-23"
echo -n "Select Java version [zulu-17]: "
read -r java_version
java_version="${java_version:-zulu-17}"
if ! validate_version_string "$java_version" "Java"; then
echo "Skipping Java installation due to invalid version"
continue
fi
export MISE_JAVA_VERSION="$java_version"
echo " ✓ Java: $java_version"
;;
rust)
export MISE_RUST_VERSION="stable"
echo " ✓ Rust: stable"
;;
go)
echo "Go versions available: 1.21, 1.22, 1.23"
echo -n "Select Go version [1.23]: "
read -r go_version
go_version="${go_version:-1.23}"
if ! validate_version_string "$go_version" "Go"; then
echo "Skipping Go installation due to invalid version"
continue
fi
export MISE_GO_VERSION="$go_version"
echo " ✓ Go: $go_version"
;;
deno)
export MISE_DENO_VERSION="latest"
echo " ✓ Deno: latest"
;;
elixir)
export MISE_ELIXIR_VERSION="latest"
echo " ✓ Elixir: latest"
;;
erlang)
export MISE_ERLANG_VERSION="latest"
echo " ✓ Erlang: latest"
;;
esac
fi
done
echo ""
}
# Validate a configuration file for syntax errors and show what would be loaded
validate_config() {
local config_path="$1"
if [[ -z "$config_path" ]]; then
echo -e "${COLOR_RED}✗${COLOR_RESET} Config path required"
return 1
fi
# Resolve config path
local resolved_path=""
if [[ ! "$config_path" =~ "/" ]]; then
# No path separator - try configs/ directory
if [[ -f "$OSA_CLI_DIR/configs/${config_path}.yaml" ]]; then
resolved_path="$OSA_CLI_DIR/configs/${config_path}.yaml"
elif [[ -f "$OSA_CLI_DIR/configs/$config_path" ]]; then
resolved_path="$OSA_CLI_DIR/configs/$config_path"
else
echo -e "${COLOR_RED}✗${COLOR_RESET} Config not found: $config_path"
echo -e "${COLOR_YELLOW}Available configs:${COLOR_RESET}"
list_configs 2>/dev/null | head -20
return 1
fi
elif [[ "$config_path" =~ ^/ ]]; then
resolved_path="$config_path"
else
resolved_path="$OSA_CLI_DIR/$config_path"
fi
if [[ ! -f "$resolved_path" ]]; then
echo -e "${COLOR_RED}✗${COLOR_RESET} Config file not found: $resolved_path"
return 1
fi
# Check if yq is available
if ! command -v yq &> /dev/null; then
echo -e "${COLOR_RED}✗${COLOR_RESET} yq not found - required to validate YAML config"
echo ""
echo "Install with:"
if [[ "$OSA_IS_MACOS" == "true" ]]; then
echo " brew install yq"
else
echo " apt-get install yq (Ubuntu/Debian)"
echo " yum install yq (RHEL/CentOS)"
echo " pacman -S yq (Arch)"
fi
return 1
fi
echo -e "${COLOR_BOLD}${COLOR_CYAN}▶ Validating configuration: $(basename "$resolved_path")${COLOR_RESET}"
echo ""
# Test YAML syntax
if ! yq eval '.' "$resolved_path" > /dev/null 2>&1; then
echo -e "${COLOR_RED}✗ YAML syntax error:${COLOR_RESET}"
yq eval '.' "$resolved_path" 2>&1 | head -20
return 1
fi
echo -e "${COLOR_GREEN}✓${COLOR_RESET} YAML syntax is valid"
echo ""
# Show what will be loaded
echo -e "${COLOR_BOLD}Configuration Summary:${COLOR_RESET}"
echo ""
local profile=$(yq eval '.profile' "$resolved_path" 2>/dev/null)
local description=$(yq eval '.description' "$resolved_path" 2>/dev/null)
echo " Profile: ${COLOR_CYAN}${profile}${COLOR_RESET}"
echo " Description: $description"
echo ""
# Show enabled components
echo -e "${COLOR_BOLD}Setup Components (to be installed):${COLOR_RESET}"
local -a component_keys=(symlinks oh_my_zsh zsh_plugins homebrew mise osa_snippets git cocoapods depot_tools 7zip duti)
for key in "${component_keys[@]}"; do
local enabled=$(yq eval ".components.${key} // false" "$resolved_path" 2>/dev/null)
if [[ "$enabled" == "true" ]]; then
echo -e " ${COLOR_GREEN}✓${COLOR_RESET} $key"
fi
done
echo ""
# Show enabled runtimes
echo -e "${COLOR_BOLD}Runtimes (installed via mise):${COLOR_RESET}"
local -a runtime_keys=(node python ruby java rust go deno elixir erlang)
local any_enabled=false
for runtime in "${runtime_keys[@]}"; do
local enabled=$(yq eval ".runtimes.${runtime}.enabled // false" "$resolved_path" 2>/dev/null)
if [[ "$enabled" == "true" ]]; then
local version=$(yq eval ".runtimes.${runtime}.version // \"latest\"" "$resolved_path" 2>/dev/null)
echo -e " ${COLOR_GREEN}✓${COLOR_RESET} $runtime (${COLOR_CYAN}${version}${COLOR_RESET})"
any_enabled=true
fi
done
if [[ "$any_enabled" != "true" ]]; then
echo " (none enabled)"
fi
echo ""
# Show snippet repos
echo -e "${COLOR_BOLD}Snippet Repositories:${COLOR_RESET}"
local snippet_repos=$(yq eval '.snippets | keys | .[]' "$resolved_path" 2>/dev/null)
if [[ -n "$snippet_repos" ]]; then
while IFS= read -r repo; do
[[ -z "$repo" ]] && continue
local enabled=$(yq eval ".snippets.${repo}.enabled" "$resolved_path" 2>/dev/null)
if [[ "$enabled" == "true" ]]; then
local features=$(yq eval ".snippets.${repo}.features | .[]" "$resolved_path" 2>/dev/null)
if [[ -n "$features" ]]; then
echo -e " ${COLOR_GREEN}✓${COLOR_RESET} $repo"
while IFS= read -r feature; do
[[ -z "$feature" ]] && continue
echo -e " • ${COLOR_CYAN}${feature}${COLOR_RESET}"
done <<< "$features"
else
echo -e " ${COLOR_GREEN}✓${COLOR_RESET} $repo (no specific features)"
fi
fi
done <<< "$snippet_repos"
else
echo " (none)"
fi
echo ""
echo -e "${COLOR_GREEN}✓ Configuration is valid and ready to use${COLOR_RESET}"
echo ""
echo -e "${COLOR_BOLD}To apply this configuration, run:${COLOR_RESET}"
echo " ${COLOR_CYAN}./osa-cli.zsh --config ${profile}${COLOR_RESET}"
echo " ${COLOR_CYAN}./osa-cli.zsh --config ${profile} --dry-run${COLOR_RESET} (preview without installing)"
echo ""
return 0
}
# Global flag for dry-run mode
OSA_DRY_RUN=false
# Run a setup component
run_component() {
local key="$1"
local script_path="${SETUP_COMPONENTS[$key]}"
if [[ -z "$script_path" && "$key" != "android" ]]; then
echo -e "${COLOR_RED}✗${COLOR_RESET} Unknown component: $key"
return 1
fi
if ! is_component_available "$key"; then
echo -e "${COLOR_YELLOW}⊘${COLOR_RESET} Component '$key' not available for $OSA_OS"
return 0
fi
# Special handling for Android (configuration-only component)
if [[ "$key" == "android" ]]; then
if [[ "$OSA_DRY_RUN" == "true" ]]; then
echo -e "${COLOR_CYAN}[DRY-RUN]${COLOR_RESET} Would enable: ${COMPONENT_DESCRIPTIONS[$key]}"
echo -e "${COLOR_CYAN}[DRY-RUN]${COLOR_RESET} Sets: OSA_SETUP_ANDROID=true"
return 0
fi
echo -e "${COLOR_BLUE}▶${COLOR_RESET} Enabling: ${COMPONENT_DESCRIPTIONS[$key]}"
echo -e "${COLOR_GREEN}✓${COLOR_RESET} Android support enabled (requires ANDROID_HOME to be set)"
return 0
fi
local full_path="$OSA_CLI_DIR/$script_path"
if [[ ! -f "$full_path" ]]; then
echo -e "${COLOR_RED}✗${COLOR_RESET} Script not found: $full_path"
return 1
fi
if [[ "$OSA_DRY_RUN" == "true" ]]; then
echo -e "${COLOR_CYAN}[DRY-RUN]${COLOR_RESET} Would run: ${COMPONENT_DESCRIPTIONS[$key]}"
echo -e "${COLOR_CYAN}[DRY-RUN]${COLOR_RESET} Script: $script_path"
return 0
fi
echo -e "${COLOR_BLUE}▶${COLOR_RESET} Running: ${COMPONENT_DESCRIPTIONS[$key]}"
# Source the script (most setup scripts are meant to be sourced)
if source "$full_path"; then
echo -e "${COLOR_GREEN}✓${COLOR_RESET} Completed: $key"
return 0
else
echo -e "${COLOR_RED}✗${COLOR_RESET} Failed: $key"
return 1
fi
}
# Interactive setup mode
interactive_setup() {
echo -e "${COLOR_BOLD}${COLOR_CYAN}"
echo "╔════════════════════════════════════════════════════════════╗"
echo "║ Open Source Automation (OSA) - Interactive Setup ║"
echo "╚════════════════════════════════════════════════════════════╝"
echo -e "${COLOR_RESET}"
print_platform_info
if ! is_platform_supported; then
echo -e "${COLOR_RED}✗ Platform '$OSA_OS' is not currently supported${COLOR_RESET}"
exit 1
fi
# macOS pre-flight check: Xcode Command Line Tools
if [[ "$OSA_IS_MACOS" == "true" ]]; then
# Check if xcode-select returns a valid path (CLT is installed)
# xcode-select -p returns the path to CLT, or fails if not installed
local xcode_path=$(xcode-select -p 2>/dev/null)
if [[ -z "$xcode_path" ]] || [[ ! -d "$xcode_path" ]]; then
echo -e "${COLOR_YELLOW}⚠️ Xcode Command Line Tools not found${COLOR_RESET}"
echo ""
echo "Homebrew and many development tools require Xcode CLT."
echo "Install from Apple Developer:"
echo " ${COLOR_BOLD}https://developer.apple.com/download/more/${COLOR_RESET}"
echo ""
echo "Or try: ${COLOR_BOLD}xcode-select --install${COLOR_RESET}"
echo ""
fi
fi
echo -e "${COLOR_BOLD}Select components to install:${COLOR_RESET}\n"
# CRITICAL: Always include required components IN CORRECT ORDER
# Order matters! Symlinks must be first, then platform tools, then shell setup
local -a selected_components
# 1. SYMLINKS FIRST (creates ~/.osa directory structure)
selected_components+=("symlinks")
OSA_SETUP_SYMLINKS=true
# 2. HOMEBREW (if on macOS - needed before installing other tools)
if [[ "$OSA_IS_MACOS" == "true" ]]; then
selected_components+=("homebrew")
OSA_SETUP_HOMEBREW=true
fi
# 3. OH-MY-ZSH (depends on ~/.osa existing)
selected_components+=("oh-my-zsh")
OSA_SETUP_OH_MY_ZSH=true
# 4. ZSH PLUGINS (depends on oh-my-zsh being installed)
selected_components+=("zsh-plugins")
OSA_SETUP_ZSH_PLUGINS=true
echo -e "${COLOR_GREEN}Core components (required):${COLOR_RESET}"
echo " ✓ Symlinks (repo, .zshrc)"
echo " ✓ Oh My Zsh framework"
echo " ✓ Zsh plugins (syntax highlighting, evalcache)"
if [[ "$OSA_IS_MACOS" == "true" ]]; then
echo " ✓ Homebrew"
fi
echo ""
echo -e "${COLOR_BOLD}Recommended: Install Mise first${COLOR_RESET}"
if ask_yes_no "Install mise (polyglot runtime manager for Node, Ruby, Java, Python, Go, Rust, etc)?" "y"; then
selected_components+=("mise")
OSA_SETUP_MISE=true
echo ""
select_runtimes_for_mise
echo ""
else
OSA_SETUP_MISE=false
fi
echo ""
echo -e "${COLOR_BOLD}Core Productivity Tools${COLOR_RESET}"
if ask_yes_no "Install osa-scripts (community helpers and productivity functions)?" "y"; then
selected_components+=("osa-snippets")
OSA_SETUP_OSA_SNIPPETS=true
else
OSA_SETUP_OSA_SNIPPETS=false
fi
echo ""
# Ask about optional development tools
echo -e "${COLOR_BOLD}Optional components:${COLOR_RESET}\n"
# Skip the required components and mise (already handled above), ask about everything else
for key in "${(@k)SETUP_COMPONENTS}"; do
# Skip required components, mise, and osa-snippets (which are handled specially)
if [[ "$key" == "symlinks" ]] || [[ "$key" == "oh-my-zsh" ]] || [[ "$key" == "zsh-plugins" ]] || [[ "$key" == "homebrew" ]] || [[ "$key" == "mise" ]] || [[ "$key" == "osa-snippets" ]]; then
continue
fi
# Special handling for CocoaPods - ask with version selection (unless --skip-cocoapods)
if [[ "$key" == "cocoapods" ]]; then
if [[ "$OSA_SKIP_COCOAPODS" == "true" ]]; then
echo -e "${COLOR_YELLOW}⊘${COLOR_RESET} Skipping CocoaPods (--skip-cocoapods flag set)"
continue
fi
if is_component_available "$key"; then
# Source the interactive CocoaPods setup
if source "$OSA_CLI_DIR/src/setup/install-cocoapods-interactive.zsh"; then
if ask_install_cocoapods; then
selected_components+=("$key")
# Already set OSA_SETUP_COCOAPODS in the function
fi
fi
fi
continue
fi
if is_component_available "$key"; then
local desc="${COMPONENT_DESCRIPTIONS[$key]}"
if ask_yes_no "Install $desc?" "n"; then
selected_components+=("$key")
# Set config variable (safe assignment without eval)
local var_name="OSA_SETUP_$(normalize_key "$key")"
typeset -g "$var_name=true"
else
local var_name="OSA_SETUP_$(normalize_key "$key")"
typeset -g "$var_name=false"
fi
fi
done
echo ""
if [[ ${#selected_components[@]} -eq 0 ]]; then
echo -e "${COLOR_YELLOW}No components selected. Exiting.${COLOR_RESET}"
return 0
fi
# Confirm and save config
if ask_yes_no "Save this configuration for future runs?" "y"; then
save_config
fi
echo -e "\n${COLOR_BOLD}Starting installation...${COLOR_RESET}\n"
# Run selected components
local failed=0
for key in "${selected_components[@]}"; do
if ! run_component "$key"; then
failed=$((failed + 1))
fi
echo ""
done
# Summary
echo -e "${COLOR_BOLD}═══════════════════════════════════════${COLOR_RESET}"
if [[ $failed -eq 0 ]]; then
echo -e "${COLOR_GREEN}✓ All components installed successfully!${COLOR_RESET}"