This repository was archived by the owner on May 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspaces.sh
More file actions
1651 lines (1441 loc) · 46.6 KB
/
workspaces.sh
File metadata and controls
1651 lines (1441 loc) · 46.6 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
#-------------------------------------------------------------------------
# workspaces.sh
#
# Provides functions to setup and control workspaces. A workspace is simply
# a directory containing some special files. It is a bit heavy handed and
# implements workspaces by running a sub-bash shell. This ensures that
# switching workspaces don't polute each others environment spaces.
# Following are the environment variables and special files that are
# used by this package.
#
# Special files/directories:
# - local: In the directory that is a workspace a
# - ./.workspace - directory is created. This directory contains:
# - on_enter.sh - file that is run on workspace startup
# - on_exit.sh - file that is run on workspace exit
# - bash_history - use this file to maintain the bash history.
# - id.<NNNN> - randomly generated unique identify for workspace.
# Structure for ~/.workspaces directory:
# - current/ - Contains symlinks to registered workspaces
# - archive/ - Contains symlinks to registered archived workspaces.
# - tmp/ - A temporary space for workspaces that are active.
# Each workspace has a sub-directory with its ID.
# - pids.tmp - temporary file containing process id of active
# workspace shells.
#
# Environment variables to override default module behaviour:
#
# - WORKSPACES_METADATA_DIR - Where to put the meta-data for the workspaces
# module. Defaults to ~/.workspaces/
#
# - WORKSPACES_TMP_DIR - Location for non-workspace specific temporary data
#
# Environment variables that can be read by user from within a workspace:
#
# - WORKSPACE_DIR - A workspace HOME directory.
# - WORKSPACE_TMP_DIR - The workspace's temporary area
# (typically set to $WORKSPACES_METADATA_DIR/tmp/<ID>).
# - WORKSPACE_ID - A workspace identifier, from the workspace id file.
#
# Environment variables that are for internal use of the module:
#
# - _WORKSPACE_LEVEL - Level of stacked workspaces.
# - _WORKSPACE_TMPFILE - A temporary file for starting and cleaning up
# stacked workspace.
# - _WORKSPACE_BASH_ROOT_PID - The PID of the workspace root.
# - _WORKSPACES_HOOK_ON_ENTER - An array of handlers to be called
# before on_enter.sh is run.
# - _WORKSPACES_HOOK_ON_EXIT - An array of handlers to be called
# after on_exit.sh has been run.
#
# The idea is to maintain symlinks to workspaces so that we can
# quickly list and go to them using tab completion.
#
# Main user callable command functions:
# 1) wksp <cmd> <args> - run "wksp help" for more information.
# 2) ws - shortcut for "wksp chg"
# 3) wsls - shortcut for "wksp ls". "ls" relative to WORKSPACE_DIR.
# 4) wscd - shortcut for "wksp cd". "cd" relative to WORKSPACE_DIR.
#
#
# Workspaces allow for a simple extension mechanism. An extension
# module can call the wksps_hook_on_enter() and wksps_hook_on_exit()
# functions to setup handlers that are then called before
# on_enter.sh and after on_exit.sh respectively. Note: once an
# extension has been attached, it cannot be detached.
# Prerequisites: assumes readlink and sed are installed.
#
# Bug/clean-up notes:
# 1) Initially I thought of allowing workspaces to be pushed and popped
# off a stack. In hindsight I don't see any particular reason for this,
# so the code contains a mix of the push/pop stuff (eg., the use of a
# WORKSPACE_LEVEL env variable) as well as a simpler load/unload model.
# So should clean this up and simplify things at some point.
# 2) Do a better job of understanding how the completions works. I'm sure
# that there must be some tricks that would simplify things a lot.
# Especially in terms of using aliases instead of short version functions.
# 3) bug: completion for "wksp ls|cd" doesn't works as well as "wsls|wscd".
# 4) Currently have to load workspaces.sh after any "ls" aliases have been
# setup, otherwise things like coloring of entries won't work. Should
# be possible to do this properly.
#
# Fixes:
# 20160615 - Problem with workspace sub-shells not being setup correctly
# because they won't inherit the bash functions that were setup
# with the workspace on_enter.sh file. Proposed solution is to
# modify the load_if function to detect if it is a sub-shell
# and if so to source the workspace's on_enter.sh.
#
# 20170518 - Added features to execute a given command as part of entering a
# workspace. Modified wksps_num_active_pids() to take an optional
# argument to check a particular workspace and not the current. Added
# function to find the workspace associated with a given file.
#
# 20180313 - Extend the .workspaces directory to include the ability to store
# archived projects. The .workspaces directory now includes 'archive'
# and 'current' sub-directories. The symlinks have now been moved
# to the 'current' sub-directory.
#----------------------------------------------------------------------
mbimport prompts
mbimport logging
mbimport misc_functions
#-----------------------------------------------------------------------
# Set the parameters.
# If WORKSPACES_METADATA_DIR is not set then use "$HOME/.workspaces"
#-----------------------------------------------------------------------
_wksps_set_env (){
if [ "$WORKSPACES_METADATA_DIR" == "" ]; then
export WORKSPACES_METADATA_DIR="$HOME/.workspaces"
fi
export _WORKSPACES_SYMLINKS_DIR="$WORKSPACES_METADATA_DIR/links"
export WORKSPACES_TMP_DIR="$WORKSPACES_METADATA_DIR/tmp"
}
#------------------------------
# _wksps_init
# - Call this to make sure things are setup correctly
#------------------------------
_wksps_init (){
if [ ! -d "$WORKSPACES_METADATA_DIR" ]; then
log_info "Creating workspaces metadata directory: $WORKSPACES_METADATA_DIR"
mkdir -p "$WORKSPACES_METADATA_DIR"
mkdir -p "$_WORKSPACES_SYMLINKS_DIR"
mkdir -p "$WORKSPACES_TMP_DIR"
fi
if [ ! -d "$_WORKSPACES_SYMLINKS_DIR" ]; then
log_info "Creating workspaces symbolic links directory: $_WORKSPACES_SYMLINKS_DIR"
mkdir -p "$_WORKSPACES_SYMLINKS_DIR"
fi
if [ ! -d "$WORKSPACES_TMP_DIR" ]; then
log_info "Creating workspaces temp directory: $_WORKSPACES_SYMLINKS_DIR"
mkdir -p "$WORKSPACES_TMP_DIR"
fi
}
#-----------------------------------------------------------------------
# Generic internal functions
#-----------------------------------------------------------------------
#------------------------------
# _wksps_get_abs_name <directory>
# - return an absolute or if it is HOME relative then ~.
# (e.g., /usr/local/hello or ~/hello)
#------------------------------
_wksps_get_abs_name (){
local cleaned=$(echo "$*" | sed -e 's!^~\(.*\)$!'"$HOME"'/\1!')
local absws=$(readlink -m "$cleaned")
echo "$absws"
}
_wksps_get_tilda_name (){
local absws=$(readlink -m "$*")
local cleanws=$(echo "$absws" | sed -e 's!^'"$HOME"'\(.*\)$!~\1!')
echo "$cleanws"
}
#------------------------------
# _wksps_args
# Pass variables through:
# <cmd> `_wksps_args <first_index> "$@"`
# @param Index of first argument to pass through
# @param List of parameters
#
# taken from - https://github.com/stianlik/bash-workspace/blob/master/workspace.sh
#------------------------------
_wksps_args() {
local i=1
local min=$(( $1 + 1 ))
for var in "$@"; do
i=$(( $i + 1 ))
if [ "$i" -gt "$min" ]; then echo $var; fi
done;
}
#export -f _wksps_args
#------------------------------
# _wksps_args_is_option
# skip optional arguments (anything starting with '-')
#------------------------------
_wksps_args_is_option() {
if [[ "$@" =~ /- ]]; then
return 0
fi
return 1
}
#export -f _wksps_args_is_option
#-----------------------------------------------------------------------
# Internal functions for creating and loading workspaces
#-----------------------------------------------------------------------
#------------------------------
# _wksps_tmp_dir <workspace>
#------------------------------
_wksps_get_ws_tmp_dir (){
local ws="$*"
local id=$(_wksps_get_ws_id "$ws")
if [ "$id" == "" ]; then
log_debug "_wksps_get_ws_tmp_dir failed: $ws : $id"
log_error "Failed to find workspace ID"
echo ""
return 1
fi
echo "$WORKSPACES_TMP_DIR/$id"
return 0
}
#------------------------------
# _wksps_mk_local_ws_dir <workspace>
#------------------------------
_wksps_mk_local_ws_dir (){
local ws="$*"
local absws=$(_wksps_get_abs_name "$*")
if [ ! -d $absws ]; then
log_error "Not a directory: $ws"
return 1
fi
if [ ! -d "$absws/.workspace" ]; then
mkdir "$absws/.workspace"
fi
return 1
}
#export -f _wksps_mk_local_ws_dir
#------------------------------
# _wksps_create_ws_id, _wksps_get_ws_id, _wksps_load_ws_id <workspace>
# Create a workspace ID file
# Get the workspace ID file
# Load a workspace ID
#------------------------------
_wksps_random_id (){
RANDOM=$(date +%s)
local r1=$(printf "%05d" $RANDOM)
local r2=$(printf "%05d" $RANDOM)
echo "$r1$r2"
}
_wksps_get_ws_id (){
local ws="$*"
local absws=$(_wksps_get_abs_name "$ws")
local id
id=$(ls "$absws"/.workspace/id.* 2>/dev/null | head -n 1 | sed -n 's/^.*\.workspace\/id\.\(.*\)$/\1/p')
if [ "$id" == "" ]; then
log_debug "_wksps_get_ws_id failed: $ws : $absws : $id"
fi
echo "$id"
}
_wksps_get_ws_pidsfile (){
local ws="$*"
local ws_tmp_dir=$(_wksps_get_ws_tmp_dir $ws)
if [ "$ws_tmp_dir" == "" ]; then
log_debug "_wksps_get_ws_pidsfile failed: $ws : $absws : $ws_tmp_dir"
log_error "Failed to get tmp dir for workspace $ws"
echo ""
return 1
fi
echo "$ws_tmp_dir/pids.tmp"
return 0
}
_wksps_create_ws_id (){
local ws="$*"
local id
id=$(_wksps_get_ws_id "$ws")
if [ "$id" != "" ]; then
log_warn "Workspace already contains an ID file: $ws"
return 0
fi
id=$(_wksps_random_id)
_wksps_mk_local_ws_dir "$ws"
touch "$ws/.workspace/id.$id"
return 0
}
#export -f _wksps_create_ws_id
_wksps_load_ws_id (){
local ws="$*"
local id
[ ! -d "$ws"/.workspace ] && return
id=$(_wksps_get_ws_id "$ws")
if [ "$id" == "" ]; then
if ! _wksps_create_ws_id "$ws"; then
log_error "Failed to create and load a workspace ID file"
return 1
fi
id=$(_wksps_get_ws_id "$ws")
fi
export WORKSPACE_ID=$id
return 0
}
#-------------------------------
# Given a workspace ID check the symlinks to find the corresponding workspace
#-------------------------------
_wksps_get_ws_from_link_id (){
local id="$*"
local link="$_WORKSPACES_SYMLINKS_DIR/$id"
if [ ! -L "$link" ] || [ ! -d "$link" ]; then
log_warn "Missing symlink $link for id: $id"
echo ""
return 1
fi
echo $(_wksps_get_abs_name "$link")
}
_wksps_has_ws_link_id (){
local id="$*"
local link="$_WORKSPACES_SYMLINKS_DIR/$id"
if [ -L "$link" ] && [ -d "$link" ]; then
return 0
fi
return 1
}
#-------------------------------
# _wksps_cleanup_inactive_pids ()
# Clean up the workspace id file to only include active entries
#-------------------------------
_wksps_cleanup_inactive_pids (){
local ws="$*"
local absws=$(_wksps_get_abs_name "$ws")
local pidsfile=$(_wksps_get_ws_pidsfile "$absws")
local activelist=()
local pid
local hasinactive="False"
if [ "$pidsfile" == "" ]; then
log_error "Failed to get name of processes id file for workspace $ws"
return 1
fi
if [ ! -f "$pidsfile" ]; then
# touch "$pidsfile"
return 0
fi
while read pid; do
if [[ $pid =~ [0-9]+ ]]; then
local result=$(ps --no-headers $pid)
if [ "$result" != "" ]; then
activelist[${#activelist[*]}]=$pid
else
hasinactive="True"
fi
fi
done < "$pidsfile"
# Now writeout the active list if required
if [ "$hasinactive" == "True" ] ; then
> "$pidsfile"
for pid in "${activelist[@]}"; do
echo $pid >> "$pidsfile"
done
# If the file is empty then remove it
if [ ! -s "$pidsfile" ]; then
rm "$pidsfile"
fi
fi
}
#-------------------------------
# _wksps_cleanup_tmpdir()
# Clean up the of the workspace tmp directory.
#
# NOTE:Removed from the cleanup of inactive pids as it should be the last action
# performed since extensions may use this and the active pids is cleaned up
# before the extendion hooks are called.
#-------------------------------
_wksps_cleanup_tmpdir (){
local ws="$*"
local absws=$(_wksps_get_abs_name "$ws")
local wstmpdir=$(_wksps_get_ws_tmp_dir "$absws")
if [ "$wstmpdir" == "" ]; then
log_error "Failed to get name of tmp dir for workspace $ws"
return 1
fi
# If the file is empty then remove it and the temporary dir
rmdir "$wstmpdir"
}
#export -f _wksps_cleanup_inactive_pids
#-------------------------------
# Returns the number of active pids for the workspace
# wksps_num_active_pids <directory>
#-------------------------------
_wksps_num_active_pids ()
{
local ws="$*"
local absws=$(_wksps_get_abs_name "$ws")
local pidsfile=$(_wksps_get_ws_pidsfile "$absws")
local activelist=()
local pid
# First cleanup any inactive pids
_wksps_cleanup_inactive_pids "$ws"
# Now just need to count the pids in the file
if [ ! -f "$pidsfile" ]; then
echo "0"
return 0
fi
# Read the list of pids - checking against running processes
while read pid; do
if [[ $pid =~ [0-9]+ ]]; then
activelist[${#activelist[*]}]=$pid
fi
done < "$pidsfile"
echo "${#activelist[*]}"
}
_wksps_has_active_pids ()
{
local ws="$*"
local num=$(_wksps_num_active_pids "$ws")
if [ "$num" != "0" ]; then
return 0
fi
return 1
}
#-------------------------------
# Returns if the workspace is active. Note: we may not be IN this workspace
#-------------------------------
_wksps_is_active (){
local ws="$*"
! _wksps_is_ws "$ws" && return 1
local num=$(_wksps_num_active_pids "$ws")
if [ "$num" != "0" ]; then
return 0
fi
return 1
}
#------------------------------
# _wksps_create_ws_history, _wksps_load_ws_history <workspace>
# Create a workspace history file
# Load a workspace history file
#------------------------------
_wksps_create_ws_history (){
local ws="$*"
local wshistory="$*/.workspace/bash_history"
if [ -f "$wshistory" ]; then
log_warn "Workspace already contains a history file: $ws"
return 0
fi
_wksps_mk_local_ws_dir "$ws"
touch "$wshistory"
return 0
}
#export -f _wksps_create_ws_history
_wksps_load_ws_history (){
local ws="$*"
local absws=$(_wksps_get_abs_name "$*")
local wshistory="$absws/.workspace/bash_history"
[ ! -d "$absws/.workspace" ] && return
if [ ! -f "$wshistory" ]; then
_wksps_create_ws_history "$ws"
fi
# history -w # write the current history
export HISTFILE="$wshistory"
history -c # clear history
history -r # read new history file
return 0
}
#export -f _wksps_load_ws_history
#-------------------------------
# _wksps_create_ws_scripts <directory>
# - Create a workspace startup file
# - Load a workspace startup file
#-------------------------------
_wksps_create_ws_scripts (){
local ws="$*"
local on_enter="$*/.workspace/on_enter.sh"
local on_exit="$*/.workspace/on_exit.sh"
_wksps_mk_local_ws_dir "$ws"
if [ -f "$on_enter" ]; then
log_warn "Workspace on_enter script already exists in: $ws"
else
cat > "$on_enter" <<EOF
# Workspace configuration file.
# Some variables that you can use:
# - WORKSPACE_ID - the workspace unique identifier
# - WORKSPACE_DIR - location of this workspace
# Additionally the function "wksps_num_active_pids" returns the
# number of active base shells for this workspace. This is useful
# if you want to run a program (eg., some background daemon) to be
# shared by all instances of the workspace.
local npids=\$(wksps_num_active_pids)
echo "Setting up workspace[\$npids]..."
EOF
fi
if [ -f "$on_exit" ]; then
log_warn "Workspace on_exit script already exists in: $ws"
else
cat > "$on_exit" <<EOF
# Workspace clean file. Can use the WORKSPACE_ID, WORKSPACE_DIR variables
# as in the startup file.
# Note: 1) any configuration that was setup in the on_enter.sh script will
# be gone by the time we get here.
# 2) The "wksps_num_active_pids" returns the number of active shells
# not including this one. So if this is cleaning up the last active
# shell for the workspace then "wksps_num_active_pids" will return 0.
local npids=\$(wksps_num_active_pids)
if [ \$npids -eq 0 ]; then
echo "Cleaning up workspace..."
fi
EOF
fi
return 0
}
#export -f _wksps_create_ws_scripts
_wksps_load_ws_on_enter_script (){
local ws="$*"
local wsfile="$*/.workspace/on_enter.sh"
[ ! -f "$wsfile" ] && return 1
source "$wsfile"
return 0
}
#export -f _wksps_load_ws_on_enter_script
#-------------------------------
# _wksps_is_common_ws, _wksps_mk_common_ws, _wksps_delws_common_ws <workspace>
# - check if the workspace is a common ws
# - make the workspace a common ws
# - remove the workspace from the common ws
#-------------------------------
#-------------------------------
# _wksps_create_ws_link, _wksps_remove_ws_link <directory>
# - create the symlink for the workspace
#-------------------------------
_wksps_create_ws_link (){
local ws="$*"
local absws=$(_wksps_get_abs_name "$*")
id=$(_wksps_get_ws_id "$ws")
if [ "$id" == "" ]; then
log_error "Not a workspace, missing id file: $ws"
return
fi
ln -s "$absws" $_WORKSPACES_SYMLINKS_DIR/$id
}
#export -f _wksps_create_ws_link
_wksps_remove_ws_link (){
local ws="$*"
local absws=$(_wksps_get_abs_name "$*")
id=$(_wksps_get_ws_id "$ws")
if [ "$id" == "" ]; then
log_error "Not a workspace, missing id file: $ws"
return
fi
rm -f $_WORKSPACES_SYMLINKS_DIR/$id
}
#export -f _wksps_remove_ws_link
_wksps_remove_ws_link_id (){
local id="$*"
local idfile=$_WORKSPACES_SYMLINKS_DIR/$id
if [ ! -L $idfile ]; then
log_error "Id $id does not have a link file: $idfile"
return
fi
rm -f $idfile
}
#-------------------------------
# _wksps_has_ws_link ()
# - Check if the workspace has a link
#-------------------------------
_wksps_has_ws_link (){
local ws="$*"
local absws=$(_wksps_get_abs_name "$*")
local found=$(ls -l $_WORKSPACES_SYMLINKS_DIR/ | grep "$absws")
[ "$found" != "" ]
}
#export -f _wksps_has_ws_link
#-------------------------------
# _wksps_is_ws <directory>
# returns 0 if the directory is a workspace
# (ie., has a .workspace.sh and workspace id file, and a ws link)
# or 1 otherwise.
#-------------------------------
_wksps_is_ws (){
local ws="$*"
local absws=$(_wksps_get_abs_name "$ws")
local id=$(_wksps_get_ws_id "$absws")
if [ ! -d "$absws" ]; then
return 1
fi
if [ ! -d "$absws/.workspace" ] || [ "$id" == "" ]; then
return 1
fi
if ! _wksps_has_ws_link "$absws" ; then
return 1
fi
return 0
}
_wksps_is_ws_id (){
local wsid="$*"
local link="$_WORKSPACES_SYMLINKS_DIR/$id"
local wsdir=$(readlink -f "$link")
if [ ! -L "$link" ] || [ ! -d "$link" ] || [ "$wsdir" == "" ] ; then
return 1
fi
return _wksps_is_ws "$wsdir"
}
#export -f _wksps_is_ws
#-------------------------------
# _wksps_in_ws <directory>
# Is this workspace currently loaded
#-------------------------------
_wksps_in_ws (){
local ws="$*"
local id=$(_wksps_get_ws_id "$*")
[ "$WORKSPACE_ID" == "$id" ]
}
#export -f _wksps_in_ws
#-------------------------------
# _wksps_load_ws <workspace>
# - Load a workspace (setting the appropriate env variable)
#-------------------------------
_wksps_load_ws (){
local ws="$*"
local absws=$(_wksps_get_abs_name "$*")
local pidsfile=$(_wksps_get_ws_pidsfile "$absws")
local wstmpdir=$(_wksps_get_ws_tmp_dir "$absws")
if ! _wksps_is_ws "$absws" ; then
log_error "Not a workspace: $ws"
return 1
fi
# Setup the environment
export _WORKSPACE_BASH_ROOT_PID=$$
if [ -z "$_WORKSPACE_LEVEL" ]; then
export _WORKSPACE_LEVEL=0
else
export _WORKSPACE_LEVEL=$(($_WORKSPACE_LEVEL+1))
fi
export WORKSPACE_DIR="$absws"
export WORKSPACE_TMP_DIR="$wstmpdir"
if [ ! -d "$WORKSPACE_TMP_DIR" ]; then
mkdir -p "$WORKSPACE_TMP_DIR"
fi
# Add the current workspace bash shell to the active PID list
echo "$_WORKSPACE_BASH_ROOT_PID" >> "$pidsfile"
# Set things up
_wksps_load_ws_id "$absws"
_wksps_load_ws_history "$absws"
# Run the workspaces on_enter hooks
for hook in "${_WORKSPACES_HOOK_ON_ENTER[@]}"; do
eval "$hook"
done
# Call the user on_enter script
_wksps_load_ws_on_enter_script "$absws"
}
#export -f _wksps_load_ws
#-------------------------------
# _wksps_set_ws_cleanup_fn <string>
# This function is run from within the workspace temp startup file
#-------------------------------
_wksps_set_ws_cleanup_fn (){
if [ "$_WORKSPACE_TMPFILE" == "" ]; then
log_error "No _WORKSPACE_TMPFILE variable defined"
return 1
fi
# We want to know the pid of the workspace shell from the calling shell
echo "export _WORKSPACE_BASH_ROOT_PID=$$" > $_WORKSPACE_TMPFILE
# Create a results function
echo "_wksps_tmp_run_cleanup_fn () {" >> $_WORKSPACE_TMPFILE
echo "$*" >> $_WORKSPACE_TMPFILE
echo "}" >> $_WORKSPACE_TMPFILE
return 0
}
#export -f _wksps_set_ws_cleanup_fn
#-------------------------------
# _wksps_get_all
# - Get the list of workspaces by setting the value of a
# known global array. WORKSPACES
#-------------------------------
_wksps_get_all (){
local fn
local ws
WORKSPACES=()
for fn in $_WORKSPACES_SYMLINKS_DIR/*; do
if [ -h "$fn" ]; then
ws=$(_wksps_get_tilda_name $(readlink "$fn"))
WORKSPACES[${#WORKSPACES[@]}]="$ws"
fi
done
}
#export -f _wksps_get_all
#-----------------------------------------------------------------------
# Functions corresponding to the main commands
#-----------------------------------------------------------------------
#-------------------------------
# wksps_push <workspace>
# Push a workspace on to the workspace stack.
# Only call this directly if you know what you are doing.
#-------------------------------
_wksps_push () {
local currdir=$(pwd)
local newws=$(readlink -m "$1")
local unsetlevel=0
local savedwstmpfile="$_WORKSPACE_TMPFILE"
local cleanupfn
shift
local execcmd="$*"
# Save state of the current environment so we can
# recover properly after the workspace is popped
if [ -z "$_WORKSPACE_LEVEL" ]; then
unsetlevel=1
export _WORKSPACE_LEVEL=0
fi
# Make sure that we are talking about a workspace
if ! _wksps_is_ws "$newws" ; then
log_error "Not a workspace: $newws"
return
fi
# A temporary file for communicating with the new shell workspace
export _WORKSPACE_TMPFILE=$(mktemp "/tmp/${USER}_tmpws.XXXXXXXXX")
# Go to the new workspace dir, load the workspace, set a default
# cleanup function, then source ~/.bashrc. Note: default cleanup
# is necessary for Ctrl-D (EOF) to exit properly.
echo "[ -f /etc/bash.bashrc ] && source /etc/bash.bashrc" > $_WORKSPACE_TMPFILE
echo "[ -f ~/.bashrc ] && source ~/.bashrc" >> $_WORKSPACE_TMPFILE
# echo "source ~/.bashrc" > $_WORKSPACE_TMPFILE
echo "cd \"$newws\"" >> $_WORKSPACE_TMPFILE
echo "_wksps_load_ws ." >> $_WORKSPACE_TMPFILE
echo "_wksps_set_ws_cleanup_fn \"exit\"" >> $_WORKSPACE_TMPFILE
if [ "$execcmd" != "" ]; then # the optional command
echo "$execcmd" >> $_WORKSPACE_TMPFILE
fi
# Set up WORKSPACE_ID, WORKSPACE_DIR, and WORKSPACE_TMP_DIR
export WORKSPACE_ID=$(_wksps_get_ws_id "$newws")
export WORKSPACE_DIR="$newws"
export WORKSPACE_TMP_DIR=$(_wksps_get_ws_tmp_dir "$newws")
# Make the workspace directory the current directory for the
# non-workspace parent shell. This will make opening a new
# shell from this parent more intuitive.
builtin cd "$newws"
# Load and enter the new workspace
bash --rcfile $_WORKSPACE_TMPFILE
# Reload the workspace tmpfile. This defines the cleanup function and
# the PID of the (now exited) workspace shell.
if [ -f "$_WORKSPACE_TMPFILE" ]; then
source "$_WORKSPACE_TMPFILE"
rm -f "$_WORKSPACE_TMPFILE"
fi
# Remove the PID from the active list in the workspace id file
_wksps_cleanup_inactive_pids "$newws"
# Now run the on_exit script
if [ -f "$newws/.workspace/on_exit.sh" ]; then
source "$newws/.workspace/on_exit.sh"
fi
# Finally run any extension hooks
for hook in "${_WORKSPACES_HOOK_ON_EXIT[@]}"; do
eval "$hook"
done
# If this is was the last active then remove the temporary directory
if ! _wksps_has_active_pids "$newws" ; then
_wksps_cleanup_tmpdir "$newws"
fi
# Recover from the
unset WORKSPACE_ID
unset WORKSPACE_DIR
unset WORKSPACE_TMP_DIR
unset _WORKSPACE_BASH_ROOT_PID
# Recover from the pop/exit/switch
if [ "$unsetlevel" -eq "1" ]; then
unset _WORKSPACE_LEVEL
fi
# Now run the cleanup function
cleanupfn=$(declare -f "_wksps_tmp_run_cleanup_fn")
if [ "$cleanupfn" != "" ]; then
_wksps_tmp_run_cleanup_fn
unset -f _wksps_tmp_run_cleanup_fn
fi
# The clean up has been performed
if [ "$unsetlevel" -eq "1" ]; then
unset _WORKSPACE_TMPFILE
else
export _WORKSPACE_TMPFILE="$savedwstmpfile"
fi
builtin cd "$currdir"
}
#export -f _wksps_push
#-------------------------------
# wksps_pop - Pop a stacked workspace
# Only call this directly if you know what you are doing.
#-------------------------------
_wksps_pop () {
if [ -z "$_WORKSPACE_LEVEL" ] || [ "$_WORKSPACE_LEVEL" -eq "0" ]; then
log_error "No loaded workspaces"
return 1
fi
# Can only unload from a workspace bash root
if wksps_is_subshell ; then
log_error "Cannot unload a workspace from a workspace sub-shell"
return 1
fi
_wksps_set_ws_cleanup_fn ":"
builtin exit &>/dev/null
}
#export -f _wksps_pop
#-------------------------------
# wksps_reload
# Reload a workspace
#-------------------------------
_wksps_reload () {
if [ "$WORKSPACE_DIR" == "" ]; then
log_error "No workspace loaded"
return 1
fi
# Can only switch workspaces from the workspace bash root
if wksps_is_subshell ; then
log_error "Cannot reload workspace: unable to unload workspace from a workspace sub-shell"
return 1
fi
log_info "Reloading workspace..."
# Reload workspace means exiting the current shell also but setting up so
# the parent shell will re-startup the workspace.
_wksps_set_ws_cleanup_fn "_wksps_push \"$WORKSPACE_DIR"\"
builtin exit &>/dev/null
return 0
}
#-------------------------------
# wksps_mk () - make a workspace
#-------------------------------
_wksps_mk (){
local ws="$*"
if _wksps_is_ws "$ws" ; then
log_error "Already a workspace: $ws"
return 1
fi
_wksps_create_ws_scripts "$ws"
_wksps_create_ws_id "$ws"
_wksps_create_ws_history "$ws"
_wksps_create_ws_link "$ws"
return 0
}
#export -f _wksps_mk
#-------------------------------
# _wksps_isnumber
#-------------------------------
_wksps_isnumber() {
printf '%f' "$1" &> /dev/null
}
#-------------------------------
# _wksps_selws_prompt
# Prompt the user to select a workspace from a list
#-------------------------------
_wksps_selws_prompt (){
local wrksps_u=()
local wrksps_s=()
local tmpi
local prompt="$@"
# Build the list of workspace symlinks that point to valid directories.
for fn in $_WORKSPACES_SYMLINKS_DIR/*; do
if [ -h "$fn" ] && [ -d "$fn" ] ; then
wrksps_u[${#wrksps_u[@]}]=$(_wksps_get_tilda_name $(readlink "$fn"))
fi
done
# Display the sorted list with a number for each selection
for fn in $(for i in ${wrksps_u[@]}; do echo "$i"; done | sort) ; do
tmpi=$(( ${#wrksps_s[@]} + 1))
wrksps_s[${#wrksps_s[@]}]=$fn
echo "$tmpi) $fn" 1>&2
done
# Read/validate/return the answer
read -p "$@" answr
if ! _wksps_isnumber $answr; then
echo "error: not a number: $answr" 1>&2
echo ""
elif [ "$answr" -gt "0" ] && [ "$answr" -le "${#wrksps_s[@]}" ]; then
tmpi=$(( $answr - 1 ))
echo "${wrksps_s[$tmpi]}"
else
echo "error: out of range selection: $answr" 1>&2
echo ""
fi
}
#-----------------------------------------------------------------------
# Functions corresponding to archiving
#-----------------------------------------------------------------------
#-------------------------------
# _wksps_chgws_prompt
# Prompt the user to select a workspace from a list then
# change to that workspace.
#-------------------------------
_wksps_chgws_prompt (){
local res=""
while [ "$res" == "" ]; do