-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnow
More file actions
executable file
·972 lines (781 loc) · 19.5 KB
/
snow
File metadata and controls
executable file
·972 lines (781 loc) · 19.5 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
#!/usr/bin/env bash
###############################################################################################
#
# SnowBlower: All Flake No Fluff
# https://github.com/use-the-fork/snowblower
#
set -e
set -o pipefail
function isSnowBlowerUp {
if ! isSnowBlowerDockerContainerUp; then
_iError "SnowBlower is not running."
_iNote "You must first run 'snow up'."
exit 1
fi
}
function isSnowBlowerDockerContainerUp {
# Check if snowblower-dev service container exists and is running
[ -n "$($SB_DOCKER_PATH compose -f "$SB_WORKSPACE_ROOT/docker-compose.yml" ps -q "$SB_APP_SERVICE" 2>/dev/null)" ]
}
function isSnowBlowerNixVolumeCreated {
[ -n "$($SB_DOCKER_PATH volume inspect snowblower-nix 2>/dev/null)" ]
}
# Function that checks
function doRunChecks {
if [ -z "$SB_SKIP_CHECKS" ]; then
# Ensure that SnowBlower is running...
if ! isSnowBlowerDockerContainerUp; then
_iFail "${BOLD}SnowBlower is not running.${NC}" >&2
_iNote "Run 'snow up' to start SnowBlower."
exit 1
fi
# Determine if SnowBlower is currently up...
# if $SB_DOCKER_PATH compose -f "$SB_WORKSPACE_ROOT/docker-compose.yml" ps -q "$SB_APP_SERVICE" 2>&1 | grep 'Exit\|exited'; then
# _iWarn "${BOLD}Shutting down old SnowBlower processes...${NC}" >&2
# $SB_DOCKER_PATH down >/dev/null 2>&1
# isNotRunning
# elif [ -z "$($SB_DOCKER_PATH ps -q "$SB_APP_SERVICE")" ]; then
# isNotRunning
# fi
fi
}
# Various Check functions
function isInsideDocker() {
test -f /.dockerenv
}
function isInsideSnowblowerShell() {
if [ -n "$SB_IN_SHELL" ]; then
return 0
else
return 1
fi
}
function shouldExit() {
local -r exit_code="${1}"
shift
local -r expected="${1}"
shift
if [ "$exit_code" != "${expected:-0}" ]; then
exit "${exit_code}"
fi
}
function isTerminal() {
isTty || isRedirect || isPipe || isSsh
}
function isTty() {
[[ -t 1 ]]
}
function isRedirect() {
[[ ! -t 1 && ! -p /dev/stdout ]]
}
function isPipe() {
[[ -p /dev/stdout ]]
}
function isSsh() {
[[ -n ${SSH_CLIENT} || -n ${SSH_CONNECTION} ]]
}
function checkLastCommand() {
local -r exit_code=$?
local -r success_message="${1}"
local -r fail_message="${2}"
local -r should_restore="${3}"
if [ $exit_code -eq 0 ]; then
if [ -n "$should_restore" ]; then
cursorRestore
_iClear
fi
if [ -n "$success_message" ]; then
_iOk "$success_message"
fi
else
if [ -n "$fail_message" ]; then
_iError "$fail_message"
fi
exit $exit_code
fi
}
# Run a command that should never fail. If the command fails execution
# will immediately terminate with an error showing the failing
# command.
ensure() {
if ! "$@"; then _iFail "command failed: $*"; fi
}
# Credits to https://github.com/nix-community/home-manager/blob/master/lib/bash/home-manager.sh
# The setup respects the `NO_COLOR` environment variable.
function doSetupColors() {
BOLD=""
DIM=""
UNDERLINE=""
BLINK=""
REVERSE=""
NC=""
BLACK=""
RED=""
GREEN=""
YELLOW=""
BLUE=""
MAGENTA=""
CYAN=""
WHITE=""
# Enable colors for terminals, and allow opting out.
if [[ ! -v NO_COLOR ]] && ([[ -t 1 ]] || [[ -n $TERM ]] || [[ -n $COLORTERM ]]); then
# See if it supports colors.
local ncolors
ncolors=$(tput colors 2>/dev/null || echo 0)
if [[ -n $ncolors && $ncolors -ge 8 ]]; then
# Text attributes
BOLD="$(tput bold)"
DIM="$(tput dim)"
UNDERLINE="$(tput smul)"
BLINK="$(tput blink)"
REVERSE="$(tput rev)"
NC="$(tput sgr0)" # No Color
# Regular colors
BLACK="$(tput setaf 0)"
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
BLUE="$(tput setaf 4)"
MAGENTA="$(tput setaf 5)"
CYAN="$(tput setaf 6)"
WHITE="$(tput setaf 7)"
fi
fi
}
# Credits: https://github.com/srid/flake-root/blob/master/flake-module.nix
# This function is used to find the flake root and set it as a env varible.
findUp() {
ancestors=()
while true; do
if [[ -f $1 ]]; then
echo "$PWD"
exit 0
fi
ancestors+=("$PWD")
if [[ $PWD == / ]] || [[ $PWD == // ]]; then
echo "Unable to locate ${1}"
exit 1
fi
cd ..
done
}
getFileMd5Hash() {
if [ -f "$1" ]; then
echo $(echo "$1") | md5sum | cut -d' ' -f1
return 0
else
return 1
fi
}
# Function to ask user for confirmation before proceeding
function confirmAction {
local message="$1"
local response
echo -n "${message} (y/N): "
read -r response
case "$response" in
[yY] | [yY][eE][sS])
return 0
;;
*)
echo "Operation cancelled."
exit 0
;;
esac
}
expand_vars() {
local arg="$1"
# Use eval to expand variables, but escape special characters first
eval "echo \"$arg\""
}
# From: https://github.com/kigster/bashmatic/blob/main/lib/output.sh#L94
export LEFT_PREFIX=" "
function cursorSave() {
isTerminal && printf "\e[s"
}
function cursorRestore() {
isTerminal && printf "\e[u"
}
function cursorHide() {
isTerminal && printf "\033[?25l"
}
function cursorShow() {
isTerminal && printf "\033[?25h"
}
function cursorRightBy() {
isTerminal && printf "\e[${1:-"1"}C"
}
function cursorLeftBy() {
isTerminal && printf "\e[${1:-"1"}D"
}
function cursorUpBy() {
isTerminal && printf "\e[${1:-"1"}A"
}
function cursorDownBy() {
isTerminal && printf "\e[${1:-"1"}B"
}
function cursorUp() {
cursorUpBy "$@"
}
function cursorDown() {
cursorDownBy "$@"
}
function _inlineFlake() {
printf "${CYAN} ❆ ${NC}"
}
function _inlineHeart() {
printf "${RED} ❤ ${NC}"
}
function _inlineCheck() {
printf "${GREEN} ✔︎ ${NC}"
}
function _inlineNotOk() {
inlineError
}
function _inlineCross() {
printf "${RED} ✘ ${NC}"
}
function _inlineWarning() {
printf "${YELLOW} ✱ ${NC}"
}
function _inlineNote() {
printf " ${NC}"
}
function _iBreak() {
echo "${NC}"
}
function _iClear() {
printf "\033[0J"
}
function _i() {
local msgid="$1"
shift
printf "${LEFT_PREFIX}${msgid}\n" "$@"
}
function _iSnow() {
_i "$@"
cursorUp 1
_inlineFlake
echo "${NC}"
}
function _iHeart() {
_i "$@"
cursorUp 1
_inlineHeart
echo "${NC}"
}
function _iOk() {
_i "$@"
cursorUp 1
_inlineCheck
echo "${NC}"
}
function _iNotOk() {
_i "$@"
cursorUp 1
_inlineCross
echo "${NC}"
}
function _iError() {
echo -n "${RED}"
_i "$@"
echo -n "${NC}"
}
function _iFail() {
_iError "$@" >&2
exit 1
}
function _iWarn() {
echo -n "${YELLOW}"
_i "$@"
echo -n "${NC}"
}
function _iNote() {
echo -n "${BLUE}"
_i "$@"
echo -n "${NC}"
}
function _iVerbose() {
if [[ -v VERBOSE ]]; then
_i "$@"
fi
}
function doSnowBash {
doRoutedCommandExecute tools zsh
exit 0
}
function doSetRoot() {
# We need to find our project root early on so downstream options can use it.
SB_WORKSPACE_ROOT="$(findUp $SB_WORKSPACE_ROOT_FILE)"
if [ $? -ne 0 ]; then
_iError "Unable to locate project root. Make sure you're in a project directory with a flake.nix file"
exit 1
fi
_iVerbose "Found project root at %s" $SB_WORKSPACE_ROOT
export SB_WORKSPACE_ROOT
}
function doSetProjectHash() {
local base_dir_name
name=$(basename "$SB_WORKSPACE_ROOT")
local hash
hash=$(echo -n "$SB_WORKSPACE_ROOT" | md5sum | awk '{print $1}')
export SB_PROJECT_HASH="${name}-r${hash}"
_iVerbose "Set project hash as $SB_PROJECT_HASH"
}
doCheckSystem() {
UNAMEOUT="$(uname -s)"
# Verify operating system is supported...
case "${UNAMEOUT}" in
Linux*) MACHINE=linux ;;
Darwin*) MACHINE=mac ;;
*) MACHINE="UNKNOWN" ;;
esac
if [ "$MACHINE" == "UNKNOWN" ]; then
_iError "Unsupported operating system [$(uname -s)]." "SnowBlower supports macOS, Linux, and Windows (WSL2)." >&2
exit 1
fi
}
function doBoot() {
doSetupColors
# Welcome Message
_iSnow "SnowBlower: All flake no fluff."
doCheckSystem
doSetupSession
doSetRoot
doSetProjectHash
# Check if our Datapath exsist in the Home Snowblower directory.
if [ ! -d "$HOME" ]; then
_iError "Unable to locate home folder. Please create or export one to continue."
exit 1
fi
export SB_PROJECT_ROOT="$HOME/snowblower/$SB_PROJECT_HASH"
export SB_PROJECT_ENV_FILE="$SB_PROJECT_ROOT/.project_env"
if [ -f "$SB_PROJECT_ENV_FILE" ]; then
_iVerbose "SnowBlower Project Directory and paths file exsist skipping init."
source "$SB_PROJECT_ENV_FILE"
return 0
fi
doInit
}
function doInit() {
if [ ! -d "$SB_PROJECT_ROOT" ]; then
_iOk "Initilizing Project at %s." $SB_PROJECT_ROOT
mkdir -p "$SB_PROJECT_ROOT/profile"
mkdir -p "$SB_PROJECT_ROOT/state"
_iVerbose "Created profile and state directories."
fi
# Check if Docker is installed
if SB_DOCKER_PATH=$(command -v docker 2>/dev/null); then
_iOk "Docker path set to %s" "${SB_DOCKER_PATH}"
else
_iError "Docker is not installed or not in PATH. Please install Docker to continue."
exit 1
fi
# All Checks passed we can now create the .project_env file so we dont have to do this every time.
echo "export SB_DOCKER_PATH=\"$SB_DOCKER_PATH\"" >>"$SB_PROJECT_ENV_FILE"
SB_PROJECT_PROFILE="$SB_PROJECT_ROOT/profile"
echo "export SB_PROJECT_PROFILE=\"$SB_PROJECT_PROFILE\"" >>"$SB_PROJECT_ENV_FILE"
SB_PROJECT_STATE="$SB_PROJECT_ROOT/state"
echo "export SB_PROJECT_STATE=\"$SB_PROJECT_STATE\"" >>"$SB_PROJECT_ENV_FILE"
if ! isSnowBlowerNixVolumeCreated; then
_iOk "Creating snowblower-nix Docker Volume."
$SB_DOCKER_PATH volume create snowblower-nix
fi
return 0
}
function doSetupSession() {
# Source the ".env" file so environment variables are available...
# shellcheck source=/dev/null
if [ -n "${APP_ENV+x}" ] && [ -n "$APP_ENV" ] && [ -f ./.env."$APP_ENV" ]; then
source ./.env."$APP_ENV"
_iNote "Found and sourced %s" ".env.{$APP_ENV}"
elif [ -f ./.env ]; then
source ./.env
_iNote "Found and sourced %s" ".env"
fi
# Global constants that are used in many parts of the script
export SB_APP_SERVICE=${APP_SERVICE:-"tools"}
export SB_USER_UID="$(id -u)"
export SB_USER_GID="$(id -g)"
export SB_WORKSPACE_ROOT_FILE=${PROJECT_ROOT_FILE:-"flake.nix"}
if isTerminal; then
export SB_TERM_TYPE="terminal"
else
export SB_TERM_TYPE="dumb"
fi
_iVerbose "Set project root file to %s" "${SB_WORKSPACE_ROOT_FILE}"
}
function doSnowBuildImagesLogic() {
_iSnow "Rebuilding Images"
_iNote "Pruning Old Images"
ensure runDocker image prune -f --filter "label=org.snowblower.project=$SB_PROJECT_HASH"
_iOk "Pruned Images"
rm -f "$SB_PROJECT_ROOT/result"
_iNote "Building Runtime Docker Image"
ensure runBuilder nix build --impure --out-link "/snowblower/result" .#dockerRuntimeImagePackage
_iOk "Built Runtime Docker Image"
_iNote "Loading Runtime Docker Image"
ensure runBuilder bash -c "/snowblower/result | sudo docker load"
_iOk "Loaded Runtime Docker Image"
rm -f "$SB_PROJECT_ROOT/result"
_iNote "Building Tooling Docker Image"
ensure runBuilder nix build --impure --out-link "/snowblower/result" .#dockerToolsImagePackage
_iOk "Built Tooling Docker Image"
_iNote "Loading Tooling Docker Image"
ensure runBuilder bash -c "/snowblower/result | sudo docker load"
_iOk "Loaded Tooling Docker Image"
rm -f "$SB_PROJECT_ROOT/result"
_iHeart "Build Complete"
return 0
}
function doSnowBuildFilesLogic() {
runBuilder bash -c "nix run --impure .#snowblowerFiles -L"
return 0
}
function doSnowBuild() {
local build_files=false
local build_images=false
# If no arguments are passed, run both
if [[ $# -eq 0 ]]; then
build_files=true
build_images=true
else
# Check arguments
for arg in "$@"; do
if [[ $arg == "-f" || $arg == "--files" ]]; then
build_files=true
elif [[ $arg == "-i" || $arg == "--images" ]]; then
build_images=true
fi
done
# If no recognized arguments, default to both
if [[ $build_files == false && $build_images == false ]]; then
build_files=true
build_images=true
fi
fi
# Always build files first if requested
if [[ $build_files == true ]]; then
doSnowBuildFilesLogic
fi
# Then build images if requested
if [[ $build_images == true ]]; then
doSnowBuildImagesLogic
fi
exit 0
}
# Function that outputs SnowBlower is not running...
function isNotRunning {
echo
_iFail "Environment is not running." >&2
_iNote "To start run %s" "snow up"
exit 1
}
# Figures out the type of envirment the command is running in and then routes approriatly.
function doRoutedCommandExecute() {
_iVerbose "Attempting to run: $*"
# Save the first argument (environment type) and shift it
local env_type="$1"
shift
case "$env_type" in
native)
_iVerbose "Executing command natively"
exec "$@"
return $?
;;
tools)
doRunChecks
ARGS=()
ARGS+=(exec -u "$SB_USER_UID")
[ ! -t 0 ] && ARGS+=(-T)
ARGS+=("$env_type")
_iVerbose "Executing command via docker compose in %s service" $env_type
# Execute the command with proper shell evaluation
runDockerCompose "${ARGS[@]}" "with-snowblower" "$@"
return $?
;;
service)
doRunChecks
# Extract the service name from the first argument
local service_name="$1"
shift
_iVerbose "Executing command via docker compose run in %s service" "$service_name"
# Execute the command using docker compose run
runDockerCompose run --rm "$service_name" "$@"
return $?
;;
*)
_iFail "Unknown environment type: $env_type" >&2
return 1
;;
esac
}
function doSnowDev {
doRoutedCommandExecute tools zsh
exit 0
}
function doSnowDownLogic() {
# Check if snowblower is running
if ! isSnowBlowerDockerContainerUp; then
_iError "SnowBlower is already down."
return 1
fi
# Execute down hooks
doHook__up__post
# If it's running, run docker compose down and wait for it to finish then run doDestroySession
_iOk "Stopping SnowBlower services..."
runDockerCompose --profile auto-start down --remove-orphans
_iHeart "SnowBlower has been stopped."
return 0
}
function doSnowDown {
doSnowDownLogic
exit 0
}
function doSnowPsLogic() {
# Check if snowblower is running
if ! isSnowBlowerDockerContainerUp; then
_iError "SnowBlower is already down."
return 1
fi
runDockerCompose ps
return 0
}
function doSnowPs {
doSnowPsLogic
exit 0
}
function runNix() {
if [[ $BUILD == "1" ]]; then
run_args+=(--build)
fi
run_args+=(--rm builder)
$SB_DOCKER_PATH compose -f "$SB_WORKSPACE_ROOT/docker-compose.yml" run "${run_args[@]}" "$@"
}
function runBuilder() {
local run_args=()
if [[ $BUILD == "1" ]]; then
run_args+=(--build)
fi
run_args+=(--rm builder)
$SB_DOCKER_PATH compose -f "$SB_WORKSPACE_ROOT/docker-compose.yml" run "${run_args[@]}" "$@"
}
function runDockerCompose() {
local compose_args=()
if [[ $BUILD == "1" ]]; then
compose_args+=(--build)
fi
compose_args+=(-f "$SB_WORKSPACE_ROOT/docker-compose.yml")
$SB_DOCKER_PATH compose "${compose_args[@]}" "$@"
}
function runDocker() {
$SB_DOCKER_PATH "$@"
}
function doSnowSwitch() {
# Check if snowblower is running
if isSnowBlowerDockerContainerUp; then
confirmAction "A SnowBlower Docker instance is running. This action will shut down the current instance. Continue?"
# If it's running, run docker compose down and wait for it to finish.
_iOk "Stopping SnowBlower services..."
doSnowDownLogic
fi
doSnowBuildImagesLogic
_iNote "Running file generator..."
doSnowBuildFilesLogic
_iHeart "Switch Complete"
exit 0
}
function doSnowUpLogic() {
# Execute pre-up hooks
doHook__up__pre
# Check if SnowBlower docker container is already running
if isSnowBlowerDockerContainerUp; then
_iError "SnowBlower container is already running. Use 'snow down' to stop it first."
return 1
fi
# Start docker-compose detached
runDockerCompose --profile auto-start up --detach --remove-orphans "$@"
doRoutedCommandExecute "tools" "snowblower-hooks" "tools_pre"
return 0
}
function doSnowUp {
doSnowUpWithMenu "$@"
}
function doSnowUpWithMenu() {
doSnowUpLogic "$@"
if [ $? -eq 1 ]; then
exit 1
fi
# Clear screen and show persistent menu with logs
while true; do
clear
echo "${YELLOW}SnowBlower Menu:${NC}"
echo " ${GREEN}q${NC} - Quit and stop containers"
echo
echo "${YELLOW}Logs (last 20 lines):${NC}"
echo "────────────────────────────────────────"
# Show last 20 lines of logs
runDockerCompose logs --tail=20 2>/dev/null || echo "No logs available yet..."
echo "────────────────────────────────────────"
echo -n "Press 'q' to quit or any other key to refresh: "
# Read single character without timeout
read -n 1 choice
case "$choice" in
q | Q)
echo
echo "Shutting down..."
doRoutedCommandExecute "tools" "snowblower-hooks" "tools_post"
doSnowDownLogic
exit 0
;;
*)
# Any other key refreshes the display
continue
;;
esac
done
}
function doSnowUpdate {
runNix flake update
doSnowSwitch
exit 0
}
function doHook__up__pre {
echo -n
# Set local git config from global if global exists and local doesn't
if git config --global user.name >/dev/null 2>&1 && ! git config user.name >/dev/null 2>&1; then
git config user.name "$(git config --global user.name)"
_iNote "Set local git user.name to: $(git config user.name)"
fi
if git config --global user.email >/dev/null 2>&1 && ! git config user.email >/dev/null 2>&1; then
git config user.email "$(git config --global user.email)"
_iNote "Set local git user.email to: $(git config user.email)"
fi
}
function doHook__up__post {
echo -n
}
function doHelp {
echo "${YELLOW}Usage:${NC}" >&2
echo " snow [options] COMMAND SUBCOMMAND [arguments]"
echo
echo
exit 1
}
doSnow() {
PASSTHROUGH_OPTS=()
SNOW_COMMAND=""
SNOW_COMMAND_ARGS=()
COMMAND_ARGS=()
# Split arguments at the `--` separator
BEFORE_SEPARATOR=()
AFTER_SEPARATOR=()
# Used here to split the function in to anything before '--' and after.
FOUND_SEPARATOR=false
for arg in "$@"; do
if [[ $arg == "--" ]]; then
FOUND_SEPARATOR=true
continue
fi
if [[ $arg == "---" ]]; then
FOUND_SEPARATOR=true
AFTER_SEPARATOR+=("just")
continue
fi
if [[ $FOUND_SEPARATOR == true ]]; then
AFTER_SEPARATOR+=("$arg")
else
BEFORE_SEPARATOR+=("$arg")
fi
done
# Process snow-specific options and commands before `--`
i=0
while [[ $i -lt ${#BEFORE_SEPARATOR[@]} ]]; do
opt="${BEFORE_SEPARATOR[$i]}"
i=$((i + 1))
case $opt in
-v | --verbose)
export VERBOSE=1
;;
-h | --help)
export HELP=1
;;
-b | --build)
export BUILD=1
;;
--version)
echo 25.11-pre
exit 0
;;
switch | up | down | update | reboot | build | ps | bash | dev | help)
SNOW_COMMAND="$opt"
;;
*)
case $SNOW_COMMAND in
switch | update | reboot | up | down | build | ps | bash | dev | help)
SNOW_COMMAND_ARGS+=("$opt")
;;
*)
if [[ -z $SNOW_COMMAND ]]; then
_iError "%s: unknown snow option '%s'. Use format: snow [options] -- command [args]" "$0" "$opt" >&2
exit 1
else
SNOW_COMMAND_ARGS+=("$opt")
fi
;;
esac
;;
esac
done
# Lets GOOOO!!!!!
doBoot
# Check if no `--` was found and no snow command was specified
if [[ $found_separator == false && -z $SNOW_COMMAND ]]; then
doHelp >&2
exit 1
fi
# Handle snow-specific commands first
if [[ -n $SNOW_COMMAND ]]; then
case $SNOW_COMMAND in
bash)
doSnowBash "${SNOW_COMMAND_ARGS[@]}"
;;
build)
doSnowBuild "${SNOW_COMMAND_ARGS[@]}"
;;
dev)
doSnowDev "${SNOW_COMMAND_ARGS[@]}"
;;
down)
doSnowDown "${SNOW_COMMAND_ARGS[@]}"
;;
ps)
doSnowPs "${SNOW_COMMAND_ARGS[@]}"
;;
reboot)
doSnowReboot "${SNOW_COMMAND_ARGS[@]}"
;;
switch)
doSnowSwitch "${SNOW_COMMAND_ARGS[@]}"
;;
up)
doSnowUp "${SNOW_COMMAND_ARGS[@]}"
;;
update)
doSnowUpdate "${SNOW_COMMAND_ARGS[@]}"
;;
help)
doHelp "${SNOW_COMMAND_ARGS[@]}"
;;
*)
_iError 'Unknown snow command: %s' "$SNOW_COMMAND" >&2
exit 1
;;
esac
exit 0
fi
# None of the below should be run if SnowBlower is not up.
isSnowBlowerUp
doRoutedCommandExecute tools "${AFTER_SEPARATOR[@]}"
}
doSnow "$@" || exit 1