-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebox_fzf
executable file
·139 lines (115 loc) · 3.31 KB
/
firebox_fzf
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
#!/bin/sh
COMMAND="firebox"
profile_dir="$("$COMMAND" config print_profile_dir)"
sep="$("$COMMAND" config print_separator)"
sep_esc="$("$COMMAND" config print_separator --escaped)"
if [ -d "$profile_dir" ]; then
options="service\nworkspace\nexplorer"
else
options="init"
fi
fzf_cmd() {
fzf --multi "$@"
}
init() {
"$COMMAND" init
}
service() {
services=$("$COMMAND" service list)
[ -z "$services" ] && exit 1
options="list\nlaunch"
option=$(echo "$options" | fzf_cmd --no-multi "$@")
case "$option" in
list)
printf "%s\n\n" "Available services:" && "$COMMAND" service list
;;
launch)
services=$("$COMMAND" service list --raw | sed "s/$sep.*//g" | fzf_cmd "$@")
service_list=""
echo "$services" | (
while read -r i; do
service_list="'$i' $service_list"
done
if [ -n "$services" ]; then
echo "$service_list" | xargs "$COMMAND" service launch
fi
)
;;
*)
echo "Invalid option" &&
exit 1
;;
esac
}
add_to_workspace() {
[ -n "$1" ] && workspace_name="$1" && shift
services=$("$COMMAND" service list --raw | sed "s/$sep.*//g" | fzf_cmd "$@" | sed 's/ /\\ /g')
[ -n "$services" ] &&
echo "$workspace_name" "$services" | xargs "$COMMAND" workspace add
}
workspace() {
services=$("$COMMAND" service list)
[ -z "$services" ] && exit 1
workspaces=$("$COMMAND" workspace list --all)
if [ -z "$workspaces" ]; then
options="create"
else
options="create\nlist\nlaunch\nadd\nremove\ndelete"
fi
option=$(echo "$options" | fzf_cmd --no-multi "$@")
case "$option" in
create)
printf "Enter workspace name: "
read -r workspace_name
"$COMMAND" workspace create "$workspace_name" &&
add_to_workspace "$workspace_name" "$@"
;;
list)
workspace_name=$(printf "== ALL ==\n%s" "$("$COMMAND" workspace list --all --raw | sed "s/$sep.*//")" | fzf_cmd --no-multi "$@")
[ "$workspace_name" = "== ALL ==" ] && workspace_name="--all"
printf "Available services:\n\n" && "$COMMAND" workspace list "$workspace_name"
;;
launch)
workspace_name=$("$COMMAND" workspace list --all --raw | sed "s/$sep.*//" | fzf_cmd --no-multi "$@")
"$COMMAND" workspace launch "$workspace_name"
;;
add)
workspace_name="'$("$COMMAND" workspace list --all --raw | sed "s/$sep.*//" | fzf_cmd --no-multi "$@")'"
add_to_workspace "$workspace_name" "$@"
;;
remove)
workspace_name="'$("$COMMAND" workspace list --all --raw | sed "s/$sep.*//" | fzf_cmd --no-multi "$@")'"
if [ -n "$workspace_name" ]; then
available_services=$(echo "$workspace_name" | xargs -I "{}" "$COMMAND" workspace list "{}" --raw | grep -iEo "$sep_esc.*\$" | sed "s/$sep//;s/$sep/\n/g;")
if [ -n "$available_services" ]; then
services=$(echo "$available_services" | fzf_cmd "$@" | sed 's/ /\\ /g')
else
echo "Workspace Empty"
fi
[ -n "$services" ] &&
echo "$workspace_name" "$services" | xargs "$COMMAND" workspace remove
fi
;;
delete)
workspace_name="'$("$COMMAND" workspace list --all --raw | sed "s/$sep.*//" | fzf_cmd --no-multi "$@")'"
[ -n "$workspace_name" ] && (echo "$workspace_name" | xargs "$COMMAND" workspace delete)
;;
*)
echo "Invalid option" &&
exit 1
;;
esac
}
explorer() {
"$COMMAND" explorer
}
option=$(echo "$options" | fzf_cmd --no-multi "$@")
case "$option" in
init | explorer | service | workspace)
"$option" "$@"
;;
*)
echo "Invalid option" &&
exit 1
;;
esac