Skip to content

Commit cf61e1a

Browse files
authored
Merge pull request #12 from unixorn/add-option-to-rm-stoppped-containers
Add options to rm or start stoppped containers
2 parents 8418e82 + 5e78d96 commit cf61e1a

File tree

1 file changed

+75
-8
lines changed

1 file changed

+75
-8
lines changed

Diff for: lima-plugin

+75-8
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,28 @@ function printMenu() {
152152
echo "--⛔ Stop $name VM | bash=$XBAR_PLUGIN param1=stop param2=$name terminal=false refresh=true"
153153

154154
echo "-- Containers"
155-
for container in $(vmContainers)
155+
for container in $(vmContainers 'Up')
156156
do
157157
echo "---- $container"
158-
# echo "------ start | bash=$XBAR_PLUGIN param1=startContainer param2=$name param3=$container terminal=false refresh=true"
159-
echo "------ stop | bash=$XBAR_PLUGIN param1=stopContainer param2=$name param3=$container terminal=false refresh=true"
160-
echo "------ kill | bash=$XBAR_PLUGIN param1=killContainer param2=$name param3=$container terminal=false refresh=true"
161-
echo "------ pause | bash=$XBAR_PLUGIN param1=pauseContainer param2=$name param3=$container terminal=false refresh=true"
162-
echo "------ unpause | bash=$XBAR_PLUGIN param1=unpauseContainer param2=$name param3=$container terminal=false refresh=true"
158+
echo "------ Running"
159+
echo "-------- stop | bash=$XBAR_PLUGIN param1=stopContainer param2=$name param3=$container terminal=false refresh=true"
160+
echo "-------- kill | bash=$XBAR_PLUGIN param1=killContainer param2=$name param3=$container terminal=false refresh=true"
161+
echo "-------- pause | bash=$XBAR_PLUGIN param1=pauseContainer param2=$name param3=$container terminal=false refresh=true"
162+
echo "-------- unpause | bash=$XBAR_PLUGIN param1=unpauseContainer param2=$name param3=$container terminal=false refresh=true"
163+
done
164+
for stopped in $(vmContainers 'Created')
165+
do
166+
echo "---- $stopped"
167+
echo "------ Stopped"
168+
echo "-------- rm | bash=$XBAR_PLUGIN param1=rmContainer param2=$name param3=$stopped terminal=false refresh=true"
169+
echo "-------- start | bash=$XBAR_PLUGIN param1=startContainer param2=$name param3=$stopped terminal=false refresh=true"
170+
done
171+
for stopped in $(vmContainers 'Exited')
172+
do
173+
echo "---- $stopped"
174+
echo "------ Stopped"
175+
echo "-------- rm | bash=$XBAR_PLUGIN param1=rmContainer param2=$name param3=$stopped terminal=false refresh=true"
176+
echo "-------- start | bash=$XBAR_PLUGIN param1=startContainer param2=$name param3=$stopped terminal=false refresh=true"
163177
done
164178

165179
echo "-- Images"
@@ -181,18 +195,29 @@ function printMenu() {
181195
}
182196

183197
function vmContainers() {
198+
# $1 = status we're looking for
199+
local wantedStatus
200+
wantedStatus="${1}"
184201
# default VM doesn't need to be specified
185202
if [[ "$VM" != 'default' ]]; then
186203
export LIMA_INSTANCE="$VM"
187204
fi
188205
# shellcheck disable=SC2001,SC2046,SC2005
189-
containerList="[$(echo $(lima nerdctl ps --format '{{json .}},') | sed 's/,$//')]"
206+
containerList="[$(echo $(lima nerdctl ps -a --format '{{json .}},') | sed 's/,$//')]"
190207
# Can have spaces in our data, deal by using base64 (ugly)
191208
for row in $(echo "${containerList}" | jq -r '.[] | @base64'); do
192209
_jq() {
193210
echo "${row}" | base64 --decode | jq -r "${1}"
194211
}
195-
_jq '.Names'
212+
if [[ $(_jq '.Status') == "$wantedStatus" ]]; then
213+
name=$(_jq '.Names')
214+
id=$(_jq '.ID')
215+
if [[ "$name" != "" ]]; then
216+
echo "$name"
217+
else
218+
echo "$id"
219+
fi
220+
fi
196221
done
197222
}
198223

@@ -248,6 +273,24 @@ function rmImage() {
248273
fi
249274
}
250275

276+
function startContainer() {
277+
# arg1 = container
278+
# arg2 = VM
279+
local containerName
280+
local VM
281+
containerName="$1"
282+
VM="$2"
283+
if [[ "$VM" != 'default' ]]; then
284+
export LIMA_INSTANCE="$VM"
285+
fi
286+
displayNotification lima "Starting ${containerName} on ${VM}..."
287+
if lima nerdctl container start "${containerName}"; then
288+
displayNotification Lima "Started ${containerName}"
289+
else
290+
displayAlert Lima "Failed to start ${containerName} on ${VM}"
291+
fi
292+
}
293+
251294
function stopContainer() {
252295
# arg1 = container
253296
# arg2 = VM
@@ -284,6 +327,24 @@ function killContainer() {
284327
fi
285328
}
286329

330+
function rmContainer() {
331+
# arg1 = container
332+
# arg2 = VM
333+
local containerName
334+
local VM
335+
containerName="$1"
336+
VM="$2"
337+
if [[ "$VM" != 'default' ]]; then
338+
export LIMA_INSTANCE="$VM"
339+
fi
340+
displayNotification lima "Removing ${containerName} on ${VM}..."
341+
if lima nerdctl container rm "${containerName}"; then
342+
displayNotification Lima "Removed ${containerName}"
343+
else
344+
displayAlert Lima "Failed to remove ${containerName} on ${VM}"
345+
fi
346+
}
347+
287348
function pauseContainer() {
288349
# arg1 = container
289350
# arg2 = VM
@@ -328,6 +389,9 @@ function processMenuCommand() {
328389
pull)
329390
pullImage "$3" "$2" # pull imagename vmname
330391
;;
392+
startContainer)
393+
startContainer "$3" "$2" # stopContainer containerName VMname
394+
;;
331395
stopContainer)
332396
stopContainer "$3" "$2" # stopContainer containerName VMname
333397
;;
@@ -337,6 +401,9 @@ function processMenuCommand() {
337401
pauseContainer)
338402
pauseContainer "$3" "$2" # pauseContainer containerName VMname
339403
;;
404+
rmContainer)
405+
rmContainer "$3" "$2" # pauseContainer containerName VMname
406+
;;
340407
unpauseContainer)
341408
unpauseContainer "$3" "$2" # unpauseContainer containerName VMname
342409
;;

0 commit comments

Comments
 (0)