-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpls
More file actions
executable file
Β·1100 lines (904 loc) Β· 31.4 KB
/
pls
File metadata and controls
executable file
Β·1100 lines (904 loc) Β· 31.4 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
# This script was generated by bashly 1.2.1 (https://bashly.dannyb.co)
# Modifying it manually is not recommended
# :wrapper.bash3_bouncer
if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
printf "bash version 4 or higher is required\n" >&2
exit 1
fi
# :command.master_script
# :command.root_command
root_command() {
# src/root_command.sh
alias="${args[alias]}"
flag_add="${args[--add]}"
flag_command="${args[--command]}"
flag_target="${args[--target]}"
flag_delete="${args[--delete]}"
flag_list="${args[--list]}"
flag_clear_cache="${args[--clear-cache]}"
flag_this_does_nothing="${args[--this-does-nothing]}"
if [ -n "$flag_this_does_nothing" ]; then
exit
fi
if [ -n "$flag_clear_cache" ]; then
rm -f "$PLS_DIR/.cache.yml"
if [ ${#args[@]} -eq 1 ]; then
exit
fi
fi
if [ -n "$flag_delete" ]; then
delete_alias
exit
fi
if [ -n "$flag_add" ]; then
add_alias
exit
fi
if [ -n "$flag_list" ]; then
list_aliases 0
exit
fi
if [ -z "$alias" ]; then
pick_and_execute_alias
else
execute_alias "$alias"
fi
}
# :command.version_command
version_command() {
echo "$version"
}
# :command.usage
pls_usage() {
if [[ -n $long_usage ]]; then
printf "pls\n\n"
printf " Project Level Shortcuts (pls) is a command-line tool designed to streamline \n your workflow by allowing you to create, manage, and execute custom aliases \n for frequently used commands.\n \n Key features:\n * Easy alias creation and modification\n Add, remove, rename, or change functionality of aliases with a simple\n CLI interface.\n * Project Level Specificity \n Aliases can be defined at both project and global levels, with\n project-specific aliases taking precedence.\n * Intuitive command execution:\n Run a command associated with an alias using the 'pls' prefix.\n e.g. pls open, pls test, pls deploy\n * Easily shareable aliases:\n Streamline your team's workflow by sharing aliases across projects, or\n make setup of your product dead-easy for your users.\n Just add a pls file to the project's root directory, and you're off!\n \n To see examples of how to use pls, run 'pls <command> --help'\n\n"
else
printf "pls - Project Level Shortcuts (pls) is a command-line tool designed to streamline \n\n"
fi
printf "%s\n" "Usage:"
printf " pls [ALIAS] [COMMAND_ARGS...] [OPTIONS]\n"
printf " pls --help | -h\n"
printf " pls --version | -v\n"
echo
# :command.long_usage
if [[ -n $long_usage ]]; then
printf "%s\n" "Options:"
# :command.usage_flags
# :flag.usage
printf " %s\n" "--add, -a"
printf " OPERATION: Adds a new alias for a --command to the pls file at a given\n --scope.\n"
printf " Needs: --command, --scope\n"
echo
# :flag.usage
printf " %s\n" "--delete, -d"
printf " OPERATION: Delete the alias from the pls file at given --scope.\n"
printf " Needs: --scope\n"
echo
# :flag.usage
printf " %s\n" "--list, -l"
printf " OPERATION: List all aliases available in the current directory.\n Or, list aliases only from --scope <[g]lobal, [l]ocal, [a]ll>.\n Use with --print to print the command associated with each alias.\n"
echo
# :flag.usage
printf " %s\n" "--command, -c COMMAND"
printf " ARGFLAG: Supplies a command to associate with the alias in the --add\n operation.\n Pass the command as a string, newline characters ('\\\n') are allowed.\n"
echo
# :flag.usage
printf " %s\n" "--scope, -s SCOPE"
printf " ARGFLAG: The selected scope for --add, --delete, and --list operations.\n The 'all' scope is exclusive to --list.\n The 'here' scope is excluded from --list.\n"
printf " Allowed: g, global, l, local, h, here, a, all\n"
echo
# :flag.usage
printf " %s\n" "--print, -p"
printf " Prints the command instead of executing it. Useful for piping to other\n commands or shells.\n Or, if used in conjunction with --list, it additionally prints the command\n for each alias.\n"
echo
# :flag.usage
printf " %s\n" "--force, -f"
printf " Overwrite the alias if it already exists when using the --add operation.\n"
echo
# :flag.usage
printf " %s\n" "--clear-cache"
printf " Before doing anything, clears the cache of all commands that have been run\n with pls.\n"
echo
# :command.usage_fixed_flags
printf " %s\n" "--help, -h"
printf " Show this help\n"
echo
printf " %s\n" "--version, -v"
printf " Show version number\n"
echo
# :command.usage_args
printf "%s\n" "Arguments:"
# :argument.usage
printf " %s\n" "ALIAS"
printf " The alias to execute, add, or delete.\n When left empty, will launch an interactive picker to select and execute an\n alias.\n"
echo
# :argument.usage
printf " %s\n" "COMMAND_ARGS..."
printf " Optional arguments to pass to the command when executing a parameterized\n command.\n"
echo
# :command.usage_environment_variables
printf "%s\n" "Environment Variables:"
# :environment_variable.usage
printf " %s\n" "PLS_RC"
printf " The path to the configuration file to use. Contains additional environment\n variables for advanced users. Default is determined by\n '\$HOME/.config/pls/.plsrc'.\n"
printf " Default: $HOME/.config/pls/.plsrc\n"
echo
fi
}
# :command.normalize_input
# :command.normalize_input_function
normalize_input() {
local arg passthru flags
passthru=false
while [[ $# -gt 0 ]]; do
arg="$1"
if [[ $passthru == true ]]; then
input+=("$arg")
elif [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
input+=("${BASH_REMATCH[1]}")
input+=("${BASH_REMATCH[2]}")
elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
input+=("${BASH_REMATCH[1]}")
input+=("${BASH_REMATCH[2]}")
elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
flags="${BASH_REMATCH[1]}"
for ((i = 0; i < ${#flags}; i++)); do
input+=("-${flags:i:1}")
done
elif [[ "$arg" == "--" ]]; then
passthru=true
input+=("$arg")
else
input+=("$arg")
fi
shift
done
}
# :command.inspect_args
inspect_args() {
if ((${#args[@]})); then
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
echo args:
for k in "${sorted_keys[@]}"; do
echo "- \${args[$k]} = ${args[$k]}"
done
else
echo args: none
fi
if ((${#other_args[@]})); then
echo
echo other_args:
echo "- \${other_args[*]} = ${other_args[*]}"
for i in "${!other_args[@]}"; do
echo "- \${other_args[$i]} = ${other_args[$i]}"
done
fi
if ((${#deps[@]})); then
readarray -t sorted_keys < <(printf '%s\n' "${!deps[@]}" | sort)
echo
echo deps:
for k in "${sorted_keys[@]}"; do
echo "- \${deps[$k]} = ${deps[$k]}"
done
fi
if ((${#env_var_names[@]})); then
readarray -t sorted_names < <(printf '%s\n' "${env_var_names[@]}" | sort)
echo
echo "environment variables:"
for k in "${sorted_names[@]}"; do
echo "- \$$k = ${!k:-}"
done
fi
}
# :command.user_lib
# src/lib/cache/add_to_cache.sh
add_to_cache() {
local origin="$1" # Is a file path, so can contain special characters such as whitespace
local alias="$2" # Is a string which cannot be multiline
local command="$3" # Is a string which can be multiline
local cache_file="$PLS_DIR/.cache.yml"
# Check if all arguments are provided
if [ -z "$origin" ] || [ -z "$alias" ] || [ -z "$command" ]; then
echo "Usage Error: add_to_cache <origin> <alias> <command>" >&2
return 1
fi
# Create cache if it does not exist
if [[ ! -d "$PLS_DIR" ]]; then
echo "Env Error: PLS_DIR is not set or is not a directory" >&2
return 1
fi
[[ ! -f "$cache_file" ]] && touch "$cache_file"
local escaped_command=$(printf '%s' "$command" | sed 's/"/\\"/g')
yq -i eval ".[\"$origin\"][\"$alias\"] = \"$escaped_command\"" "$cache_file"
}
# src/lib/cache/exists_in_cache.sh
exists_in_cache() {
local origin="$1"
local alias="$2"
local command="$3"
if [[ -z "$origin" || -z "$alias" || -z "$command" ]]; then
echo "Usage Error: exists_in_cache <origin> <alias> <command>" >&2
return 1
fi
if [ ! -d "$PLS_DIR" ]; then
echo "Directory not found at $PLS_DIR" >&2
return 1
fi
if [ ! -f "$PLS_DIR/.cache.yml" ]; then
return 1
fi
local result=$(yq eval ".[\"$origin\"][\"$alias\"]" "$PLS_DIR/.cache.yml")
if [[ "$result" == "null" ]]; then
return 1
fi
if [[ ! "$(printf "$command")" == "$result" ]]; then
return 1
fi
}
# src/lib/helpers/check_version.sh
check_version() {
local min_version="$1"
local actual_version="$2"
if [[ "$(printf '%s\n%s' "$min_version" "$actual_version" | sort -V | head -n1)" != "$min_version" ]]; then
return 1
fi
}
# src/lib/helpers/get_closest_file.sh
get_closest_file() {
local from_dir="$1"
local target_file="$2"
local mock_root="$3"
# Traverse up the directory tree until we find the target file
# or reach the root directory.
# If we reach the root directory, return an empty string.
local current_dir="$(realpath $from_dir)"
while [ "$current_dir" != "/" ]; do
local file_path="$current_dir/$target_file"
if [[ -f "$file_path" && "$file_path" != "$PLS_GLOBAL" ]]; then
# Echo the path of the file
# (If the current directory is the mock root,
# return path as if it were the root directory)
echo "$([[ "$current_dir" == "$mock_root" ]] && \
echo "/$target_file" || echo "$current_dir/$target_file"
)"
return 0
fi
current_dir=$(dirname $(realpath "$current_dir"))
done
if [[ -f "/$target_file" ]]; then
echo "/$target_file" && return 0
fi
}
# src/lib/helpers/merge_pls_files.sh
merge_pls_files() {
local global_file="$1"
local local_file="$2"
local combined_file="$(mktemp)"
# Start with an empty, valid pls file
echo "commands:" > "$combined_file"
# If both files exist, merge them
if [ -f "$local_file" ] && [ -f "$global_file" ]; then
# Combine into a single yml with nodes 'local' and global' respectively
local temp_file="$(mktemp)"
sed "s/commands:/local:/" "$local_file" > "$temp_file"
sed "s/commands:/global:/" "$global_file" >> "$temp_file"
# Add a (*) to all aliases that are in the global: section
yq e -i '.commands = ((.global | map(.alias = .alias + " *")) + .local) | del(.global,.local)' "$temp_file"
# Combine the two sections into a single section
# Conflicts are resolved by taking the alias that does not have a * at the end
yq eval '
.commands |= (
map(select(.alias | test(" \\*$") | not)) +
map(select(.alias | test(" \\*$"))) |
unique_by(.alias | sub(" \\*$"; "")) |
sort_by(.alias)
)' "$temp_file" > "$combined_file"
elif [ -f "$local_file" ]; then
# If the file has no entries, we are already done
if [[ "$(yq e '.commands | length' "$local_file")" -eq 0 ]]; then
return
fi
yq eval '.commands | sort_by(.alias)' "$local_file" >> "$combined_file"
elif [ -f "$global_file" ]; then
# If the file has no entries, we are already done
if [[ "$(yq e '.commands | length' "$global_file")" -eq 0 ]]; then
return
fi
# Else use the global file and add a (*) to all aliases
yq eval '.commands | map(.alias = .alias + " *") | sort_by(.alias)' "$global_file" >> "$combined_file"
fi
cat "$combined_file"
rm "$combined_file"
}
# src/lib/helpers/scope_to_path.sh
scope_to_path() {
local scope="$1"
# If the target is "g" or "global", return the global destination
if [ "$scope" == "g" ] || [ "$scope" == "global" ]; then
echo "$PLS_GLOBAL"
return 0
# If the target is "l" or "local", return the closest file
elif [ "$scope" == "l" ] || [ "$scope" == "local" ]; then
local closest_file="$(get_closest_file "$PWD" "$PLS_FILENAME")"
echo "$closest_file"
return 0
# Return target in the current directory
elif [ "$scope" == "h" ] || [ "$scope" == "here" ]; then
echo "$(realpath $PWD/$PLS_FILENAME)"
return 0
fi
echo "Error: Invalid argument. Must be one of [ g, global, l, local, h, here ]" >&2
return 1
}
# src/lib/query/add_entry_to_file.sh
add_entry_to_file() {
local alias="$1"
local command="$2"
local target_file="$3"
if [[ -z "$alias" || -z "$command" || -z "$target_file" ]]; then
echo "Usage Error: add_entry_to_file <alias> <command> <file>" >&2
return 1
fi
if [ ! -f "$target_file" ]; then
echo "Error: Can not add to file '$target_file' as it does not exist." >&2
return 1
fi
yq e -i ".commands += [{\"alias\": \"$alias\", \"command\": \"$command\"}]" "$target_file"
}
# src/lib/query/delete_entry_from_file.sh
delete_entry_from_file() {
local alias="$1"
local file="$2"
if [[ -z "$alias" || -z "$file" ]]; then
echo "Usage Error: delete_entry_from_file <alias> <file>" >&2
return 1
fi
if [ ! -f "$file" ]; then
echo "Error: Can not delete from file '$file' as it does not exist." >&2
return 1
fi
yq e -i "del(.commands[] | select(.alias == \"$alias\"))" "$file"
}
# src/lib/query/query_command.sh
query_command() {
local alias="$1"
local report_found_in="$2" || 0
if [ -z "$alias" ]; then
echo "Usage Error: Alias cannot be empty." >&2
return 1
fi
local global_file="$PLS_GLOBAL"
local local_file="$(get_closest_file "$PWD" "$PLS_FILENAME")"
local command
local found_in
if [ -f "$global_file" ]; then
local result=$(query_command_in_file "$alias" "$global_file")
if [ -n "$result" ]; then
command="$result"
found_in="$global_file"
fi
fi
if [ -f "$local_file" ]; then
local result=$(query_command_in_file "$alias" "$local_file")
if [ -n "$result" ]; then
command="$result"
found_in="$local_file"
fi
fi
echo "$command"
if [ $report_found_in ]; then
echo "$found_in"
fi
}
# src/lib/query/query_command_in_file.sh
query_command_in_file() {
local alias="$1"
local file="$2"
if [ -z "$alias" ]; then
echo "Usage Error: Alias cannot be empty." >&2
return 1
fi
if [ ! -f "$file" ]; then
echo "Usage Error: '$file' does not exist." >&2
return 1
fi
local command="$(yq ".commands | map(select(.alias == \"$alias\"))[0] | .command" "$file")"
[[ $command == "null" ]] && echo "" || echo "$command"
}
# src/lib/validations/validate_format_of_files.sh
validate_format_of_file() {
local file="$1"
# # Check if 'commands' is the only top-level node
local toplevel_nodes_count="$(yq 'length' "$file")"
local commands_nodes_count
if [[ "$(yq '. == null' "$file")" == "true" ]]; then
commands_nodes_count=0
else
commands_nodes_count="$(yq 'keys | map(select(. == "commands")) | length' "$file")"
fi
if [[ "$toplevel_nodes_count" -gt 1 || "$commands_nodes_count" -ne 1 ]]; then
echo "Format Error: '$file' should have 'commands' as the (only) top-level node." >&2
echo "It currently has $toplevel_nodes_count top-level nodes." >&2
echo "$commands_nodes_count of which are titled 'commands'." >&2
return 1
fi
# Check if 'commands' has an equal number of 'alias' and 'command' keys
local size_of_commands_array="$(yq '.commands | length' "$file")"
local num_of_aliases="$(yq '.commands | map(select(has("alias"))) | length' "$file")"
local num_of_command="$(yq '.commands | map(select(has("command"))) | length' "$file")"
if [[ "$size_of_commands_array" -ne "$num_of_aliases" || "$size_of_commands_array" -ne "$num_of_command" ]]; then
echo "Format Error: '$file' should have 'alias' and 'command' for each command." >&2
echo "It currently has $size_of_commands_array commands." >&2
echo " $num_of_aliases of which have a key 'alias'." >&2
echo " $num_of_command of which have a key 'command'." >&2
return 1
fi
if [[ "$size_of_commands_array" -eq 0 ]]; then
return 0
fi
# Check if all 'alias' have no newlines
local num_of_aliases_with_newlines="$(yq '.commands | map(select(.alias | contains("\n"))) | length' "$file")"
if [[ "$num_of_aliases_with_newlines" -gt 0 ]]; then
echo "Format Error: '$file' should not have newlines in aliases." >&2
echo "It currently has $num_of_aliases_with_newlines aliases with newlines." >&2
return 1
fi
# Check if all 'alias' are unique
local aliases="$(yq '.commands[].alias' "$file")"
local unique_aliases="$(echo "$aliases" | sort | uniq)"
local num_of_unique_aliases="$(echo "$unique_aliases" | wc -l)"
if [[ "$num_of_aliases" -ne "$num_of_unique_aliases" ]]; then
echo "Format Error: '$file' should have unique aliases." >&2
echo "It currently has $num_of_aliases aliases, but there are only $num_of_unique_aliases unique names." >&2
echo "The following aliases are duplicated:" >&2
diff --changed-group-format='%<' --unchanged-group-format='' <(echo "$aliases" | sort) <(echo "$unique_aliases" | sort) >&2
return 1
fi
# Check if all 'command' are not empty
local num_of_empty_commands="$(yq '.commands | map(select(.command | test("^\\s*$"))) | length' "$file")"
if [[ "$num_of_empty_commands" -gt 0 ]]; then
echo "Format Error: '$file' should not have empty 'command' keys." >&2
echo "It currently has $num_of_empty_commands empty 'command' keys." >&2
return 1
fi
}
validate_format_of_files() {
local global_file="$PLS_GLOBAL"
local local_file=$(get_closest_file "$PWD" "$PLS_FILENAME")
if [[ -f "$global_file" ]]; then
validate_format_of_file "$global_file"
fi
if [[ -f "$local_file" ]]; then
validate_format_of_file "$local_file"
fi
}
# src/lib/validations/validate_is_destination.sh
validate_is_destination() {
local arg="$1"
[ "$arg" != "g" ] && [ "$arg" != "l" ] && [ "$arg" != "h" ] && [ "$arg" != "global" ] && [ "$arg" != "local" ] && [ "$arg" != "here" ] && echo "Error: Invalid argument. Must be one of [ g, global, l, local, h, here ]"
}
# src/lib/validations/validate_is_only_operation.sh
validate_is_only_operation() {
flag_add="${args[--add]}"
flag_delete="${args[--delete]}"
flag_list="${args[--list]}"
is_only_operation "$flag_add" "$flag_delete" "$flag_list"
}
is_only_operation() {
flag_add="$1"
flag_delete="$2"
flag_list="$3"
num_operations=0
if [ -n "$flag_add" ]; then
num_operations=$((num_operations + 1))
fi
if [ -n "$flag_delete" ]; then
num_operations=$((num_operations + 1))
fi
if [ -n "$flag_list" ]; then
num_operations=$((num_operations + 1))
fi
if [ "$num_operations" -eq 0 ]; then
echo "No operation flag supplied."
return
fi
if [ "$num_operations" -ne 1 ]; then
echo "Can only use one operation flag (--add, --delete, --list) at a time."
fi
}
# src/lib/validations/validate_not_empty.sh
validate_not_empty() {
[[ -z "$1" ]] && echo "must not be empty"
}
# src/lib/workers/add_alias.sh
add_alias() {
alias="${args[alias]}"
command="${args[--command]}"
scope="${args[--scope]}"
target_file="$(scope_to_path "$scope")"
if [[ -z "$target_file" && ("$scope" == "local" || "$scope" == "l") ]]; then
target_file="$(realpath .)/$PLS_FILENAME"
fi
# Create file if it does not exist
if [ ! -f "$target_file" ]; then
echo "commands:" > "$target_file"
fi
# If alias already exists in file, --force flag must be used
num_occurances_of_alias="$(yq e ".commands | map(select(.alias == \"$alias\")) | length" "$target_file")"
if [[ $num_occurances_of_alias -eq 1 ]]; then
if [ ! "${args[--force]}" ]; then
echo "Error: Alias '$alias' already exists in file '$target_file'. Use --force to overwrite." >&2
exit 1
fi
# Remove the old alias
delete_entry_from_file "$alias" "$target_file"
fi
# Add alias:command entry to file
add_entry_to_file "$alias" "$command" "$target_file"
}
# src/lib/workers/delete_alias.sh
delete_alias() {
alias="${args[alias]}"
scope="${args[--scope]}"
target_file="$(scope_to_path "$scope")"
if [[ -z "$target_file" ]]; then
echo "Error: Can not delete from local file as it does exist here or in any parent." >&2
exit 1
fi
delete_entry_from_file "$alias" "$target_file"
}
# src/lib/workers/execute_alias.sh
execute_alias() {
local alias="${1}"
local result=$(query_command "$alias" 1)
# command is all but last line
local command=$(echo "$result" | head -n -1)
# found_in is the last line
local found_in=$(echo "$result" | tail -n 1)
# If no command found, exit
if [ -z "$command" ]; then
echo "Alias '$alias' was not found."
exit 1
fi
prompt_to_continue() {
if [[ "$is_cached" == "true" ]]; then
echo "This command is already cached."
fi
echo
echo "$command"
echo
read -p "Are you sure you want to invoke the above command? [y/n] " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Command was not invoked."
echo "Exiting..."
exit 1
fi
}
if [ "${args[--print]}" ]; then
echo "$command"
return
fi
if exists_in_cache "$found_in" "$alias" "$command"; then
if [[ "$PLS_ALWAYS_VERIFY" == "true" ]]; then
prompt_to_continue
fi
elif [[ "$PLS_ENABLE_CACHE_CHECK" == "true" || "$PLS_ALWAYS_VERIFY" == "true" ]]; then
echo "Alias '$alias' was found in '$found_in', but this command seems new."
prompt_to_continue
fi
command_args="${args[command_args]}"
# Create a temporary function
exec_alias_command() {
eval "$command"
}
# Execute the function with the arguments
exec_alias_command $command_args
add_to_cache "$found_in" "$alias" "$command"
}
# src/lib/workers/list_aliases.sh
list_aliases() {
local verbose="${args[--print]}"
local flag_scope="${args[--scope]}"
local local_file="$(get_closest_file "$PWD" "$PLS_FILENAME")"
local global_file="$PLS_GLOBAL"
print_aliases() {
local file="$1"
local should_print_command="$2"
if [[ ! -f "$file" || "$(yq e '.commands | length' "$file")" -eq 0 ]]; then
echo "No aliases found."
return
fi
local alias_query='.commands | sort_by(.alias) | .[] | .alias'
local command_query='.commands | sort_by(.alias) | .[] | .alias + "\n" + (.command | split("\n") | map(select(. != "") | " " + .) | join("\n"))'
yq e "$([[ "$should_print_command" ]] && echo "$command_query" || echo "$alias_query")" "$file"
}
if [ "$flag_scope" == "a" ] || [ "$flag_scope" == "all" ]; then
echo "--- Global Aliases ---"
print_aliases "$global_file" "$verbose"
echo "--- Local Aliases ---"
print_aliases "$local_file" "$verbose"
elif [ "$flag_scope" == "g" ] || [ "$flag_scope" == "global" ]; then
print_aliases "$global_file" "$verbose"
elif [ "$flag_scope" == "l" ] || [ "$flag_scope" == "local" ]; then
print_aliases "$local_file" "$verbose"
elif [ "$flag_scope" == "h" ] || [ "$flag_scope" == "here" ]; then
echo "Error: Invalid argument. Must be one of [ g, global, l, local, a, all ]" >&2
exit 1
else
local temp_combined_file="$(mktemp)"
echo "$(merge_pls_files "$global_file" "$local_file")" > "$temp_combined_file"
print_aliases "$temp_combined_file" "$verbose"
rm "$temp_combined_file"
fi
}
# src/lib/workers/pick_and_execute_alias.sh
pick_and_execute_alias() {
current_list="$(list_aliases)"
if [[ "$current_list" == "No aliases found." ]]; then
echo "No aliases found."
exit 0
fi
# if $PLS_ENABLE_FZF is true, and fzf is installed
if [[ "$PLS_ENABLE_FZF" = true ]] && command -v fzf >/dev/null 2>&1; then
picked_alias="$(echo "$current_list" | fzf --height 40% --reverse --prompt="Enter alias: ")"
# Remove ' *' from the end of the alias if it exists
else
# print the list with
# 1 | content
echo "$current_list" | awk '{print NR " | " $0}'
read -p "Enter alias (or n): " input
if [[ "$input" =~ ^[0-9]+$ && "$input" -gt 0 ]]; then
# Get alias from line number
picked_alias="$(echo "$current_list" | sed -n "${input}p")"
else
# actually entered an alias instead of a number
picked_alias="$input"
fi
fi
picked_alias="${picked_alias% \*}"
execute_alias "$picked_alias"
}
# :command.command_functions
# :command.parse_requirements
parse_requirements() {
# :command.fixed_flags_filter
while [[ $# -gt 0 ]]; do
case "${1:-}" in
--version | -v)
version_command
exit
;;
--help | -h)
long_usage=yes
pls_usage
exit
;;
*)
break
;;
esac
done
# :command.environment_variables_filter
# :command.environment_variables_default
export PLS_RC="${PLS_RC:-$HOME/.config/pls/.plsrc}"
env_var_names+=("PLS_RC")
# :command.dependencies_filter
if command -v yq >/dev/null 2>&1; then
deps['yaml_parser']="$(command -v yq | head -n1)"
else
printf "missing dependency: yaml_parser\n" >&2
printf "%s\n" "See https://github.com/mikefarah/yq for installation instructions." >&2
exit 1
fi
# :command.command_filter
action="root"
# :command.parse_requirements_while
while [[ $# -gt 0 ]]; do
key="$1"
case "$key" in
# :flag.case
--add | -a)
# :flag.case_no_arg
args['--add']=1
shift
;;
# :flag.case
--delete | -d)
# :flag.case_no_arg
args['--delete']=1
shift
;;
# :flag.case
--list | -l)
# :flag.case_no_arg
args['--list']=1
shift
;;
# :flag.case
--command | -c)
# :flag.case_arg
if [[ -n ${2+x} ]]; then
args['--command']="$2"
shift
shift
else
printf "%s\n" "--command requires an argument: --command, -c COMMAND" >&2
exit 1
fi
;;
# :flag.case
--scope | -s)
# :flag.case_arg
if [[ -n ${2+x} ]]; then
args['--scope']="$2"
shift
shift
else
printf "%s\n" "--scope requires an argument: --scope, -s SCOPE" >&2
exit 1
fi
;;
# :flag.case
--print | -p)
# :flag.case_no_arg
args['--print']=1
shift
;;
# :flag.case
--force | -f)
# :flag.case_no_arg
args['--force']=1
shift
;;
# :flag.case
--clear-cache)
# :flag.case_no_arg
args['--clear-cache']=1
shift
;;
# :flag.case
--this-does-nothing)
# :flag.case_no_arg
args['--this-does-nothing']=1
shift
;;
-?*)
printf "invalid option: %s\n" "$key" >&2
exit 1
;;
*)
# :command.parse_requirements_case
# :command.parse_requirements_case_repeatable
# :argument.case_repeatable
escaped="$(printf '%q' "$1")"
if [[ -z ${args['alias']+x} ]]; then
args['alias']="$1"
# :argument.case_repeatable
elif [[ -z ${args['command_args']+x} ]]; then
args['command_args']="$escaped"
else
args['command_args']="${args['command_args']} $escaped"
fi
shift
;;
esac
done
# :command.needy_flags_filter
# :flag.needs
if [[ -n ${args['--add']+x} ]]; then
for need in --command --scope; do
if [[ -z "${args[$need]:-}" ]]; then
printf "%s\n" "--add needs $need" >&2
exit 1
fi
done
fi
# :flag.needs
if [[ -n ${args['--delete']+x} ]] && [[ -z "${args[--scope]:-}" ]]; then
printf "%s\n" "--delete needs --scope" >&2
exit 1
fi
# :command.validations
# :argument.validations
if [[ -v args['alias'] && -n $(validate_not_empty "${args['alias']:-}") ]]; then
printf "validation error in %s:\n%s\n" "ALIAS" "$(validate_not_empty "${args['alias']:-}")" >&2
exit 1
fi
# :flag.validations
if [[ -v args['--add'] && -n $(validate_is_only_operation "${args['--add']:-}") ]]; then
printf "validation error in %s:\n%s\n" "--add, -a" "$(validate_is_only_operation "${args['--add']:-}")" >&2
exit 1
fi
# :flag.validations
if [[ -v args['--delete'] && -n $(validate_is_only_operation "${args['--delete']:-}") ]]; then
printf "validation error in %s:\n%s\n" "--delete, -d" "$(validate_is_only_operation "${args['--delete']:-}")" >&2
exit 1
fi
# :flag.validations
if [[ -v args['--list'] && -n $(validate_is_only_operation "${args['--list']:-}") ]]; then
printf "validation error in %s:\n%s\n" "--list, -l" "$(validate_is_only_operation "${args['--list']:-}")" >&2
exit 1
fi
# :flag.validations
if [[ -v args['--command'] && -n $(validate_not_empty "${args['--command']:-}") ]]; then
printf "validation error in %s:\n%s\n" "--command, -c COMMAND" "$(validate_not_empty "${args['--command']:-}")" >&2
exit 1
fi
# :command.whitelist_filter